I have copy & pasted all the code as is and it is working fine using the records DB and players table exactly as illustrated. I have tried to change the data to match a different DB & table and now on the edit.php page I am simply getting "Error! Is there a way I can debug where this error is coming from? The view.php page is working OK with my DB information. I am a newbie when it comes to PHP & MySql so many thanks in advance for any help you can offer.
Here is the edit.php code I have:
<?php
/*
EDIT.PHP
Allows user to edit specific entry in database
*/
// creates the edit record form
// since this form is used multiple times in this file, I have made it a function that is easily reusable
function renderForm($agency_num, $agency_name, $agency_contact, $error)
{
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Edit Agency Information</title>
</head>
<body>
<?php
// if there are any errors, display them
if ($error != '')
{
echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>';
}
?>
<form action="" method="post">
<input type="hidden" name="agency_num" value="<?php echo $agency_num; ?>"/>
<div>
<p><strong>Agency Num:</strong> <?php echo $agency_num; ?></p>
<strong>Agency Name: *</strong> <input type="text" name="agency_name" value="<?php echo $agency_name; ?>"/><br/>
<strong>Contact Name: *</strong> <input type="text" name="agency_contact" value="<?php echo $agency_contact; ?>"/><br/>
<p>* Required</p>
<input type="submit" name="submit" value="Submit">
</div>
</form>
</body>
</html>
<?php
}
// connect to the database
include('connect-db.php');
// check if the form has been submitted. If it has, process the form and save it to the database
if (isset($_POST['submit']))
{
// confirm that the 'id' value is a valid integer before getting the form data
if (is_numeric($_POST['agency_num']))
{
// get form data, making sure it is valid
$agency_num = $_POST['agency_num'];
$agency_name = mysql_real_escape_string(htmlspecialchars($_POST['agency_name']));
$agency_contact = mysql_real_escape_string(htmlspecialchars($_POST['agency_contact']));
// check that firstname/lastname fields are both filled in
if ($agency_name == '' || $agency_contact == '')
{
// generate error message
$error = 'ERROR: Please fill in all required fields!';
//error, display form
renderForm($agency_num, $agency_name, $agency_contact, $error);
}
else
{
// save the data to the database
mysql_query("UPDATE contact_data SET agency_name='$agency_name', agency_contact='$agency_contact' WHERE agency_num='$agency_num'")
or die(mysql_error());
// once saved, redirect back to the view page
header("Location: view1.php");
}
}
else
{
// if the 'id' isn't valid, display an error
echo 'ID Error!';
}
}
else
// if the form hasn't been submitted, get the data from the db and display the form
{
// get the 'id' value from the URL (if it exists), making sure that it is valid (checing that it is numeric/larger than 0)
if (isset($_GET['agency_num']) && is_numeric($_GET['agency_num']) && $_GET['agency_num'] > 0)
{
// query db
$id = $_GET['agency_num'];
$result = mysql_query("SELECT * FROM contact_data WHERE agency_num=$id")
or die(mysql_error());
$row = mysql_fetch_array($result);
// check that the 'id' matches up with a row in the databse
if($row)
{
// get data from db
$agency_name = $row['agency_name'];
$agency_contact = $row['agency_contact'];
// show form
renderForm($agency_num, $agency_name, $agency_contact, '');
}
else
// if no match, display result
{
echo "No results!";
}
}
else
// if the 'id' in the URL isn't valid, or if there is no 'id' value, display an error
{
echo 'Error!';
}
}
?>