Solved

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

Posted on 2009-05-03
3
763 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 143

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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.
When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
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 look for a specific file type in a local or remote server directory using PHP.

763 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