Mysql UPDATE doesn't updates data

149 Views Asked by At

I have a weird problem when using my PHP file to update data using Mysql database.

here my code in file.php :

.....
.....
$con=mysqli_connect("mysite","user","mypass","mydb");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$data = json_decode(file_get_contents("php://input"));
$id = mysqli_real_escape_string($con, $data->id);
$sql = "UPDATE invites SET status='rejected' where id ='$id'";
....
....

I call it in my browser, like :

https://mysite/phpfile/file.php?id=224

But it doesn't updates my data. If I change the $id directly like :

$id = 224;

It changes my data.

How to resolve this problem?

1

There are 1 best solutions below

1
On

You should replace

$data = json_decode(file_get_contents("php://input"));
$id = mysqli_real_escape_string($con, $data->id);

with

id = mysqli_real_escape_string($con, $_REQUEST['id']);

You are reading the raw request data through the input stream then you decode it from JSON (although the stream you are reading is not in JSON format) and then you escape the id (which is not there of course. The id you are looking for is already put by PHP in the $_REQUEST superglobal. No need for all the rest ...