Hi there,

I've been trying to figure out why my update script wont commit changes to the database. I'm building a simple call tracking web app for our technicians. Currently the view, add, and delete functions of the app work fine. The trouble I'm running into is when I want to update a record. I want the fields of the form to be auto filled from the current record. On the view page you enter the work order number and hit an update button. This is the update page:

<?php
$username="xxxxxxx";
$password="xxxxxxx";
$database="xxxxxxx";
$con=mysql_connect(localhost,$username,$password);
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db($database, $con);

$work_order=$_GET['work_order'];
$query="SELECT * FROM call_tracker WHERE work_order='$work_order'";
$result=mysql_query($query);
$num=mysql_numrows($result);
$i=0;

while ($i < $num) {
$location=mysql_result($result,$i,"location");
$customer_name=mysql_result($result,$i,"customer_name");
$contact_number=mysql_result($result,$i,"contact_number");
$date_last_called=mysql_result($result,$i,"date_last_called");
$call_frequency=mysql_result($result,$i,"call_frequency");
$call_today=mysql_result($result,$i,"call_today");
$notes=mysql_result($result,$i,"notes");
++$i;
}
?>
<table border="0">
<form action="update.php" method="get">
<tr><td>Work Order:</td><td><? echo $work_order; ?></td></tr>
<tr><td>Location: </td><td><input type="text" name="location" value="<? echo $location; ?>"></td></tr>
<tr><td>Customer Name: </td><td><input type="text" name="customer_name" value="<? echo $customer_name; ?>"></td></tr>
<tr><td>Contact Number: </td><td><input type="text" name="contact_number" value="<? echo $contact_number; ?>"></td></tr>
<tr><td>Date Last Called: </td><td><input type="text" name="date_last_called" value="<? echo $date_last_called; ?>"></td></tr>
<tr><td>Call Frequency: </td><td><input type="text" name="call_frequency" value="<? echo $call_frequency; ?>"></td></tr>
<tr><td>Call Today: </td><td><input type="text" name="call_today" value="<? echo $call_today; ?>"></td></tr>
<tr><td>Notes: </td><td><input type="text" name="notes" value="<? echo $notes; ?>"></td></tr>
<tr><td></br><input type="button" onclick="window.location.href='view.php'" value="View Call Tracker"></input></td><td align="right"></br><input type="Submit" value="Update"></td></tr>
</form>
</table>

Once the fields are modified it gets sent onto this script to write the changes to the database:

<?php
$location=$_GET['location'];
$work_order=$_GET['work_order'];
$customer_name=$_GET['customer_name'];
$contact_number=$_GET['contact_number'];
$date_last_called=$_GET['date_last_called'];
$call_frequency=$_GET['call_frequency'];
$call_today=$_GET['call_today'];
$notes=$_GET['notes'];

$username="xxxxxxx";
$password="xxxxxxx";
$database="xxxxxxx";
$con=mysql_connect(localhost,$username,$password);
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db("$database", $con);

$query="UPDATE call_tracker SET location='$location', customer_name='$customer_name', contact_number='$contact_number', date_last_called='$date_last_called', call_frequency='$call_frequency', call_today='$call_today', notes='$notes' WHERE work_order='$work_order'";
mysql_query($query);

echo "Record Updated";
?>
<br>
<form>
<input type="button" onclick="window.location.href='view.php'" value="View Call Tracker"></input>
</form>

I'm very new to PHP and SQL, but I've been using several different tutorials and I have a simpler version of this that just uses 2 fields and it works properly, I just can't seem to figure out why this doesn't work. Any help would be greatly appreciated.

Change line 22 to mysql_query($query) or exit(mysql_error()); and see what you get.

I actually got some other help and found what the problem was.

Because I was just displaying the value for work_order instead of keeping it in an input field, that data wasn't being forwarded to the second script, and therefore couldn't write the data. I had to add this to the form to retain the $work_order.

<input name="work_order" type="hidden" value="<?php echo $work_order; ?>" />
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.