How do u post a NULL to a Database?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do u post a NULL to a Database?

Post by simonmlewis »

Code: Select all

$category = $category == 'none' ? 'NULL' : "'$category'";
$application = $application == 'none' ? 'NULL' : "'$application'";
$title=$_POST['title'];
$description=$_POST['description'];
$video=$_POST['video'];
$photoprimary=$_POST['photoprimary'];
$photo=$_POST['photo'];
$update=$_POST['update'];
$photodelete=$_POST['photodelete'];

$cookietype = $_COOKIE['type'];
if ($cookietype == "admin") {
  include "dbconn.php";
  
  
  if ($update == "totalupdate") {
$description=mysql_real_escape_string($_POST["description"]);
$title=mysql_real_escape_string($_POST["title"]);
$video=mysql_real_escape_string($_POST["video"]);

$query  = mysql_query ("UPDATE products SET 
category = $category,
application = $application,
title = '$title', 
description = '$description', 
video = '$video'
WHERE id = '$id'");
And to respond to Benjamin: for a Site Administrator, that's a rather unintelligent thing to say. You just don't seem to put ANY effort in, as this whole thread has been fairly straight forward to ask about, and I haved tried everything people have suggested, and fed it all back. And all you can say is "'NULL' !== NULL", which doesn't tell me where to put that, or WHY what we have is wrong from that.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: How do u post a NULL to a Database?

Post by Benjamin »

You have received an answer to your question from Jonah Bron, AbraCadaver, pytrin and I. We've each explained how this works around 3 different ways which has resulted in over 30 posts to this thread. It appears you are going to continue to post this question until the exact code you are looking for is posted. We like to help people here, but you really ought to put a little effort into this.

When you want a PHP variable to be null, you assign null to it like this:

Code: Select all

$foo = NULL;
I've already posted detailed instructions on how to transfer the NULL variable into MySQL. I've even posted a simple function which would even write the query for you. I don't mean to be harsh but I don't see where the disconnect is here.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do u post a NULL to a Database?

Post by simonmlewis »

I am pretty sure you will find that I tried this early on in the thread.
I know exactly how to assign a word or a NULL to a variable, but it did not work. And that is why I posted to this thread.

$foo = "Hello world";
$foo = NULL;

One will assign Hello World to $foo, and one will assign NULL.

BUT - when I post this to the query, it does NOT change the value of the field to NULL.

Again - this is why I posted. I only come here when it's my last resort.

UPDATED:.......
This:

Code: Select all

$category = $_POST['category'];
if ($category == "none") { $category = NULL; }
$application = $_POST['application'];
if ($application == "none") { $application = NULL; }
$title=$_POST['title'];
$description=$_POST['description'];
$video=$_POST['video'];
$photoprimary=$_POST['photoprimary'];
$photo=$_POST['photo'];
$update=$_POST['update'];
$photodelete=$_POST['photodelete'];

$description=mysql_real_escape_string($_POST["description"]);
$title=mysql_real_escape_string($_POST["title"]);
$video=mysql_real_escape_string($_POST["video"]);

$query  = mysql_query ("UPDATE products SET 
category = $category,
application = $application,
title = '$title', 
description = '$description', 
video = '$video'
WHERE id = '$id'");
... just empties the field and does not assign the field to be NULL either.
Last edited by simonmlewis on Tue Jun 08, 2010 12:47 pm, edited 1 time in total.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: How do u post a NULL to a Database?

Post by Benjamin »

echo the query, and post it, as Pytrin requested.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do u post a NULL to a Database?

Post by simonmlewis »

Don't think I have ever done that.
How do you "echo a query"? I have tried echo "$query"; but it doesn't work.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: How do u post a NULL to a Database?

Post by Benjamin »

Code: Select all

category=$_POST['category'] ==  'none' ? 'NULL'  : ("'"  . $_POST['category'] .  "'");
$application=$_POST['application'] == 'none' ? 'NULL' : ("'" . $_POST['application'] . "'");
$query = "UPDATE products SET
category = '$category',
application = '$application'........";

echo $query;

$query  = mysql_query ($query);
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do u post a NULL to a Database?

Post by simonmlewis »

Very strangely, it produces just....
1
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: How do u post a NULL to a Database?

Post by Benjamin »

Post the code.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do u post a NULL to a Database?

Post by simonmlewis »

The form is posted to this.

Code: Select all

$id=$_POST['id'];

$category=$_POST['category'] ==  'none'  ? 'NULL'  : ("'"  . $_POST['category'] .   "'");
$application=$_POST['application'] == 'none' ? 'NULL' : ("'" . $_POST['application'] . "'");
$title=$_POST['title'];
$description=$_POST['description'];
$video=$_POST['video'];
$photoprimary=$_POST['photoprimary'];
$photo=$_POST['photo'];
$update=$_POST['update'];
$photodelete=$_POST['photodelete'];

$cookietype = $_COOKIE['type'];
if ($cookietype == "admin") {
  include "dbconn.php";
  
  
  if ($update == "totalupdate") {
$description=mysql_real_escape_string($_POST["description"]);
$title=mysql_real_escape_string($_POST["title"]);
$video=mysql_real_escape_string($_POST["video"]);

$query  = mysql_query ("UPDATE products SET 
category = $category,
application = $application,
title = '$title', 
description = '$description', 
video = '$video'
WHERE id = '$id'");
echo "start here > $query <";
  echo "<div class='admincompletedbox' style='margin-top: 5px'>
		<b>This product has been updated.App: $application, Cat:$category</b>
		</div>";
  }
And this is the category part of the form:

Code: Select all

$resultcategory = mysql_query ("SELECT * FROM categories WHERE categories <> 'none' AND categories IS NOT NULL ORDER BY categories ASC");
			$nullvalue = NULL;
			while ($rowcat = mysql_fetch_object($resultcategory))
      {
			echo "<input type='radio' name='category' ";
			if ($row->category == $rowcat->categories) { echo " checked='checked'";}
			echo " value='$rowcat->categories'>$rowcat->categories";
			}
			mysql_free_result($resultcategory);
			echo "<input type='radio' name='category' value='none'>NULL
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: How do u post a NULL to a Database?

Post by Benjamin »

Ok, so the problem is that you have quotes around NULL.

To echo the query:

Code: Select all

$query = "UPDATE products SET
category = $category,
application = $application,
title = '$title',
description = '$description',
video = '$video'
WHERE id = '$id'";

echo $query;

$query  = mysql_query($query);

simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do u post a NULL to a Database?

Post by simonmlewis »

There are no quotes around the variables in the UPDATE query, and I have tried the top POST bit like this:

Code: Select all

$category=$_POST['category'] ==  'none'  ? NULL  : ("'"  . $_POST['category'] .   "'");
$application=$_POST['application'] == 'none' ? NULL : ("'" . $_POST['application'] . "'");
And it ignores whatever I enter in the form. If I choose different options from the radio buttons, it doesn't update the db.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Post Reply