AakLak
AakLak

Reputation: 129

How do you use variables in a PHP SQL query?

Trying to execute SQL query like below in my PHP. It works fine when executed on database.

UPDATE `wp_essx95_cf7dbplugin_submits` SET field_value = 'hiya' WHERE submit_time = '1500944028.4748' and field_name='status'

In my PHP I have two values stored as variable. I'm calling it like this:

$SQL = "UPDATE `wp_essx95_cf7dbplugin_submits` SET field_value = '$status' WHERE submit_time = '$trade_num' and field_name='status'";
$result = mysqli_query($db_handle, $SQL);
return $result;

However it doesn't seem to be working.

Am I writing the statement correctly? How can I further debug the situation, like see the result of the PHP query. I have the PHP function return $result, and echo it on my page, but it doesn't seem to output anything.

echo(setStatus($time, $sts));

Thanks

Upvotes: 0

Views: 61

Answers (2)

Daniel Azamar
Daniel Azamar

Reputation: 692

Try what @Kevin P says.... I looks like works ;)

Upvotes: 0

Kevin P
Kevin P

Reputation: 601

Try escaping the variables and adding a die statement if an error occurs. Also do not forget to clean the data to prevent mysql injection.

$SQL = "UPDATE `wp_essx95_cf7dbplugin_submits` SET field_value = '".$status."' WHERE submit_time = '".$trade_num."' and field_name='status'";
$result = mysqli_query($db_handle, $SQL) or die(mysqli_error($db_handle));
return $result;

Upvotes: 2

Related Questions