Skip to main content
April 5, 2010
Answered

sql update not working

  • April 5, 2010
  • 1 reply
  • 652 views

Hi,

I am trying to update my database with the following code but even though the code definately runs because it does get to the echos on the line before the update, the database is not getting updated.

if ($totalRows_rsportsale >'0') {
$_POST['dpaid'] = 1;
$_POST['listedby'] = $row_rsportid['listedid'];
echo 'dpaid is: ' . $_POST['dpaid'] . '<br />';
echo 'listedby is: ' . $_POST['listedby'] . '<br />';
$updateSQL = sprintf("UPDATE detailstable SET dpaid=%s WHERE listedby=%s",
                                     GetSQLValueString($_POST['listedby'], "int"),
                                     GetSQLValueString($_POST['dpaid'], "int"));

  mysql_select_db($database_connUpdate, $connUpdate);
  $Result1 = mysql_query($updateSQL, $connUpdate) or die(mysql_error());
}

The values that i want to update in the database are being echoed right before the update script, so the values are correct for dpaid and listedby, i have entered the update bit of the code in the sql in myphp admin and that does update the database. The connection for the database connUpdate is correct. Both fields are "int" in the database and are not primary keys or unique. Any suggestions as to why this update is not working would be much appreciated.

Thank you in advance.

This topic has been closed for replies.
Correct answer bregent

No, I don't mean the order of SQL clauses, I mean the order of the two parameters you are using.

$updateSQL = sprintf("UPDATE detailstable SET dpaid=%s WHERE  listedby=%s",
                                      GetSQLValueString($_POST['listedby'], "int"),
                                      GetSQLValueString($_POST['dpaid'], "int"));

As I said, I don't know PHP or the sprintf command, but I would assume that it replaces the parameters  (%s) in the order they are listed. Your SQL statement parameters (listedby and dpaid) are in the reverse order. Try switching them.

$updateSQL = sprintf("UPDATE detailstable SET dpaid=%s WHERE  listedby=%s",
                                      GetSQLValueString($_POST['dpaid'], "int")),

                                     GetSQLValueString($_POST['listedby'], "int");

1 reply

Participating Frequently
April 5, 2010

I don't know PHP, but it seems like you have your parms listed in the wrong order. Your set value is listed after your where clause value. Right?

April 6, 2010

I´m pretty sure i have things in the right order, UPDATE, SET, WHERE. and I have other updates in my site that work on this format. I think I must be missing something obvious but cannot see it.

Look forward to further suggestions.

bregentCorrect answer
Participating Frequently
April 6, 2010

No, I don't mean the order of SQL clauses, I mean the order of the two parameters you are using.

$updateSQL = sprintf("UPDATE detailstable SET dpaid=%s WHERE  listedby=%s",
                                      GetSQLValueString($_POST['listedby'], "int"),
                                      GetSQLValueString($_POST['dpaid'], "int"));

As I said, I don't know PHP or the sprintf command, but I would assume that it replaces the parameters  (%s) in the order they are listed. Your SQL statement parameters (listedby and dpaid) are in the reverse order. Try switching them.

$updateSQL = sprintf("UPDATE detailstable SET dpaid=%s WHERE  listedby=%s",
                                      GetSQLValueString($_POST['dpaid'], "int")),

                                     GetSQLValueString($_POST['listedby'], "int");