problem passing a NULL value to MySQL using a php variable

Reply

Join Date: Sep 2008
Posts: 27
Reputation: sleign is an unknown quantity at this point 
Solved Threads: 2
sleign sleign is offline Offline
Light Poster

problem passing a NULL value to MySQL using a php variable

 
0
  #1
Dec 10th, 2008
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/>";

?>
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 791
Reputation: darkagn has a spectacular aura about darkagn has a spectacular aura about darkagn has a spectacular aura about 
Solved Threads: 109
darkagn's Avatar
darkagn darkagn is offline Offline
Master Poster

Re: problem passing a NULL value to MySQL using a php variable

 
0
  #2
Dec 10th, 2008
I would do something like this:
  1. if ( is_set( $_POST[ "newName" ]))
  2. {
  3. $displayName = $_POST[ "newName" ];
  4. // add quotes here
  5. $displayName = "'$displayName'";
  6. } // otherwise $displayName is not set ie is NULL
  7.  
  8. // ...
  9. //UPDATE OLD MESSAGE
  10. // don't insert quotes in $displayName here
  11. // because it is either NULL or a string with quotes already
  12. $query = " UPDATE messages SET NAME = $displayName, MESSAGE = '$displayMessage' WHERE ID = '$displayID'";
  13. $result = mysql_query($query);
Please note that I haven't tested this code, but I think it *should* work
There are no stupid questions, only those too stupid to ask for help.
echo is a web developer's best friend.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 94
Reputation: sikka_varun is an unknown quantity at this point 
Solved Threads: 11
sikka_varun's Avatar
sikka_varun sikka_varun is offline Offline
Junior Poster in Training

Re: problem passing a NULL value to MySQL using a php variable

 
0
  #3
Dec 11th, 2008
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..
VâRûN
---Happy to Help---
sikka_varun@yahoo.com
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the PHP Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC