Link to home
Start Free TrialLog in
Avatar of palmtreeinfotech
palmtreeinfotechFlag for United States of America

asked on

Dreamweaver Update Record PHP not working

Seems this is hit and miss.  I manually made a form and tied the ID from the previous page (in a recordset dynamic table).  When the user clicks on the ID it is a link to the update_record.php page.  Everything seems fine but the record is not updating for some reason.  No error codes or anything displays, just the form does not work.  The form has dynamic form fields on it so the user can update and see what is currently in the DB.  I'm not much of a coder so don't know if I'm missing something in my code but it even goes to the update_success.php page after updating, but nothing has changed in the DB.  Any ideas?
Avatar of palmtreeinfotech
palmtreeinfotech
Flag of United States of America image

ASKER


<?php require_once('../Connections/connect01.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) {
  $updateSQL = sprintf("UPDATE tblInfo SET Modified=%s, Bus_Name=%s, Contact_Name=%s, Address=%s, City=%s, `State`=%s, Zip=%s, Phone=%s, Fax=%s, Email=%s, Website=%s, Active=%s, Bus_Month=%s, Bus_Year=%s, Comments=%s WHERE ID=%s",
                       GetSQLValueString($_POST['datemodified'], "date"),
                       GetSQLValueString($_POST['busname'], "text"),
                       GetSQLValueString($_POST['contactname'], "text"),
                       GetSQLValueString($_POST['address'], "text"),
                       GetSQLValueString($_POST['city'], "text"),
                       GetSQLValueString($_POST['state'], "text"),
                       GetSQLValueString($_POST['zip'], "text"),
                       GetSQLValueString($_POST['phone'], "text"),
                       GetSQLValueString($_POST['fax'], "text"),
                       GetSQLValueString($_POST['email'], "text"),
                       GetSQLValueString($_POST['website'], "text"),
                       GetSQLValueString($_POST['status'], "int"),
                       GetSQLValueString($_POST['bommonth'], "text"),
                       GetSQLValueString($_POST['bomyear'], "text"),
                       GetSQLValueString($_POST['comments'], "text"),
                       GetSQLValueString($_POST['id'], "int"));

  mysql_select_db($database_connect01, $connect01);
  $Result1 = mysql_query($updateSQL, $connect01) or die(mysql_error());

  $updateGoTo = "update_success.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";
    $updateGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $updateGoTo));
}

$colname_Recordset1 = "-1";
if (isset($_GET['ID'])) {
  $colname_Recordset1 = $_GET['ID'];
}
mysql_select_db($database_connect01, $connect01);
$query_Recordset1 = sprintf("SELECT * FROM tblInfo WHERE ID = %s", GetSQLValueString($colname_Recordset1, "int"));
$Recordset1 = mysql_query($query_Recordset1, $connect01) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Admin Login for Members PACC Site</title>
<style type="text/css">
body {
	background-image: url(images/bluestrip_bg.png);
}
</style>
</head>

