当我插入数据库时,它有效。但是当我尝试更新到数据库时,它不会插入值。
<?php
// Connecting to MySQL Database.
$con = mysqli_connect("localhost", "root", "****", "users");
// Getting the received JSON into $json variable.
$json = file_get_contents('php://input');
// decoding the received JSON and store into $obj variable.
$obj = json_decode($json,true);
$username = $obj['username'];
// Populate Student name from JSON $obj array and store into $S_Name.
$Q1 = $obj['q1'];
// Populate Student Class from JSON $obj array and store into $S_Class.
$Q2 = $obj['q2'];
// Populate Student Phone Number from JSON $obj array and store into $S_Phone_Number.
$Q3 = $obj['q3'];
// Populate Email from JSON $obj array and store into $S_Email.
$Q4 = $obj['q4'];
// Creating SQL query and insert the record into MySQL database table.
$Sql_Query = "UPDATE testuser SET q1= '$Q1', q2 = '$Q2', q3 = '$Q3', q4 = '$Q4' WHERE username = '$username'";
if(mysqli_query($con,$Sql_Query)){
// If the record inserted successfully then show the message.
$MSG = 'Record Successfully Inserted Into MySQL Database.' ;
// Converting the message into JSON format.
$json = json_encode($MSG);
// Echo the message.
echo $json ;
}
else{
echo 'Try Again';
}
mysqli_close($con);
?>
如果此代码有任何问题,请告诉我们。我还想说我是PHP和React Native的初学者。