When I did insert into the database, it worked. But when I try Update into the Database it doesn't insert the value.
<?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);
?>
If there is anything wrong with this code, please let me know. I also want to say that I am a beginner in PHP and React Native.