Update query not working properly

Bill Sullivan
Bill Sullivan used Ask the Experts™
I have a problem updating a mysql table entry.  I copied and pasted the format I've used for years to update tables, but for some reason it is not working here.

My query reads, $queryUpdate1=mysql_query("UPDATE material SET _3Quan = '$_3Quan' WHERE file='$file'") or die('Query failed1: '.mysql_error());

The variable $_3Quan has a value of 100, but when the material table is updated, it returns a value of 0 to the _3Quan field.  Prior to update the value was 95.

Can you help me figure out what I'm doing wrong here?
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Ryan ChongSoftware Team Lead

try check what you getting there...

$sql = "UPDATE material SET _3Quan = '$_3Quan' WHERE file='$file'";

Open in new window

if the sql generated looks good, then the decrepancy is most likely causing by other issues.
Zakaria AcharkiAnalyst Developer
Distinguished Expert 2018

The extension you're using was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0, you may need to use MySQL Improved Extension "mysqli" instead, it's simple and it will give you the exact error happened in your query like :

$con = mysqli_connect("my_host","my_user","my_password","my_db");

// Perform a query, check for error
if (!mysqli_query($con, "UPDATE material SET _3Quan = '$_3Quan' WHERE file='$file'"))
	echo("Error description: " . mysqli_error($con));

Open in new window

Else you could always use mysql_error() to see what happen exactly :

$queryUpdate1 = mysql_query("UPDATE material SET _3Quan = '$_3Quan' WHERE file='$file'");
if (!$queryUpdate1) {
    trigger_error('Invalid query: ' . mysql_error());

Open in new window


When a query fails I usually check PHP log file and I try to run the query manually in PHPmyAdmin to help me find the problem.

Make sure you enable error reporting from PHP http://php.net/manual/en/function.error-reporting.php

If you can use https://xdebug.org/ it is a fantastic tool (this is bundle with wampserver)
i'd assume you enabled strict mode or upgraded to a version that uses strict mode. so '100' is actually text and it's numeric value is not 100.

also note that your code is wide open to sql injections.

for both issues, you may wish to use var_export instead of '$whatever' . other ways to deal with this include using mysql_real_escape_string or prepared queries.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial