I have a html form on another page that is automatically filled from data from the table. I then run the the following script on submit to update the row if any information is changed. Here is the code

<?php

$db=mysql_connect  ("connection info") or die ('I cannot connect to the database  because: ' . mysql_error());
 
  $mydb=mysql_select_db("databasename");

$OrderID = $_POST['OrderID'];
$CompanyID = $_POST['CompanyID'];
$CoPhone = $_POST['CoPhone'];
$CustomerID = $_POST['OrderID'];
$Customer = $_POST['CompanyID'];
$Address = $_POST['CoPhone'];
$City = $_POST['OrderID'];
$State = $_POST['CompanyID'];
$ZipCode = $_POST['CoPhone'];
$Telephone = $_POST['OrderID'];
$ServiceCategory = $_POST['CompanyID'];
$DateCall = $_POST['CoPhone'];
$WStatus = $_POST['WStatus'];

$result = mysql_query("UPDATE TableName SET OrderID='$OrderID', CompanyID='$CompanyID', CoPhone='$CoPhone', CustomerID='$CustomerID', Address='$Address', City='$City', State='$State', ZipCode='$ZipCode', Telephone='$Telephone', ServiceCategory='$ServiceCategory, DateCall='$DateCall', WStatus='$Wstatus' WHERE OrderID='$OrderID'");
if (!$result) {
    die("oh snap I got an error. It is ".mysql_error());
}  

?>

This is my first update script and I can't get it to work...I'm pretty sure it's something simple. Any help is appreciated!

Recommended Answers

All 14 Replies

could u post the error message?

yeah, it's kinda hard to tell where the problem lies, try echoing the query itself and running it in mysql.

echo the values of Post and see if they work then from there we can track the query.

Member Avatar for diafol

Here's one mistake

$WStatus = $_POST['WStatus'];

AND

WStatus='$Wstatus'

Variable names are case-sensitive so $WStatus and $Wstatus are deemed two different vars.

Also, is this right:

$DateCall = $_POST['CoPhone'];

If you're trying to save a string as a date you might get a problem.

//EDIT
Actuallay:

$OrderID = $_POST['OrderID'];
$CompanyID = $_POST['CompanyID'];
$CoPhone = $_POST['CoPhone'];
$CustomerID = $_POST['OrderID'];
$Customer = $_POST['CompanyID'];
$Address = $_POST['CoPhone'];
$City = $_POST['OrderID'];
$State = $_POST['CompanyID'];
$ZipCode = $_POST['CoPhone'];
$Telephone = $_POST['OrderID'];
$ServiceCategory = $_POST['CompanyID'];
$DateCall = $_POST['CoPhone'];
$WStatus = $_POST['WStatus'];

There seems to be a lot of repetition of POST vars - have you mapped these properly? They don't seem to make a lot of sense.

hey, yeah just noticed that. Copy pasted from wrong notepad. I'll check the errors then try again. Thanks.

This is the error I'm getting:

oh crap I got an error. It 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 '', DateCall='', WStatus='' WHERE OrderID=''' at line 1

Made some changes and corrections and now have this code:

$mydb=mysql_select_db("Repairs");

$OrderID = $_POST['OrderID'];
$CompanyID = $_POST['CompanyID'];
$CoPhone = $_POST['CoPhone'];
$CustomerID = $_POST['CustomerID'];
$Customer = $_POST['Customer'];
$Address = $_POST['Address'];
$City = $_POST['City'];
$State = $_POST['State'];
$ZipCode = $_POST['ZipCode'];
$Telephone = $_POST['Telephone'];
$ServiceCategory = $_POST['ServiceCategory'];
$DateCall = $_POST['DateCall'];
$WStatus = $_POST['WStatus'];

$result = mysql_query("UPDATE C SET OrderID='$OrderID', CompanyID='$CompanyID', CoPhone='$CoPhone', CustomerID='$CustomerID', Address='$Address', City='$City', State='$State', ZipCode='$ZipCode', Telephone='$Telephone', ServiceCategory='$ServiceCategory', DateCall='$DateCall', WStatus='$WStatus' WHERE OrderID='$OrderID'");
if (!$result) {
    die("oh crap I got an error. It is ".mysql_error());
}  
echo "Record Edited"; 
mysql_close($db)
?>

It echoes "record edited" but the record is not changed in the database...

Member Avatar for diafol

Look at mysql_affected_rows() and echo that out. See which rows affected, if any.

Really new so this might be a stupid question but how should I set up that mysql_affect_rows?

Look at mysql_affected_rows() and echo that out. See which rows affected, if any.

ok figured it out myself. Small error with a parenthesis. Here is the correct code for anyone else who gets stuck.

<?php

$db=mysql_connect  ("connection_information") or die ('I cannot connect to the database  because: ' . mysql_error());
 
  $mydb=mysql_select_db("databasename");

$OrderID = $_POST['OrderID'];
$CompanyID = $_POST['CompanyID'];
$CoPhone = $_POST['CoPhone'];
$CustomerID = $_POST['CustomerID'];
$Customer = $_POST['Customer'];
$Address = $_POST['Address'];
$City = $_POST['City'];
$State = $_POST['State'];
$ZipCode = $_POST['ZipCode'];
$Telephone = $_POST['Telephone'];
$ServiceCategory = $_POST['ServiceCategory'];
$DateCall = $_POST['DateCall'];
$WStatus = $_POST['WStatus'];

$result = mysql_query("UPDATE Table_name SET OrderID='$OrderID', CompanyID='$CompanyID', CoPhone='$CoPhone', CustomerID='$CustomerID', Address='$Address', City='$City', State='$State', ZipCode='$ZipCode', Telephone='$Telephone', ServiceCategory='$ServiceCategory', DateCall='$DateCall', WStatus='$WStatus' WHERE OrderID='$OrderID'");
if (!$result) {
    die("oh crap I got an error. It is ".mysql_error());
}  
echo "Record Edited"; 
mysql_close($db)
?>

Thanks for the help

My apologies forum.. I did not see that this poast was closed so will re-post

Is orderid your primary key in the database?
Or else it should be:

where id='$orderid';

That would target one specific row to be updated.

Maybe there isnt any orderid which is equal to '$orderid', just a thought?

oops solved, sorry...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.