General Programming Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingGeneral Programming Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old September 3rd, 2003, 06:00 AM
fstorr fstorr is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 5 fstorr User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Post change sql query if null

Hi all

I'm using PHP to insert data into MySQL. I need to find a way of changing the SQL query depending on whether information has been entered by the user. If there is information, the SQL query would include '$longform', if there wasn't, it would be $longform (ie, without the single quotes).

The reason I need this is that I need enter NULL if there isn't data. Quoting '$longform' enters the string "NULL" rather than the value NULL.

Any help gratefully received.

Reply With Quote
  #2  
Old September 3rd, 2003, 07:18 AM
Joe4JC Joe4JC is offline
The name's Joe. Yours?
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2002
Location: Lurking in the shadows...
Posts: 147 Joe4JC User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Well, it all depends on how you are getting your variables. For example, you could do it this way:
PHP Code:
<?
$longform 
$_REQUEST['longform'];
if (empty(
$longform) && !isset($longform)) {
// Error message here
}
else {
// MySQL query here
}


Good luck!
Joe
__________________
Check out 4Life today!

Reply With Quote
  #3  
Old September 3rd, 2003, 08:16 AM
fstorr fstorr is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 5 fstorr User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Hi

Thanks

I'm getting them via a $_POST, but there's 4 or 5 different fields that are potentially able to be NULL. I am trying to find a way of not doing a whole load of if() statements with SQL queries in.

Regards

F

Reply With Quote
  #4  
Old September 3rd, 2003, 09:55 PM
Joe4JC Joe4JC is offline
The name's Joe. Yours?
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2002
Location: Lurking in the shadows...
Posts: 147 Joe4JC User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
You can use my above code to achieve that effect; you just have to test each variable by using "elseif(){}" statements.

PHP Code:
 $longform $_REQUEST['longform'];
$var2 $_POST['something'];

if (empty(
$longform) && !isset($longform)) {

// Error message here

}
elseif (empty(
$var2) && !isset($var2)) {

// Error message here

}
else {

// MySQL query here


Reply With Quote
  #5  
Old September 5th, 2003, 03:36 AM
fstorr fstorr is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 5 fstorr User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thanks

I suppose, thinking about it (this always helps!) I could build up my SQL query using your example, instead of writing error messages.

Regards

F

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingGeneral Programming Help > change sql query if null


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
Stay green...Green IT