Home > OS >  PHP Scripting: How to return the value of the ID field (primary key auto-increment 1) of the SQL rec
PHP Scripting: How to return the value of the ID field (primary key auto-increment 1) of the SQL rec

Time:01-17

I'm creating a multi-page web survey form, and I have a PHP script process.php that takes my responses on www.buythosecars.com and places them in a mySQL table and redirects the user to www.buythosecars.com/survey_two.html.

I want to pass the ID value from the users_data table record where the responses are being stored, to survey_two so that the results in survey_two will get posted to the same record in user_data as the results from the first page.

The idea here being that survey_respondants don't need to login or otherwise identify themselves to answer the survey.

I think $_GET might be the way; but I'm new to PHP....

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {//Check it is coming from a form

    //mysql credentials
    $mysql_host = "buythosecarscom.fatcowmysql.com";
    $mysql_username = "[redacted]";
    $mysql_password = "[redacted]";
    $mysql_database = "buythatcar";
    
//header("Location: survey_two.html");

    $u_q1 = filter_var($_POST["question_1"], FILTER_SANITIZE_STRING); //set PHP variables like this so we can use them anywhere in code below
    $u_q2 = filter_var($_POST["question_2"], FILTER_SANITIZE_STRING);
    $u_q3 = filter_var($_POST["question_3"], FILTER_SANITIZE_STRING);
        $u_q4 = filter_var($_POST["question_4"], FILTER_SANITIZE_STRING);
        $u_q4b = filter_var($_POST["question_4b"], FILTER_SANITIZE_STRING);
        $u_q5 = filter_var($_POST["question_5"], FILTER_SANITIZE_STRING);        
        $u_q6 = filter_var($_POST["question_6"], FILTER_SANITIZE_STRING);
        $u_q7 = filter_var($_POST["question_7"], FILTER_SANITIZE_STRING);
        $u_q8 = filter_var($_POST["question_8"], FILTER_SANITIZE_STRING);
        $u_q9 = filter_var($_POST["question_9"], FILTER_SANITIZE_STRING);
        $u_q10 = filter_var($_POST["question_10"], FILTER_SANITIZE_STRING);

    //Open a new connection to the MySQL server
    $mysqli = new mysqli($mysql_host, $mysql_username, $mysql_password, $mysql_database);
    
    //Output any connection error
    if ($mysqli->connect_error) {
        die('Error : ('. $mysqli->connect_errno .') '. $mysqli->connect_error);
    }   
    
    $statement = $mysqli->prepare("INSERT INTO users_data (question_1, question_2, question_3, question_4, question_4b, question_5, question_6, question_7, question_8, question_9, question_10) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"); //prepare sql insert query
    //bind parameters for markers, where (s = string, i = integer, d = double,  b = blob)
    $statement->bind_param('sssssssssss', $u_q1, $u_q2, $u_q3, $u_q4, $u_q4b, $u_q5, $u_q6, $u_q7, $u_q8,  $u_q9, $u_q10); //bind values and execute insert query
    
    if($statement->execute()){
//Modify to print the ID of the record where the response to question 3 has 
//been entered instead of the response to question 3        
print "Hello " . $u_q3 . "!, your message has been saved!";
    }else{
        print $mysqli->error; //show mysql error if any
    }
}
?>

CodePudding user response:

When you're saving that value, you're automatically redirecting the user to the second page. At this point, you could simply get the most recently inserted value:

$mysqli->insert_id

It's highly unlikely this won't be your target, though be aware that this could be an issue with precise timing and lots of users, if another user is entering their information at the exact same time.

To prevent this, I'd recommend simply retrieving the information on the second page based on a unique piece of information that's passed across in the first page. Considering you're working with cars, a licence plate number or VIN would be a great example of information that you could save that would be unique.

Then on the next page you can retrieve the target with something like:

$targetVIN = 1; // Logic to target your user based on unique ID
$query = "SELECT * FROM `cars` WHERE `vin` = " . $targetVIN;
$result = $mysqli->query($query);
while ($car = $result->fetch_object()) {
    // Access properties via $car->vin, etc.
}
  •  Tags:  
  • Related