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/>";

?>

I would do something like this:

if ( is_set( $_POST[ "newName" ]))
{
  $displayName = $_POST[ "newName" ];
  // add quotes here
  $displayName = "'$displayName'";
} // otherwise $displayName is not set ie is NULL

//  ...
//UPDATE OLD MESSAGE
// don't insert quotes in $displayName here
// because it is either NULL or a string with quotes already
$query = " UPDATE messages SET NAME = $displayName, MESSAGE = '$displayMessage' WHERE ID = '$displayID'";
$result = mysql_query($query);

Please note that I haven't tested this code, but I think it *should* work ;)

Hiii...
Check your database field...
And check if the <displayName> field in the database allows you to enter NULL...

You can check it from phpmyadmin... and in the table, check the option for this field... Allow Nulls...
If it is no, then your database is not alllowing this field to be NULL.. either NULL (in words) or Blank value (it is same in this case)....
Change the value to Yes for Allow Nulls..

Hope this helps..

This article has been dead for over six months. Start a new discussion instead.