Update form not working

Hi,  

I am going crazy withis update code. I am trying to update a page which has data pulled from a database. But those fields are empty every time. Even when i fill it up and hit update i get an error. I am posting the code...hope it makes sense:


<?php require_once ('../includes/config.inc') ?>
<?php require_once ("../../mysql_connect.php") ?>
<?php include ("../includes/customer_header.html") ?>

<?php if(isset($_SESSION["username"]))
{
// get customerid from session
$cid = $_SESSION['customerid'];

if (isset($_POST['submit'])) { // Handle the form.

$message = NULL; // Create an empty new variable.

if (empty($_POST['first_name']) || empty($_POST['last_name']) || empty($_POST['business_name'])) {
$message .= "<span class='errormessage'><p align='left'>We could not process your request for the following reason(s):</p></span>";
}
//Check for first_name
if (empty($_POST['first_name'])) {
      $fn = False;
      $message .= "<span class='errormessage'><div align='left'>• Missing First Name.</div></span>";
} else {
      $fn = escape_data($_POST['first_name']);
}

//Check for last_name
if (empty($_POST['last_name'])) {
      $ln = False;
      $message .= "<span class='errormessage'><div align='left'>• Missing Last Name.</div></span>";
} else {
      $ln = escape_data($_POST['last_name']);
}

//Check for business_name
if (empty($_POST['business_name'])) {
      $bn = False;
      $message .= "<span class='errormessage'><div align='left'>• Missing Business Name.</div></span>";
} else {
      $bn = escape_data($_POST['business_name']);
}
      
      if ($fn && $ln && $bn) { // If everything's OK.

            // Make the query.
            $query = "UPDATE customers SET first_name='$fn', last_name='$ln', business_name='$bn' WHERE customerid='$cid'";            
            $result = @mysql_query ($query); // Run the query.
            if (mysql_affected_rows() == 1) { // If it ran OK.
                  
            $message .= "<span class='errormessage'><div align='center'>Your store info has been updated.</div></span>";
                        
                  } else { // If it did not run OK.
                        $message .= "<span class='errormessage'><div align='center'>Your store info could not be updated due to a system error. We apologize for any inconvenience.</div></span><p>" . mysql_error() . "</p>";
}

mysql_close(); //close database connection
}
} //end of submit conditional


// Retrieve the user's information.
$query = "SELECT * FROM customers WHERE customerid = '$cid'";            
$result = @mysql_query ($query); // Run the query.

if (mysql_num_rows($result) == 1) { // Valid user ID, show the form.

      // Get the user's information.
      $row = mysql_fetch_array($result, MYSQL_ASSOC);
      
echo ' <table border="0" width="100%" cellspacing="0" cellpadding="0">
      <tr>
            <td valign="top" width="370"><a href="index.php">Home</a> » <span class="content">Edit Store Info</span></td>
            <td valign="top">
            <p align="right"><span class="content">You are logged in as: <b>'.$_SESSION["username"];
echo '</b></span><br><a href="../logout.php">Logout</a></td>
      </tr>
</table>
                        <div align="center">';
                        if(isset($message)) echo $message;
                        echo' </div><br><br>
                        <div align="center">
                              <table border="0" width="70%" cellspacing="0" cellpadding="0">
                                    <tr>
                                          <td valign="top" align="right"><span class="darkbluesmall">All fields marked * are required.</span></td>
                                    </tr>
                              </table>
                        </div>
                        <form name="form1" method="post" action='.$_SERVER["PHP_SELF"];
                        echo '>
                        <div align="center">
                              <table class="inputform" borderColor="#f0f0f0" border="1">
                                    <tr class="rowcolor1"><td width="162">First Name:</td>
                                          <td><input type="text" name="first_name" class="textbox" maxlength="50" value="';
                        if(isset($_POST['first_name'])) echo $_POST['first_name'];
                        echo'"><span class="darkbluesmall"> *</span></td>
                                    </tr>
                                    <tr class="rowcolor2"><td width="162">Last Name:</td>
                                          <td><input type="text" name="last_name" class="textbox" maxlength="50" value="';
                        if(isset($_POST['last_name'])) echo $_POST['last_name'];
                        echo'"><span class="darkbluesmall"> *</span></td>
                                    </tr>
                                    <tr class="rowcolor1"><td width="162">Business Name:</td>
                                          <td><input type="text" name="business_name" class="textbox" maxlength="200" value="';
                        if(isset($_POST['business_name'])) echo $_POST['business_name'];
                        echo'"><span class="darkbluesmall"> *</span></td>
                                    </tr>
                                    <tr class="rowcolor2"><td width="162">Address:</td>
                                          <td><input type="text" name="address1" class="textbox" maxlength="200" value="';
                        if(isset($_POST['address1'])) echo $_POST['address1'];
                        echo'"><br><input type="text" name="address2" class="textbox" maxlength="200" value="';
                        if(isset($_POST['address2'])) echo $_POST['address2'];
                        echo'"></td>
                                    </tr>
                              </table>
                        </div>
                        <div align="center">
                              <table cellSpacing="0" cellPadding="0" width="70%" border="0">
                                    <tr>
                                          <td width="50%">
                                                <p align="right"><BR><input type="submit" name="submit" value="Edit Store" class="button">
                                                      </p>
                                          </td>
                                          <td width="1%"></td>
                                          <td width="49%">
                                                <p align="left"><BR><input type="reset" name="Submit2" value="Clear" class="button">
                                                </td>
                                    </tr>
                              </table>
                              </form>
                              </div> ';
                              } else { // Not a valid user ID.
                        echo '<h1 id="mainhead">Page Error</h1>
                        <p class="error">This page has been accessed in error.</p><p><br /><br /></p>';
                        }
                        
                        } else {
                        echo '<span class="content"><br><br><br><br><br><br><p align="center">You are not logged in.<br><br>To login click <a href="../login.php">here</a>.</p></span>';
            }
include ('../includes/customer_footer.html');
?>
stevegingellAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

BogoJokerCommented:
Hi stevegingell,

Have you checked if there is an error message?
Change:
$result = @mysql_query ($query);
To:
$result = mysql_query ($query) or die(mysql_error());

Also do some more checks, after:
$query = "UPDATE customers SET first_name='$fn', last_name='$ln', business_name='$bn' WHERE customerid='$cid'";          
Put:
print $query;

Instead of:
if (mysql_affected_rows() == 1) { // If it ran OK.
Try:
if ($result)

Just some options, if the UPDATE is failing then I would like to see some debugging / error message about why =)
Joe P
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
stevegingellAuthor Commented:
Hey Joe,

Still nothing happens when the page comes up, my fields are still not pulling in anything from the database. Even after i tried the die command like you said.

Also when i myself fill everything in, and hit update i get the following error.

An error occurred in script c:\program files\easyphp1-8\www\scratchdent\includes\config.inc on line 11: mysql_query(): Access denied for user 'ODBC'@'localhost' (using password: NO)An error occurred in script c:\program files\easyphp1-8\www\scratchdent\includes\config.inc on line 11: mysql_query(): A link to the server could not be establishedAccess denied for user 'ODBC'@'localhost' (using password: NO)
0
star_trekCommented:
Looks like you don't have enough permissions and cannot establish connection to the database.
Check the mysql_connect whether you have proper permissions for the user to connect to database (make sure you entered the username and password properly).
If the above doesn't work
Consider using odbc functions
http://us2.php.net/manual/en/ref.uodbc.php
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.