2

This is going to be an easy one for you but I'm a noob so I need help.

Here is my code, for whatever reason the else statement isn't executing. If I change the NULL value for a $variable = NULL it works, but enters an empty string into my database rather than the NULL value. Can anyone help with why?

if (isset($_POST['agreeto']))
{
    $enter_feedback = "INSERT INTO feedback" . //(initial, surname, country_id, date, friend_score, return_score, service_score, comments)
        " VALUES('" . $initial. "', '" . $lastname . "', '" . $country_id . "', NOW(),  '" . $friend . "', '" . $return . "', '" . $service . "', '" . $_POST['comments'] . "')";
}else
{
    $enter_feedback = "INSERT INTO feedback" . //(initial, surname, country_id, date, friend_score, return_score, service_score, comments)
    " VALUES('" . $initial. "', '" . $lastname . "', '" . $country_id . "', NOW(),  '" . $friend . "', '" . $return . "', '" . $service . "', " . NULL . ")" or die("couldn't execute my query");
}
6
  • 1
    warning your code is vulnerable to sql injection attacks. use a parameratized query, which will fix your problem as well. Commented Sep 18, 2013 at 13:34
  • Does your column allow NULL? Commented Sep 18, 2013 at 13:34
  • if your column default value is NULL - just don't insert anything into comments column, and remove it from insert statement Commented Sep 18, 2013 at 13:35
  • Why are you omitting the column list from your actual statement? Commented Sep 18, 2013 at 13:38
  • You have NULL as a variable. It should be inside the SQL statement. Remove the dots and quotes around it. ". NULL ." to just NULL Commented Sep 18, 2013 at 13:41

4 Answers 4

4

MySQL requires the string=NULL and you are passing it the value of the php NULL constant.

if (isset($_POST['agreeto']))
                {
                    $enter_feedback = "INSERT INTO feedback" . //(initial, surname, country_id, date, friend_score, return_score, service_score, comments)
                    " VALUES('" . $initial. "', '" . $lastname . "', '" . $country_id . "', NOW(),  '" . $friend . "', '" . $return . "', '" . $service . "', '" . $_POST['comments'] . "')";
                }else
                    {
                        $enter_feedback = "INSERT INTO feedback" . //(initial, surname, country_id, date, friend_score, return_score, service_score, comments)
                        " VALUES('" . $initial. "', '" . $lastname . "', '" . $country_id . "', NOW(),  '" . $friend . "', '" . $return . "', '" . $service . "',NULL)" or die("couldn't execute my query");
                    }
Sign up to request clarification or add additional context in comments.

Comments

0

This should work as expected.

Try set the column to int, if this does not work.

INSERT INTO table (val1, val2) VALUES('String Value', NULL);

or set default column value to NULL.

Comments

0

A NULL value can not be enclosed in 'apostrophes'

Think about how you will get the SQL:

INSERT INTO table_name VALUES ('null','');?

The database may understand as a string

1 Comment

Where is the OP enclosing it in apostrophes?
0

Just replace the 'NULL' value with NULL without the quotes.

  $enter_feedback = "INSERT INTO feedback" . //(initial, surname, country_id, date, friend_score, return_score, service_score, comments)
                " VALUES('" . $initial. "', '" . $lastname . "', '" . $country_id . "', NOW(),  '" . $friend . "', '" . $return . "', '" . $service . "', NULL) " or die("couldn't execute my query");

Once you do that and save the record you will see NULL in the database column for that field when you browse via PHPMYADMIN. Please make sure that the field in question "comments" allows NULL.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.