leony Posted January 28, 2009 Share Posted January 28, 2009 Hello, I have an HTML form and it collects customer address, tel no, post code etc...However all these fields are not required as in the following: <select name="contact"> <option value="0" selected="selected">Select</option> <option value="1">Yes</option> <option value="2">No</option> </select> If the customer does not select anything, I want to insert a NULL value to the database, because, I cannot get it right, instead of NULL value, I have to use a ZERO (0) value. INSERT INTO table (first_name, last_name, house_no, street, city, county, postcode, email, phone, contact insert_date) VALUES ('$first_name', '$last_name', '$house_name', '$street', '$city', '$county', '$postcode', '$email', '$telephone', $contact, NOW())"; There are lots of 0s in the database and somehow I want to insert NULL value instead of zero. How can it be done? Thanks... Link to comment https://forums.phpfreaks.com/topic/142814-mysql-and-null-values/ Share on other sites More sharing options...
rhodesa Posted January 28, 2009 Share Posted January 28, 2009 if(empty($contact)) $contact = 'NULL'; $sql = "INSERT INTO table (first_name, last_name, house_no, street, city, county, postcode, email, phone, contact insert_date) VALUES ('$first_name', '$last_name', '$house_name', '$street', '$city', '$county', '$postcode', '$email', '$telephone', $contact, NOW())"; Link to comment https://forums.phpfreaks.com/topic/142814-mysql-and-null-values/#findComment-748608 Share on other sites More sharing options...
fenway Posted January 28, 2009 Share Posted January 28, 2009 Assuming you made the column NULLable. Link to comment https://forums.phpfreaks.com/topic/142814-mysql-and-null-values/#findComment-748619 Share on other sites More sharing options...
leony Posted January 29, 2009 Author Share Posted January 29, 2009 if(empty($contact)) $contact = 'NULL'; $sql = "INSERT INTO table (first_name, last_name, house_no, street, city, county, postcode, email, phone, contact insert_date) VALUES ('$first_name', '$last_name', '$house_name', '$street', '$city', '$county', '$postcode', '$email', '$telephone', $contact, NOW())"; Hi, For your sample code, I have noticed that as long as the contact value is NULL, the query is executed , however if it is not, I get an error message. The column is NULL meanwhile. Thanks... Link to comment https://forums.phpfreaks.com/topic/142814-mysql-and-null-values/#findComment-749611 Share on other sites More sharing options...
rhodesa Posted January 29, 2009 Share Posted January 29, 2009 is $contact a number? try this instead: $contact = empty($contact) ? 'NULL' : "'".mysql_real_escape_string($contact)."'"; $sql = "INSERT INTO table (first_name, last_name, house_no, street, city, county, postcode, email, phone, contact insert_date) VALUES ('$first_name', '$last_name', '$house_name', '$street', '$city', '$county', '$postcode', '$email', '$telephone', $contact, NOW())"; edit: and if that doesn't work...when the query fails what is the error outputted by mysql_error() ? Link to comment https://forums.phpfreaks.com/topic/142814-mysql-and-null-values/#findComment-749616 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.