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]

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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']}'");

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.