简体   繁体   English

PHP 脚本:如何返回正在写入的 SQL 记录的 ID 字段(主键自增 1)的值

[英]PHP Scripting: How to return the value of the ID field (primary key auto-increment 1) of the SQL record being written to

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'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.我想将存储响应的 users_data 表记录中的ID 值传递给survey_two,以便将survey_two 中的结果与第一页的结果发布到 user_data 中的同一记录中。

The idea here being that survey_respondants don't need to login or otherwise identify themselves to answer the survey.这里的想法是,survey_respondants 不需要登录或以其他方式表明自己的身份来回答调查。

I think $_GET might be the way;我认为 $_GET 可能是这样; but I'm new to PHP....但我是 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
    }
}
?>

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.考虑到您正在使用汽车,车牌号或 VIN 将是您可以保存的唯一信息的一个很好的示例。

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

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

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