views:

46

answers:

2

I have an insert:

$sql = 'INSERT into orders SET 
fax_int_prefix = "'.$_SESSION['fax_int_prefix'].'", 
fax_prefix = "'.$_SESSION['fax_prefix'].'", 
fax_first = "'.$_SESSION['fax_first'].'", 
fax_last = "'.$_SESSION['fax_last'];

The value of all of these fields is that they are blank right before the insert. Here is an example of one of them I echo'd out just before the insert:

$_SESSION[fax_prefix] =

For some reason it inserts the integer 0, instead of a blank value or null, as it should. Why is it inserting 0's instead of blank spaces into my DB?

+2  A: 

Check the structure of your table. It's possible that it is not of type char or varchar (or that it is and has a default value set to '0'). To do this you can use phpmyadmin, SQLyog or other MySql admin programs.

Edit: If you want to store integers, but have the option of no value then make sure the type is nullable. i.e.: column_name INT(n) DEFAULT NULL

JYelton
Well, the field is set to integer. I want the information they provide in those fields to be an integer or blank(nothing). Do I really need to change the datatype just to do what I want to do?
zeckdude
If you want them to be blank instead of 0, make sure you `allow null` for the field.
JYelton
They do allow for Null. The Null box is checked in phpmyadmin and the default is set to NULL. How can it still be saving the '0' value if I have those null options set?
zeckdude
If you are passing an empty string you need to test for that and insert null instead. So your query would read (briefly): `if ($_SESSION['fax_int_prefix'] == '') $query = 'INSERT INTO orders SET fax_int_prefix NULL' // ...`
JYelton
Just a further note: Blank strings translate to zero when inserting into an int column in sql. You have to specify null because it's not the same as zero. (A good example is a temperature column. 0°C is freezing. Null is "we don't know the value.")
JYelton
A: 

Check the schema of your database. Most likely they are integer fields or such.

SHOW FIELDS FROM orders;

or

SHOW CREATE TABLE orders;

might help. Additionally just printing out a variable is no good check, rather use var_dump().

johannes
What's wrong with using integer fields? I want the information they provide in those fields to be an integer or blank(nothing).
zeckdude