<body>
<p>You are updating this business.  The database will not update until you have &quot;submitted&quot; your changes below.</p>
<p>You will receive a confirmation page once updated correctly.</p>
<form action="<?php echo $editFormAction; ?>" id="form1" name="form1" method="POST">
  <table width="100%" border="1" cellpadding="2" cellspacing="2">
    <tr>
      <td width="22%">ID:</td>
      <td width="78%"><label for="id"></label>
      <input name="id" type="text" disabled="disabled" id="id" value="<?php echo $row_Recordset1['ID']; ?>" /></td>
    </tr>
    <tr>
      <td>Date Added:</td>
      <td><label for="dateadded"></label>
      <input name="dateadded" type="text" disabled="disabled" id="dateadded" value="<?php echo $row_Recordset1['Date_Added']; ?>" size="45" /></td>
    </tr>
    <tr>
      <td>Date Last Modified:</td>
      <td><label for="datemodified"></label>
      <input name="datemodified" type="text" disabled="disabled" id="datemodified" value="<?php date("F j, Y, g:i a")?>" size="45" /></td>
    </tr>
    <tr>
      <td>Business Name:</td>
      <td><label for="busname"></label>
      <input value="<?php echo $row_Recordset1['Bus_Name']; ?>" name="busname" type="text" id="busname" size="45" /></td>
    </tr>
    <tr>
      <td>Contact Name:</td>
      <td><input value="<?php echo $row_Recordset1['Contact_Name']; ?>" name="contactname" type="text" id="contactname" size="45" /></td>
    </tr>
    <tr>
      <td>Address:</td>
      <td><input value="<?php echo $row_Recordset1['Address']; ?>" name="address" type="text" id="address" size="45" /></td>
    </tr>
    <tr>
      <td>City:</td>
      <td><input value="<?php echo $row_Recordset1['City']; ?>" name="city" type="text" id="city" size="45" /></td>
    </tr>
    <tr>
      <td>State:</td>
      <td><input value="<?php echo $row_Recordset1['State']; ?>" name="state" type="text" id="state" size="10" /></td>
    </tr>
    <tr>
      <td>Zip:</td>
      <td><input value="<?php echo $row_Recordset1['Zip']; ?>" name="zip" type="text" id="zip" size="15" /></td>
    </tr>
    <tr>
      <td>Phone:</td>
      <td><input value="<?php echo $row_Recordset1['Phone']; ?>" name="phone" type="text" id="phone" size="45" /></td>
    </tr>
    <tr>
      <td>Fax:</td>
      <td><input value="<?php echo $row_Recordset1['Fax']; ?>" name="fax" type="text" id="fax" size="45" /></td>
    </tr>
    <tr>
      <td>Email Address:</td>
      <td><input value="<?php echo $row_Recordset1['Email']; ?>" name="email" type="text" id="email" size="45" /></td>
    </tr>
    <tr>
      <td>Website:</td>
      <td><input value="<?php echo $row_Recordset1['Website']; ?>" name="website" type="text" id="website" size="45" /></td>
    </tr>
    <tr>
      <td>Active?</td>
      <td><label for="status"></label>
        <select name="status" id="status">
          <option value="1" <?php if (!(strcmp(1, $row_Recordset1['Active']))) {echo "selected=\"selected\"";} ?>>Active</option>
          <option value="2" <?php if (!(strcmp(2, $row_Recordset1['Active']))) {echo "selected=\"selected\"";} ?>>Inactive</option>
      </select></td>
    </tr>
    <tr>
      <td>Business of the Month Month:</td>
      <td><label for="bommonth"></label>
        <select name="bommonth" id="bommonth">
          <option value="01" <?php if (!(strcmp(01, $row_Recordset1['Bus_Month']))) {echo "selected=\"selected\"";} ?>>January</option>
          <option value="02" <?php if (!(strcmp(02, $row_Recordset1['Bus_Month']))) {echo "selected=\"selected\"";} ?>>February</option>
          <option value="03" <?php if (!(strcmp(03, $row_Recordset1['Bus_Month']))) {echo "selected=\"selected\"";} ?>>March</option>
          <option value="04" <?php if (!(strcmp(04, $row_Recordset1['Bus_Month']))) {echo "selected=\"selected\"";} ?>>April</option>
          <option value="05" <?php if (!(strcmp(05, $row_Recordset1['Bus_Month']))) {echo "selected=\"selected\"";} ?>>May</option>
          <option value="06" <?php if (!(strcmp(06, $row_Recordset1['Bus_Month']))) {echo "selected=\"selected\"";} ?>>June</option>
          <option value="07" <?php if (!(strcmp(07, $row_Recordset1['Bus_Month']))) {echo "selected=\"selected\"";} ?>>July</option>
          <option value="08" <?php if (!(strcmp(08, $row_Recordset1['Bus_Month']))) {echo "selected=\"selected\"";} ?>>August</option>
          <option value="09" <?php if (!(strcmp(09, $row_Recordset1['Bus_Month']))) {echo "selected=\"selected\"";} ?>>September</option>
          <option value="10" <?php if (!(strcmp(10, $row_Recordset1['Bus_Month']))) {echo "selected=\"selected\"";} ?>>October</option>
          <option value="11" <?php if (!(strcmp(11, $row_Recordset1['Bus_Month']))) {echo "selected=\"selected\"";} ?>>November</option>
          <option value="12" <?php if (!(strcmp(12, $row_Recordset1['Bus_Month']))) {echo "selected=\"selected\"";} ?>>December</option>
          <option value="" <?php if (!(strcmp("", $row_Recordset1['Bus_Month']))) {echo "selected=\"selected\"";} ?>>Not selected...</option>
        </select></td>
    </tr>
    <tr>
      <td>Business of the Month Year:</td>
      <td><label for="bomyear"></label>
        <select name="bomyear" id="bomyear">
          <option value="2010" <?php if (!(strcmp(2010, $row_Recordset1['Bus_Year']))) {echo "selected=\"selected\"";} ?>>2010</option>
          <option value="2011" <?php if (!(strcmp(2011, $row_Recordset1['Bus_Year']))) {echo "selected=\"selected\"";} ?>>2011</option>
          <option value="2012" <?php if (!(strcmp(2012, $row_Recordset1['Bus_Year']))) {echo "selected=\"selected\"";} ?>>2012</option>
          <option value="" <?php if (!(strcmp("", $row_Recordset1['Bus_Year']))) {echo "selected=\"selected\"";} ?>>Not selected...</option>
        </select></td>
    </tr>
    <tr>
      <td>Comments:</td>
      <td><label for="comments"></label>
      <textarea name="comments" id="comments" cols="70" rows="10"><?php echo $row_Recordset1['Comments']; ?></textarea></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td><input type="submit" name="button" id="button" value="Submit" /></td>
    </tr>
  </table>
  <input type="hidden" name="MM_update" value="form1" />
