Here's some useful chunks of one of my update forms - it works - only got it going this week, so I'm sharing the things i've learnt this week!
This chunk checks whether anything has been posted from the previous page (ie, clicked "edit")
if (!$_POST['submit'])
{
//check for record id
if ((!isset($_GET['id']) || trim ($_GET['id']) == ''))
{
die('Missing Record ID!');
}
//Generate & Execute query
// Get the PHP file containing the DbConnector class
require_once('../sypphp/sypcms/DbConnector.php');
// Create an instance of DbConnector
$connector = new DbConnector();
$id = $_GET['id'];
$query = "SELECT company, jobtitle, jobdesc, contact, closingdate from jobs where id = '$id'";
$result = $connector->query($query) or die("Error in query $query.".mysql_error());
//IF A RESULT IS RETURNED
if (mysql_num_rows($result)>0){ //this if statement is closed after the form, which I see you already have OK!)
//TURN IT INTO AN ARRAY
$row = mysql_fetch_assoc($result);
?>
<!-- PRINT FORM WITH JOB VALUES -->
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<input type="hidden" name ="id" value="<?php echo $id; ?>">
} //closes the if statement
//no result returned - return error message
else
{
echo 'Sorry, cannot find that job in the database';
} //end else
Up to this point you are telling it what to do when NOTHING has been submitted
Here comes the bit where you HAVE submitted something, covered by the following else statement. Of course, feel free to amend my syntax according to Roger's instructions
}else{
$company = $_POST['company'];
$jobtitle = $_POST['jobtitle'];
$jobdesc = $_POST['jobdesc'];
$contact = $_POST['contact'];
$closingdate = $_POST['closingdate'];
//validate the fields
if (trim($_POST['company']) == '') {$errorlist[] = 'Invalid entry: company';}
if (trim($_POST['jobtitle']) == '') {$errorlist[] = 'Invalid entry: jobtitle';}
if (trim($_POST['jobdesc']) == '') {$errorlist[] = 'Invalid entry: jobdesc';}
if (trim($_POST['contact']) == '') {$errorlist[] = 'Invalid entry: contact';}
if (trim($_POST['closingdate']) == '') {$errorlist[] = 'Invalid entry: closingdate';}
//check for errors, if none found:
if (sizeof($errorlist) == 0)
{
$connector = new DbConnector();
$insertQuery="UPDATE jobs SET company='$company', jobtitle='$jobtitle', jobdesc='$jobdesc', contact='$contact', closingdate='$closepost' WHERE id = '$id' ";
$result = $connector->query($insertQuery) or die ("Error in query: $insertQuery.".mysql_error());
//print result
echo 'Update Successful<br><a href=jobsadmin.php>Click here to return to the jobs admin menu</a>';
}else{
//errors found: show them in a list
echo 'The following errors have been encountered:<br>';
for ($x=0; $x<sizeof($errorlist); $x++)
{
echo $errorlist [$x];
}
}
}
?>
This is the link (having performed a select query) from the start page
<a href="jobsfull.php?id=<?php echo $row['id'];?>"><?php echo $row['jobtitle'];?></a>