0

I have variable set to NULL that im trying to insert into a database but for some reason they keep getting submitted as '0'. Im positive that column im trying to inset into allows NULL and that the default is set to to NULL. Heres my code:

$insert = NULL;
$query = mysql_query("INSERT INTO `table1` (column1) VALUES ('$insert')") or die(mysql_error());
Jonah Katz
  • 5,230
  • 16
  • 67
  • 90
  • 2
    Have you tried using a prepared statement? See [http://stackoverflow.com/questions/5329542/php-mysql-insert-null-values][1] [1]: http://stackoverflow.com/questions/5329542/php-mysql-insert-null-values – jle Jul 09 '12 at 15:28
  • Try to put default value as NULL in database table. When you create a new record column1 will be NULL automatically. Or try to set $insert variable as null $insert = null. And the question is - why you need it as NULL? – comprex Jul 09 '12 at 15:27

3 Answers3

5

Warning:

Please, don't use mysql_* functions for new code. They are no longer maintained and the community has begun the deprecation process. Instead you should learn about prepared statements and use either PDO or MySQLi.

IF you want it to be NULL (and you really really still want to use mysqli_*) in the database you can do the following:

$insert = NULL;
$query = mysql_query("INSERT INTO `table1` (column1) VALUES ("
                         .(($insert===NULL)?
                                 "NULL":
                                 "'".mysql_real_escape_string($insert)."'").
                     ")") or die(mysql_error());

But this could lead to nefarious SQL injection and is not recommended.

See Bobby Tables


So: all in all you should be using prepared statements.

You can use MySQLi like so:

        $dbHandle = new mysqli(...);
        $query = "INSERT INTO `table1` (column1) VALUES (?)";
        $statement = $dbHandle->prepare($query);
        if($statement){
            $statement->bind_param('s', $insert);
            if(!$statement->execute()){
                echo "Statement insert error: {$statement->error}";
            }
            $statement->close();
        }
        else {
            echo "Insert error: {$dbHandle->error}";
        }
Naftali
  • 144,921
  • 39
  • 244
  • 303
1

Try this for static query:

$query = mysql_query("INSERT INTO `table1` (column1) VALUES (NULL)")  or die(mysql_error());

Using Variable :

$insert= NULL;
$insert = ($insert===NULL)? 'NULL' : "'$insert'";
mysql_query("INSERT INTO `table1` (column1) VALUES ($insert)") or die(mysql_error());
Mahesh Meniya
  • 2,627
  • 3
  • 18
  • 17
0

Try without the quotes;

$query = mysql_query("INSERT INTO `table1` (`column1`) VALUES (".$insert.")") or die(mysql_error()); 

The query should be;

INSERT INTO table1 (column1) VALUES (NULL);

craig1231
  • 3,769
  • 4
  • 31
  • 34
  • `You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '., '24')' at line 1` – Jonah Katz Jul 09 '12 at 15:33
  • Well it works on my server... so you must be typing something incorrectly – craig1231 Jul 09 '12 at 15:52