Jump to content

How to insert NULL instead of 0 for integer column?


tamumech

Recommended Posts

Hi all.

 

I am inserting a lot of data into a MySQL integer column via an html form.  When a field in the form is left blank, I want the value in MySQL to be NULL, but it keeps inputting '0'.  The default value for my column is NULL.  I don't really think there is a simple way to do this on the php side (using a hidden form to set blank entries equal to NULL would require an extra page), so I was wondering if MySQL had some sort of capability.  Here's a snippet.

 

UPDATE table 
SET column1 = '$_POST[$value1]', column2 = '$_POST[$value2]'
WHERE id = '$_POST[$id]'

$_POST[$value1] = isset($_POST[$value1]) && is_numeric($_POST[$value1]) ? "'".$_POST[$value1]."'" : "NULL";
$_POST[$value1] = isset($_POST[$value1]) && is_numeric($_POST[$value1]) ? "'".$_POST[$value1]."'" : "NULL";

settype($_POST[$id], "integer");

query = "
      UPDATE table 
      SET column1 = {$_POST[$value1]}, 
            column2 = {$_POST[$value2]}
      WHERE id = {$_POST[$id]}";

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.