I have written a guestbook in php using a MySQL database to store the entries. I am now writing the backend for it so that the rows in the table can be modified via a form.
The way the form works is this:
A table is displayed on a page showing the message ID, name, date, etc etc called from a table.
The form itself has 3 fields to manipulate 2 columns in the database - one for the message ID, one for the name, and one for the message itself.
The name and message fields can have data entered optionally.
My problem is that I am having a problem passing a NULL value to MySQL using a php variable in an if statement on the condition that the particular field is left blank.
It will render one of two things after the record has been updated - either the word NULL will appear or the column will be erased. It won't pass the actual value of NULL to MySQL so that the previous data in that particular column is left in tact / ignored (provided there was previous data stored in that column before the update) if the field on the form was left blank.
Can anyone help show me what I'm leaving out? My code is as follows:
<?php
echo "<h1>Edit guestbook</h1>";
//Database info code here...
//FORM VARIABLES
$displayID = trim($_POST['idNo']);
if(!$displayID)
echo "<h3>Error: No Message ID submitted, please go <a href=edit.php>back</a> and enter a value.</h3>";
$displayName = $_POST['newName'];
if($displayName == "")
$displayName = NULL;
$displayMessage = $_POST['newMessage'];
if(!$displayMessage)
echo "<h3>Error: No Message submitted, please go <a href=edit.php>back</a> and enter a message.</h3>";
//Connect to database code here...
#################################################
//UPDATE OLD MESSAGE
$query = " UPDATE messages SET NAME = '$displayName', MESSAGE = '$displayMessage' WHERE ID = '$displayID'";
$result = mysql_query($query);
#################################################
//DISPLAY NEW MESSAGE
$message = @mysql_query("SELECT * FROM `messages` WHERE `ID` =".$displayID);
if(!$message)
echo "Error: Query is invalid.";
$result = mysql_fetch_array($message);
echo "<strong>Name:</strong> <br/>";
echo $result['NAME']."<br/>";
echo "<strong>Message:</strong> <br/>";
echo $result['MESSAGE']."<br/><br/>";
################################################
echo "Guestbook updated.<br/>";
?>