AakLak AakLak - 3 years ago 55
MySQL Question

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

Answer Source

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;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download