Page 1 of 1

UPDATE Mysql with PHP Variables

Posted: Mon Jul 21, 2008 2:42 pm
by OrganicMan
I'm a newbie to PHP and trying to UPDATE a Mysql db from a form on a PHP page. My code doesn't throw any errors, but as far as I can tell, nothing happens to the DB. Any thoughts on how to get this to work? Orders_id is the primary key for the zen_orders table and is autoincrement.

Code: Select all

 
<?php
if (!isset($_POST['submit'])) {
?>
<form action="" method="post">
Order ID: <input type="text" name="orders_id"><br />
Customer ID: <input type="text" name="customers_id"><br />
Customer Name: <input type="text" name="customers_name"><br />
Customer City: <input type="text" name="customers_city">
<input type="submit" name="submit" value="Submit!">
</form>
<?php
} else {
$orders_id = $_POST['orders_id'];
$customers_id = $_POST['customers_id'];
$customers_name = $_POST['customers_name'];
$customers_city = $_POST['customers_city'];
//MySQL string:
$sql = "UPDATE zen_orders SET
    orders_id = '$orders_id',
    customers_id = '$customers_id'
    customers_name = '$customers_name'
    WHERE orders_id='$orders_id'";
    $result = mysql_query($sql);
 
//Output a result
if($result){
    echo "Update Successful!";
} else {
    $message = "Failed to update! This is the MySQL Error output:<br>" + mysql_error();}
}
?>

Re: UPDATE Mysql with PHP Variables

Posted: Mon Jul 21, 2008 3:49 pm
by califdon
For future reference, please enclose any code in your posts with tags to make it more readable, as I have done for you in your original post.

Your basic problem is that you are including the autonumber field in your Update SQL. Just remove the field from both the SET part and the VALUES part. Just be sure that you have it in the Where clause, which you do (otherwise, it would replace ALL the data in your table!).

It would be advisable for you to use the die() function to report back on SQL errors, like this:

Code: Select all

 mysql_query($sql) or die("Update query failed - " . mysql_error() );
There is no need to use the $result variable except in a Select query.

Re: UPDATE Mysql with PHP Variables

Posted: Mon Jul 21, 2008 3:52 pm
by Dynamis
OrganicMan wrote:

Code: Select all

 
$sql = "UPDATE zen_orders SET
    orders_id = '$orders_id',
    customers_id = '$customers_id'
    customers_name = '$customers_name'
    WHERE orders_id='$orders_id'";
    $result = mysql_query($sql);
 
Looks like you are missing a comma also. And califdon is correct in saying it is quite weird to update orders_id while you are searching for orders_id. Just adding a comma and not removing your update orders_id while searching for orders_id, the code is as follows:

Code: Select all

 
$sql = "UPDATE zen_orders SET
    orders_id = '$orders_id',
    customers_id = '$customers_id',
    customers_name = '$customers_name'
    WHERE orders_id='$orders_id'";
    $result = mysql_query($sql);
 

Re: UPDATE Mysql with PHP Variables

Posted: Mon Jul 21, 2008 5:02 pm
by OrganicMan
Works fine now. I will keep on refining it. Thanks so much for your help.