Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > PHP/MySQL > PHP How-To
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old August 14th, 2003, 10:39 PM
Authorized User
 
Join Date: Aug 2003
Location: , , .
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



Reply With Quote
  #2 (permalink)  
Old August 15th, 2003, 05:10 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , USA.
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
Reply With Quote
  #3 (permalink)  
Old August 18th, 2003, 02:39 AM
Authorized User
 
Join Date: Aug 2003
Location: , , .
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks, I will give it a try.

Reply With Quote
  #4 (permalink)  
Old August 18th, 2003, 01:48 PM
Friend of Wrox
Points: 2,570, Level: 21
Points: 2,570, Level: 21 Points: 2,570, Level: 21 Points: 2,570, Level: 21
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: San Diego, CA, USA
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/
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

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 06:11 PM



All times are GMT -4. The time now is 02:11 PM.


Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.