0

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!

8
Contributors
14
Replies
15
Views
6 Years
Discussion Span
Last Post by klemme
Featured Replies
  • 1
    diafol 3,720   6 Years Ago

    Here's one mistake [CODE]$WStatus = $_POST['WStatus'];[/CODE] AND [CODE]WStatus='$Wstatus'[/CODE] Variable names are case-sensitive so $WStatus and $Wstatus are deemed two different vars. Also, is this right: [CODE]$DateCall = $_POST['CoPhone'];[/CODE] If you're trying to save a string as a date you might get a problem. //EDIT Actuallay: [CODE]$OrderID = $_POST['OrderID']; $CompanyID = … Read More

1

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.

Edited by diafol: n/a

0

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

0

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

0

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...

0

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.

0

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

0

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

0

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?

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.