Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2009-05-03
3
Medium Priority
?
778 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 2000 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

This article discusses how to implement server side field validation and display customized error messages to the client.
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
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.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

721 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