</form>
<p>&nbsp;</p>
<p>&nbsp;</p>
</body>
</html>
<?php
mysql_free_result($Recordset1);
?>

Open in new window

As a side note, I'm trying to update the [modified] field in the DB when the user hits the submit button.  I have it has a hidden field called "modified" which is the same field as my database and I have the value set at <?php date("y:m:d h:i:s")?>

Would this update in the database based on the time they hit the submit button to the [modified] field each time?  Or should I be posting another question for this?
Avatar of Jason C. Levine
Hi again, palmtreeinfotech,

Your recordset is looking for a URL parameter named ID:

$colname_Recordset1 = "-1";
if (isset($_GET['ID'])) {
  $colname_Recordset1 = $_GET['ID'];
}


Is the URL something.php?ID=xxx or something.php?id=xxx

Because if it is the latter, this won't work as PHP is case sensitive.

>> Would this update in the database based on the time they hit the submit button to the [modified]
>> field each time?

Yes, but easier to set the field to a TIMESTAMP and then set it to ON UPDATE CURRENT TIMESTAMP.  So each time the row is updated, it gets the latest system time and date.  
Hi Jason!  You're the resident expert I see!

I changed my text field name to capital "ID" and that didn't make a difference.  It's actually linking correctly from the previous page based on the ID of the row in the table:

update_record.php?ID=1

The actual URL Param id is "ID" on both page.

What I did on the timestamp is tell Dreamweaver to update my hidden field (hiddenmodified) to update to my database field of [modified] and set the hidden field to "now()" but not sure that will work in MySQL though.

Everything seems to be working except the update is still not working??
>> Hi Jason!  You're the resident expert I see!

For Dreamweaver, more or less.  I also tend to be home at nights because having a small child really ruins ones ability to be out on Saturday nights :)

1. When you go to the update page, are the form fields prepopulated?  That will handle the passing of the $_GET array

2. If the form fields are prepopulated, the error is most likely here:

<input name="id" type="text" disabled="disabled" id="id" value="<?php echo $row_Recordset1['ID']; ?>" />

disabled="disabled" may prevent the form from submitting the data altogether.  Better to do:

<input name="id" type="hidden" id="id" value="<?php echo $row_Recordset1['ID']; ?>" />

or, if it must be visible:

<input name="id" type="text" readdonly id="id" value="<?php echo $row_Recordset1['ID']; ?>" />

I don't think setting to NOW() will work as expected.  You really should define it like so:

ALTER TABLE `tblInfo` CHANGE `datemodified` `datemodified` TIMESTAMP( 23 ) ON UPDATE CURRENT_TIMESTAMP NOT NULL
Hey Jason cool I understand!  I took off the "disabled" and to no avail no change.

I'm not much of a coder.  In any case, I changed my hiddenfield of [modified] to timestamp().  I will see if that works but I won't know until I can get the page to actually update!

Good suggestions.  Anymore ideas?  Everything seems to be working great but just no updates to the database.  Weird.
Can I get a link to the page for testing?
Sure!

http://peculiarchamber.com/admin2341231/index.php
Login:  admin
Pass:  MDDBel9jpU

It's okay to post on here..I just have the session variable set so you cannot access the page without logging in first.  I'm just testing anyway.

After login, mainmenu.php, click on an ID, takes you to update page. Change something then hit Submit, go back to main menu and record not updated!  Frustrating.  Thank you so much for looking at it!
ASKER CERTIFIED SOLUTION
Avatar of Jason C. Levine
Jason C. Levine
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Jason, thank you so much and sorry for the late reply.  I changed the variable like in your example and it still didn't update. :(  I'm wondering if I should delete the update record behaviour and start all over just to be safe?  But I shouldn't have to do that I wouldn't think.  I checked all the other case id=ID and all of them are capitalized in the update query.
Jason, I was playing around and I took off the "disabled" after changing the case id to ID and it seems to be working!  How weird is that?  It should work either way in my opinion...thanks for all your help!
Took off the "disabled" tags and it seems to be working.  Thanks a lot for your help and I hope you and the baby are doing well!
>> I took off the "disabled" after changing the case id to ID and it seems to be working!  How weird is that?

Why would you think it's weird?  Both things were wrong :)

It appears you changed the HTML field name to ID when my first post talked about checking the URL (which turns out to have been a bit of a red herring).  The disabled can prevent the form from submitting the data but this appears to be a browser to browser thing.

>> I hope you and the baby are doing well!

Not a baby.  4.5...he just craps out at an early hour :)