Jump to content

PHP/MySQL error - cannot update table through form


Dan06

Recommended Posts

I'm trying to update a table based on a session variable. Unfortunately, I keep getting an invalid mysql code error. Below is the code I'm using:

 

if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "indProForm")) {
  $updateSQL = sprintf("UPDATE individualprofile SET City=%s, `State`=%s, Zip=%s, WHERE Id=" .$_SESSION['RegId'],
                       GetSQLValueString($_POST['City'], "text"),
                       GetSQLValueString($_POST['State'], "text"),
                       GetSQLValueString($_POST['Zip'], "text"),
                       GetSQLValueString($_POST['Id'], "text"));

  mysql_select_db($database_dbConnection, $dbConnection);
  $Result1 = mysql_query($updateSQL, $dbConnection) or die(mysql_error());
}

Help solving this problem is much appreciated.

The error message is:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE Id=' at line 1

 

As for single quotes, I've used this very type of code which uses double quotes, i.e.

"INSERT INTO registration (Id, FirstName, LastName, Email, Password) VALUES (%s, %s, %s, %s, %s)",
                       GetSQLValueString($_POST['Id'],"text"),
                       GetSQLValueString($_POST['FirstName'], "text"),
                       GetSQLValueString($_POST['LastName'], "text"),
                       GetSQLValueString($_POST['Email'], "text"),
                       GetSQLValueString($_POST['Password'], "text"));

for inserting records and there were no problem with that code. Why do you think that is?

try

"INSERT INTO registration (Id, FirstName, LastName, Email, `Password`) VALUES (%s, %s, %s, %s, %s)"

 

password() is a mysql function so you need the ` marks.

 

I think what PFM was talking about is to put ' around %s, but I'm guessing that is one thing that GetSQLValueString() does automatically... if not, you'll want to change all %s to '%s' as well.

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.