| | |
problem passing a NULL value to MySQL using a php variable
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Sep 2008
Posts: 27
Reputation:
Solved Threads: 2
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:
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:
Please note that I haven't tested this code, but I think it *should* work
php Syntax (Toggle Plain Text)
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);
There are no stupid questions, only those too stupid to ask for help.
echo is a web developer's best friend. 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..
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..
![]() |
Similar Threads
- php.ini confusion (PHP)
Other Threads in the PHP Forum
- Previous Thread: Warning: mysql_numrows()
- Next Thread: Cant upload mp3's with this script?
| Thread Tools | Search this Thread |
# 5.2.10 alexa apache api array beginner binary broken cakephp checkbox class clean clients cms code cron curl database date directory display dissertation dropdown dynamic echo echo$_get[x]changingitintovariable... email encode error fairness file files folder form forms function functions google href htaccess html image images include indentedsubcategory insert ip javascript joomla legislation limit link local login mail memberships menu mlm multiple multipletables mysql mysqlquery newsletters oop open paypal pdf persist php problem provider query radio random recursion remote rss script search server sessions sms sockets source space spam sql syntax system table tutorial update upload url validator variable video web youtube





