Solved

How to insert null value into MySQL database when using PHP variables

Posted on 2009-05-03
3
723 Views
Last Modified: 2012-06-21
It sure looks like what I'm doing is correct, but MySQL is giving me a syntax error and I cannot figure out why.

I have a field called "staples". It's defined as DECIMAL 10,2. It is set so that a NULL value is allowed. I submit a form and populate the field with the value of 100. All is well. I go back and change the value on the form to essentially erase it - I backspace over it. The code to process the value is this:

$value = $_POST['staples'];

$query = "UPDATE tblSurveys SET staples = {$value} WHERE ansID = 43";

The query throws a syntax error.
Note that if I have entered a value like 100 into the field, the query works fine.

What am I missing?

I put some logic in to check to see if $_POST['staples'] is set, and it is - even when I've backspaced over the field. So what does post return if the field is empty? I tried to add this check:

if ($value == "") { $value = NULL; } but that doesn't help. Still get the syntax error.
0
Comment
Question by:alicia1234
3 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 24290735
if (isset($_POST['staples']) && ($_POST['staples'] != "") )
{
  $value = $_POST['staples'];
}
else
{
  $value = "NULL";
}

$query = "UPDATE tblSurveys SET staples = {$value} WHERE ansID = 43";
0
 
LVL 28

Expert Comment

by:gamebits
ID: 24290736
try

$query = "UPDATE tblSurveys SET staples = '$value' WHERE ansID = 43";
0
 

Author Comment

by:alicia1234
ID: 24290826
gamebits stopped the error but put a 0.00 in the database field; what I wanted in the field was NULL. angelIII's solution both fixed the syntax error and put the NULL value in the field

Thanks!

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article discusses how to create an extensible mechanism for linked drop downs.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now