Solved

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

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

Get Database Help Now w/ Support & Database Audit

Keeping your database environment tuned, optimized and high-performance is key to achieving business goals. If your database goes down, so does your business. Percona experts have a long history of helping enterprises ensure their databases are running smoothly.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Why isn't my DIV and Form centering? 1 31
Can't find an open element in HTML 5 55
How do I add date to text file name 15 30
Php variable to be sent back 3 35
This article discusses how to create an extensible mechanism for linked drop downs.
This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

710 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