0

I have this <input type="text" name="Fax" id="faxbox" maxlength="9" value="<?php echo $row['Fax'] ?>" />

I want to leave it blank on submit. On Data Base, that column (as int) is set to allow nulls too.

But when i submit, my empty/blank field is turn to 0 (zero) by MySql. I want that appear NULL instead 0.

What should I do? I try if(empty($_POST['Fax'])){$Fax=NULL;} but wont work.

Sergio
  • 28,539
  • 11
  • 85
  • 132
user1148875
  • 449
  • 4
  • 11
  • 24

2 Answers2

1

You need to actually use a string null, e.g.

$Fax = 'NULL';

A php null will be converted into an empty string if you use it in a string context anywhere.


followup:

if you have:

$Fax = null;
$sql = "INSERT INTO .... VALUES ($Fax)";

you end up with

INSERT INTO ... VALUES ()

because the PHP null was converted to an empty string. But if you have

$Fax = 'null'; // a string whose contents is the word 'null'

then the query statement will actually be

INSERT INTO ... VALUES (null)

because the value of the string was the word null, which then will be treated as an sql null.

Marc B
  • 356,200
  • 43
  • 426
  • 500
  • My Fax column on DBase is set as INT. Im sorry if i wasnt clear, but what I want, is to save a few data from a form in DB and one of that entrys is empty, but mysql save it as 0. I need that Mysql save it as NULL – user1148875 Aug 06 '12 at 02:15
1

In order to insert NULL value in MySQL using php, you need to do it like this:

INSERT INTO table (field,field2) VALUES (NULL,3)

in your case, try this:

$Fax = !empty($Fax) ? "'$Fax'" : "NULL"; 
INSERT INTO tableName (Fax) VALUES ($Fax);

more on this link

Community
  • 1
  • 1
John Woo
  • 258,903
  • 69
  • 498
  • 492