Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

sql update not working

Guest
Apr 05, 2010 Apr 05, 2010

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.

TOPICS
Server side applications
656
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Apr 06, 2010 Apr 06, 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 paramet

...
Translate
LEGEND ,
Apr 05, 2010 Apr 05, 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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Apr 06, 2010 Apr 06, 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 06, 2010 Apr 06, 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");

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Apr 07, 2010 Apr 07, 2010
LATEST

I  see now what you mean. As suggested I swapped the params and all works perfectly.

Thank you ever so much for your help, much appreciated.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines