Jump to content

Mysql update query isn't getting $_POST data


VictorVonKai

Recommended Posts

Hi, I'm having an issue with a script should be getting data via POST from a form and then updating the data based a 2nd post variable.  However, even though the post variables echo fine when I checked, they don't seem to work inside the update mysql query. I've checked on several websites and my syntax seems good.

 

 

Please Help!!

--Zach

<?php

$con = mysql_connect("localhost","NAME","PASSWORD");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db("DB", $con);
mysql_query("UPDATE DB SET value1 = $_POST['data1'] WHERE value = $_POST['data2']");
mysql_close($con);
?>

 

 

 

 

[attachment deleted by admin]

Most likely your query is invalid.  What types of columns are 'value1' and 'value'?  My guess is that your columns are a type that require single quotes around them.  For a quick and dirty way to display mysql errors you can modify your code to this:

 

mysql_query("UPDATE DB SET value1 = $_POST['data1'] WHERE value = $_POST['data2']") or die("Error: " . mysql_error());

 

For a better way and explanation of how to properly handle mysql errors you should read this - http://www.phpfreaks.com/blog/or-die-must-die.

You should also sanitize your post values with mysql_real_escape_string.

The code you posted contains fatal parse/syntax errors due to putting array variables inside of a string. You would need to put {} around each {$_POST[...]} variable and you should be developing and debugging your code on a system with error_reporting set to E_ALL and display_errors set to ON in your master php.ini (or a local php.ini or a .htaccess file) so that all the errors that php detects will be reported and displayed. You will save a ton of time.

 

Also, since the values are apparently text, you would need to enclose them in single-quotes inside of the query to prevent sql syntax errors.

 

mysql_query("UPDATE DB SET value1 = '{$_POST['data1']}' WHERE value = '{$_POST['data2']}'");

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.