Please help Im having problem with my codes the time I hate UPDATE. I got an error.

Discussion in 'PHP' started by dynx, Jul 27, 2009.

  1. #1
    <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>
     
    dynx, Jul 27, 2009 IP
  2. greatlogix

    greatlogix Active Member

    Messages:
    664
    Likes Received:
    13
    Best Answers:
    1
    Trophy Points:
    85
    #2
    what is the error you are getting. Post here
     
    greatlogix, Jul 27, 2009 IP
  3. dynx

    dynx Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    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
     
    dynx, Jul 27, 2009 IP
  4. AustinQPT

    AustinQPT Member

    Messages:
    75
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    43
    #4
    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:
     
    AustinQPT, Jul 27, 2009 IP
  5. zandigo

    zandigo Greenhorn

    Messages:
    71
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #5
    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.
     
    zandigo, Jul 27, 2009 IP
  6. dynx

    dynx Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    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
     
    dynx, Jul 28, 2009 IP
  7. dynx

    dynx Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    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
     
    dynx, Jul 28, 2009 IP
  8. ivan.kristianto

    ivan.kristianto Active Member

    Messages:
    136
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #8
    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:
     
    ivan.kristianto, Jul 28, 2009 IP
  9. dynx

    dynx Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    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!
     
    dynx, Jul 28, 2009 IP