I have an update statement which should update a product using information the user has submitted from a form

The error being that nothing gets updated, the sql code does nothing, and changes nothing in the database
Can anyone see an error in the code?

$user_id = ($_SESSION['user_id']);
	$product_id = ($_POST['product_id']);
	$sql_updateProduct = "UPDATE products SET title = '$_POST[title]', director = '$_POST[director]', cert_id = '$_POST[certificate]', cat_id = '$_POST[category]', format_id = '$_POST[format]', condition_id = '$_POST[condition]', user_id = '$user_id', description = '$_POST[description]', image = '$_POST[image]' WHERE product_id = '$product_id'";
	mysql_query($sql_updateProduct) or die (mysql_error());

		echo"<h1>Record Updated</h1>";

Everything above this section of code works fine, I have narrowed the issue down to this section of code. Any help would be much appreciated.

Recommended Answers

All 5 Replies

Have you tested the connection to the database?

$con = mysql_connect("localhost","username","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("my_db", $con);

Are you sure you're wanting to update an existing record? Or are you trying to add a brand new record (in which case you want an 'INSERT INTO' statement rather than an 'UPDATE'). I can't seem to see any immediate errors here... Do you get a mysql error printed to the browser? or do you see the 'Record updated'

the connection is fine as there are other queries using the same connection. And yes I am trying to update an existing record, there is a form previous to this is populated with existing product information the user then edits any of the fields they require and then clicks update.
When this process.update.php script runs I see record updated, however there is no change in the product

perhaps your form values aren't being passed correctly.
Perform a check to see if they're set first:

if(isset($_SESSION['user_id']))
{
   echo "it's set";
}

Or alternatively simply echo out each of the $_POST values to see if they're all there

they are set correctly. I have figured out further that if I take out the WHERE statement out it works, changing all the values in the table. So perhaps its something to do with the product_id = $product_id

I just can't work it out though

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.