Solved

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

Posted on 2009-05-03
3
736 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Amazon Redshift 2 27
RELATED: How do I fix this error:  Warning: strpos(): Empty needle on line 122 11 32
Calculating number of days between two dates 7 32
sql_mode 1 19
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

896 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

13 Experts available now in Live!

Get 1:1 Help Now