I have this processing page to either add or update a fishing report depending if there is a record in the database:
include("../inc/stuff.php");
// error list array
$errorList = array();
$count = 0;
// validate form fields
if (!$lake_id) { $errorList[$count] = "Invalid entry: Lake Name"; $count++; }
if (!$auth_id) { $errorList[$count] = "Invalid entry: Author Id"; $count++; }
if (!$fish_report) { $errorList[$count] = "Invalid entry: Fish report"; $count++; }
if (sizeof($errorList) == 0)
{
// open DB connection
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");
// select DB
mysql_select_db($db) or die ("Unable to select database!");
if(checkdate($month, $day, $year))
{
$date_for_query = $year.'-'.$month.'-'.$day;
}
else
{
die("Invalid Date Choice, please <a href='add_freshwater_report.php'>go back</a> and pick a date");
}
Everything works fine until this point:
$query = "SELECT lake_id, report_id FROM freshwater_report WHERE lake_id = '$lake_id' AND report_id = report_id";
$result = mysql_query($query) or die ("Error in query: $query. " . mysql_error());
if(!$result){
// execute insert query
$query = "INSERT INTO freshwater_report(lake_id, author_id, date_fished, fish_report) VALUES ('$lake_id', '$auth_id', '$date_for_query', '$fish_report')";
$result = mysql_query($query) or die ("Error in query: $query. " . mysql_error());
// if the query was successful print:
echo "The Fishing report was added successfully. <a href='index.php'>Go back to the main menu</a>.";
}
If there is no record in the DB the portion above gets skipped and my update string is echoed to the brower: The Fishing report was updated successfully. Go back to the main menu
else{
$query = "UPDATE freshwater_report SET lake_id = '$lake_id', author_id = '$auth_id', date_fished = '$date_for_query', fish_report = '$fish_report'
WHERE lake_id = '$lake_id' AND report_id = report_id";
$result = mysql_query($query) or die ("Error in query: $query. " . mysql_error());
echo "The Fishing report was updated successfully. <a href='index.php'>Go back to the main menu</a>.";
}
mysql_close($connection);
}
else
{
// if there were errors found print them out
echo "The following errors were encountered: <br>";
echo "<ul>";
for ($x=0; $x<sizeof($errorList); $x++)
{
echo "<li>$errorList[$x]";
}
echo "</ul>";
}
BUT if there is a record in the database the update works. I've checked both the sql statments and they work the problem is in the logic I think.
Thanks for any help -E