<html> <head></head> <body> <!-- standard page header --> <?php // includes include('crm_conf.php'); // form not yet submitted // display initial form with values pre-filled if (!$_POST['submit']) { // check for record ID if ((!isset($_GET['id']) || trim($_GET['id']) == '')) { die('Missing record ID!'); } // open database connection $connection = mysql_connect($host, $user, $pass) or die ('Unable to connect!'); // select database mysql_select_db($db) or die ('Unable to select database!'); // generate and execute query $id = $_GET['id']; $query = "SELECT custID, custName, custAddr, custSupport, custSalesAgent, custVersion, custBalance FROM Cust_tbl WHERE custID = $id"; $result = mysql_query($query) or die ("Error in query: $query. " . mysql_error()); // if a result is returned if (mysql_num_rows($result) > 0) { // turn it into an object $row = mysql_fetch_object($result); // print form with values pre-filled ?> <table cellspacing="5" cellpadding="5"> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST"> <input type="hidden" name="id" value="<?php echo $id; ?>"> <tr> <td valign="top"><b><font size="-1">Custmer ID</font></b></td> <td> <input size="50" maxlength="250" type="text" name="id" value="<?php echo $row->custID; ?>"> </td> </tr> <tr> <td valign="top"><b><font size="-1">Customer Name</font></b></td> <td> <input size="50" maxlength="250" type="text" name="Name" value="<?php echo $row->custName; ?>"> </td> </tr> <tr> <td valign="top"><font size="-1">Customer Address</font></td> <td> <input size="50" maxlength="250" type="text" name="Addr" value="<?php echo $row->custAddr; ?>"> </td> </tr> <tr> <td valign="top"><font size="-1">Support Contract</font></td> <td> <input size="50" maxlength="250" type="text" name="Support" value="<?php echo $row->custSupport; ?>"> </td> </tr> <tr> <td valign="top"><font size="-1">Sales Agent</font></td> <td> <input size="50" maxlength="250" type="text" name="Agent" value="<?php echo $row->custSalesAgent; ?>"> </td> </tr> <tr> <td valign="top"><font size="-1">Aloha Version</font></td> <td> <input size="50" maxlength="250" type="text" name="Version" value="<?php echo $row->custVersion; ?>"> </td> </tr> <tr> <td valign="top"><font size="-1">Balance Owing</font></td> <td> <input size="50" maxlength="250" type="text" name="Balance" value="<?php echo $row->custBalance; ?>"> </td> </tr> <tr> <td colspan=2> <input type="Submit" name="submit" value="Update"> </td> </tr> </form> </table> <?php } // no result returned // print graceful error message else { echo '<font size=-1>That customer could not be located in our database.</font>'; } } else { // form submitted // start processing it // set up error list array $errorList = array(); $id = $_POST['custID']; $Name = $_POST['custName']; $Addr = $_POST['custAddr']; $Support = $_POST['custSupport']; $Agent = $_POST['custSalesAgent']; $Version = $_POST['custVersion']; $Balance = $_POST['custBalance']; // check for record ID if ((!isset($_POST['id']) || trim($_POST['id']) == '')) { die ('Missing record ID!'); } // check for errors // if none found... if (sizeof($errorList) == 0) { // open database connection $connection = mysql_connect($host, $user, $pass) or die ('Unable to connect!'); // select database mysql_select_db($db) or die ('Unable to select database!'); // generate and execute query $query = "UPDATE Cust_tbl SET custID = $id, custName = '$Name', custAddr = '$Addr', custSupport = '$Support', custSalesAgent = '$Agent', custVersion = '$Version', custBalance = $Balance WHERE custID = $id"; $result = mysql_query($query) or die ("Error in query: $query. " . mysql_error()); // print result echo '<font size=-1>Update successful.'; echo '<a href=crm_list.php>Go back to the main menu</a>.</font>'; // close database connection mysql_close($connection); } else { // errors occurred // print as list echo '<font size=-1>The following errors were encountered:'; } } ?> <!-- standard page footer --> </body> </html>
Error in query: UPDATE Cust_tbl SET custID = , custName = '', custAddr = '', custSupport = '', custSalesAgent = '', custVersion = '', custBalance = WHERE custID = . You have an error in your SQL syntax near ' custName = '', custAddr = '', custSupport = '', custSalesAgent = '', custVersio' at line 1 thank you GREATLOGIX
replace the original with this: $query = "UPDATE Cust_tbl SET custID = ".$id.", custName = ".$Name.", custAddr = ".$Addr.", custSupport = ".$Support.", custSalesAgent = ".$Agent.", custVersion = ".$Version.", custBalance = ".$Balance." WHERE custID = ".$id.""; PHP:
The Austin's query and the original are virtually identical, actually the original is more accurate (since custName in database is usually string, and should be compared to string '$name'). The error comes from the variables ($name, $id,...) haven't assigned any values. Change $id = $_POST['custID']; $Name = $_POST['custName']; $Addr = $_POST['custAddr']; $Support = $_POST['custSupport']; $Agent = $_POST['custSalesAgent']; $Version = $_POST['custVersion']; $Balance = $_POST['custBalance']; PHP: to $id = $_POST['ID']; $Name = $_POST['Name']; $Addr = $_POST['Addr']; $Support = $_POST['Support']; $Agent = $_POST['Agent']; $Version = $_POST['Version']; $Balance = $_POST['Balance']; PHP: The name of POST fields in html and $_POST are not matched. That causes the your error.
Its good to wake up in the morning wherein somebody expressed their gratitude on my problem. I will try this right away. More power to all of you guys! Dynx
Error in query: UPDATE Cust_tbl SET custID = , custName = John Salcedo, custAddr = Bedford NS, custSupport = Support, custSalesAgent = Steve Aucoin, custVersion = 6.2.19, custBalance = 100000 WHERE custID = . You have an error in your SQL syntax near ' custName = John Salcedo, custAddr = Bedford NS, custSupport = Support, custSale' at line 1
Why do you need to update the custID?? it's the Primary key. i would prefer like this: $query = "UPDATE Cust_tbl SET custName = '".$Name."', custAddr = '".$Addr."', custSupport = '".$Support."', custSalesAgent = '".$Agent."', custVersion = '".$Version."', custBalance = $Balance WHERE custID = $id"; PHP: where: $id = $_POST['id']; $Name = $_POST['Name']; $Addr = $_POST['Addr']; $Support = $_POST['Support']; $Agent = $_POST['Agent']; $Version = $_POST['Version']; $Balance = $_POST['Balance']; PHP:
I went back to my original codes and consolidate all the good inputs that was shared and came up with this codes: $query = "UPDATE Cust_tbl SET custName = '$Name', custAddr = '$Addr', custSupport = '$Support', custSalesAgent = '$Agent', custVersion = '$Version', custBalance = '$Balance' WHERE custID = '$id'"; More questions coming as Im done on my first phase in learning PHP. Thanks again!