Page 3 of 3
Re: How do u post a NULL to a Database?
Posted: Tue Jun 08, 2010 12:27 pm
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.
Re: How do u post a NULL to a Database?
Posted: Tue Jun 08, 2010 12:36 pm
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:
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.
Re: How do u post a NULL to a Database?
Posted: Tue Jun 08, 2010 12:44 pm
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.
Re: How do u post a NULL to a Database?
Posted: Tue Jun 08, 2010 12:45 pm
by Benjamin
echo the query, and post it, as Pytrin requested.
Re: How do u post a NULL to a Database?
Posted: Tue Jun 08, 2010 12:51 pm
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.
Re: How do u post a NULL to a Database?
Posted: Tue Jun 08, 2010 12:54 pm
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);
Re: How do u post a NULL to a Database?
Posted: Tue Jun 08, 2010 12:57 pm
by simonmlewis
Very strangely, it produces just....
1
Re: How do u post a NULL to a Database?
Posted: Tue Jun 08, 2010 12:58 pm
by Benjamin
Post the code.
Re: How do u post a NULL to a Database?
Posted: Tue Jun 08, 2010 1:06 pm
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
Re: How do u post a NULL to a Database?
Posted: Tue Jun 08, 2010 1:10 pm
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);
Re: How do u post a NULL to a Database?
Posted: Tue Jun 08, 2010 1:15 pm
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.