简体   繁体   English

如何使用php和mysql从数据库一次提取一行

[英]How to pull one row at a time from the database using php and mysql

I am creating a survey that asks the user questions in order to determine what Majors would be most appropriate for that user based on their answers. 我正在创建一个调查问卷,询问用户问题,以便根据他们的答案来确定最适合该用户的专业。 I want to display the questions one at a time. 我想一次显示一个问题。 The user should be able to press the next button and the next question stored in the database will appear on the screen. 用户应该能够按下下一步按钮,并且数据库中存储的下一个问题将出现在屏幕上。 Right now I have my code working to where it displays the first question from the database, but I still need to get the next button working. 现在,我的代码可以在其中显示数据库中第一个问题的位置,但是我仍然需要使下一个按钮起作用。 I need an efficient way to do this since I have 60 questions stored in the database. 由于数据库中存储了60个问题,因此我需要一种有效的方法。 Any help is greatly appreciated! 任何帮助是极大的赞赏! Below is a picture of how my survey currently looks! 以下是我的问卷调查外观的图片!

Survey Link 调查链接

Below is my code! 下面是我的代码!

$dbhost = "localhost";
$dbuser = "root";
$dbpass = "Cherries7";

$conn = mysql_connect($dbhost, $dbuser, $dbpass);

if(! $conn )
{
        die('Could not connect: ' . mysql_error());
}

$id = 1;        //global variable that represents id

$sql = "SELECT questiontext FROM md WHERE ID= '$id'" ;
mysql_select_db('MajorDecider');
$retval = mysql_query( $sql, $conn );
$nr = mysql_num_rows($retval);

if(! $retval ) {
        die('Could not get data: ' . mysql_error());
}

else if($nr == 0) {
    echo "<h2>Question not found.</h2>";
}

//there should only be one question with specified id number
else if($nr == 1) {
    $row = mysql_fetch_array($retval);
    echo " <form  method='POST'>
    <section id='question'><table id='questions'> 
    <tr> 
        <td> {$row['questiontext']} </td> 
        <td> <input type='radio' name='choose' id='interested' value='true' /> Yes </td> 
        <td> <input type='radio' name='choose' id='uninterested' value='false' /> No </td>          
        <td> <button name='next'>Next</button> </td> 
    </tr> 
    </table></section></form> ";
}

Add a hidden input inside your form that holds the row ID. 在表单中添加一个隐藏的输入,其中包含行ID。

<input type='hidden' name='id' value='{$row['ID']}'>

Then, before you execute your query, check for an ID in $_POST . 然后,在执行查询之前,请在$_POST检查ID。 If there's one there, increment it and use that. 如果那里有一个,增加它并使用它。

$id = 1;
if (isset($_POST['id'])) {
    $id = $_POST['id'] + 1;
}

You'll need to modify your query a little so that it selects the ID column as well. 您需要稍微修改一下查询,以便它也选择ID列。 Also, if you use >= instead of > it will still work in case there are any missing IDs. 另外,如果使用>=而不是> ,则在缺少任何ID的情况下仍然可以使用。

$sql = "SELECT ID, questiontext FROM md WHERE ID >= '$id' ORDER BY ID";

Two more things, already mentioned in the comments , but important enough to reiterate. 注释中已经提到了另外两件事,但有两点要重申。 First, you are using the mysql extension, which is no longer supported by PHP. 首先,您正在使用mysql扩展,PHP不再支持该扩展。 You need to update your code to use either mysqli or PDO . 您需要更新代码以使用mysqliPDO Second, when you use a variable directly in your SQL, (like WHERE ID >= '$id' ) you are vulnerable to SQL injection. 其次,当您直接在SQL中使用变量时(例如WHERE ID >= '$id' ),您很容易受到SQL注入的攻击。 Binding your values to prepared statements will reduce that vulnerability. 将您的值绑定到准备好的语句将减少该漏洞。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM