Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > PHP How-To
|
PHP How-To Post your "How do I do this with PHP?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the PHP How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 14th, 2003, 10:39 PM
Authorized User
 
Join Date: Aug 2003
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to make integer to accept null value

I have a sql insert query as below:

$sql = ("INSERT INTO ticket(ticketid, type, subject, issue, phone, email, clientcode, submittedby,submitteddate,status,addedby,priority)
values(nextval('ticketidseq'), '$problemtype', '$subject', '$issue', '$phone', '$email', '$company', '$name', now(),'$status', '$p[staffid]','$priority')");

The problem is here that phone field which is integer. When there is no value typed in by the user it prints error message : pg_atoi: zero-length string.

I have tried to make the phone field to allow null value with this statement :

ALTER TABLE ticket DROP COLUMN phone;
             ALTER TABLE ticket ADD COLUMN phone integer NULL;

but no success.

Please help to solve this problem. I'm using postgresql.

Regards,
ganesh



 
Old August 15th, 2003, 05:10 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 101
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via AIM to Moharo
Default

why don't you rewrite to something like this:

<?

if(strlen($phone) == 0 || empty($phone)) {
$phone = 0; //if the field is a INTEGER
//$phone = "no phone"; if the field is a VARCHAR
}

$sql = ("INSERT INTO ticket(ticketid, type, subject, issue, phone, email, clientcode, submittedby,submitteddate,status,addedby,priority)
values(nextval('ticketidseq'), '$problemtype', '$subject', '$issue', $phone, '$email', '$company', '$name', now(),'$status', '$p[staffid]','$priority')");

?>

try changing phone field type to VARCHAR (for example notation "203-349-4343" would require VARCHAR field).
also if i am not mistaken, if you want to insert a interger variable to integer field in a database, you don't have to use single or double quotes...

:D:D:D:D


the genuine genius
 
Old August 18th, 2003, 02:39 AM
Authorized User
 
Join Date: Aug 2003
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks, I will give it a try.

 
Old August 18th, 2003, 01:48 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

another problem is that you're surrounding your integer value in quotes. When inserting into a DB, it's not necessary to quote your integer values, nor is it allowed to insert 'NULL' when you mean NULL. The first, 'NULL', is the text-string "NULL", which won't be allowed into an integer column.

If you're sticking with an integer approach, I suggest you remove the single quotes from the query around $phone. At the top of the script, do something like:

$phone = isset($_POST['phone'])? $_POST['phone'] : 'NULL';

That way, your query will either insert the phone number or NULL.


You can try the same approach with text-based columns, too. Just make sure to add the single-quotes at the testing part of the script:

$phone = isset($_POST['phone'])? "'{$_POST['phone']}'" : 'NULL';



Take care,

Nik
http://www.bigaction.org/





Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing null value to integer abhishekkashyap27 C# 2005 9 April 18th, 2008 04:11 AM
Make user entered field an integer sswingle Classic ASP Basics 4 April 10th, 2006 10:09 PM
How to set Not Null constraint to Null Columns arasu Oracle 1 August 22nd, 2005 10:09 AM
Cannot make ‘databound’ date textbox null / blank? dlsc C# 0 August 12th, 2005 12:57 AM
Can Access accept a Null from an ASP web page? kaytrina Classic ASP Databases 5 March 20th, 2004 07:11 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.