Re: update db with variables

From: Date: Wed, 24 Sep 2003 15:54:59 +0000
Subject: Re: update db with variables
References: 1 2  Groups: php.db 
Request: Send a blank email to php-db+get-30664@lists.php.net to get a copy of this message
Thanks to all. Single quotes around '$value' did the trick. Robbie CPT John W. Holmes wrote:
From: "Robbie Staufer" <rstaufer@scd.ucar.edu>
I'm writing an application that will display the contents of a db in the browser, including the db-assigned id number. The user can then enter the id number in a form and the browser will display the record corresponding to that id number, with the data in editable text fields. The user edits the text fields and submits to an 'update' script using POST, and the update script puts the new values back into the database. In the update script, I'm trying to access the field names and values in the $_POST array, and update the db using a loop:
                               while ($data = each($_POST)) {
                                           $name = $data[ 'key' ];
                                           $value = $data[ 'value' ];
                                           echo $name, $value;
                                           $result =
mysql_query("UPDATE tablename SET $name = $value WHERE ID=$id") or die ("unable to update record");
                              }
The echo statement shows me that the script dies on the first time through the loop.
Put quotes around your strings in your SQL: mysql_query("UPDATE tablename SET $name = '$value' WHERE ID=$id") Also, instead of "dieing" with your own error message, use mysql_error(), which would have given you a better error to act upon... or die("Error in update: " . mysql_error()); ---John Holmes...
-- -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* Robbie Staufer NCAR/SCD 1850 Table Mesa Dr. Rm. 42 Boulder, CO. 80305 (303) 497-1836

« previous php.db (#30664) next »