简体   繁体   中英

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. 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.

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

Then, before you execute your query, check for an ID in $_POST . 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. Also, if you use >= instead of > it will still work in case there are any missing IDs.

$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. You need to update your code to use either mysqli or PDO . Second, when you use a variable directly in your SQL, (like WHERE ID >= '$id' ) you are vulnerable to SQL injection. Binding your values to prepared statements will reduce that vulnerability.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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