Siggles Posted June 30, 2008 Share Posted June 30, 2008 Hi, I have a form and if the user does not fill out one of the fields it is still changing that field to a 0 and I want it to stay as NULL. Here is my code chopped down (the php bit)... switch($_POST['opt2']) { case "editprediction": //SQL FOR EDITING PREDICTION IN DBASE $score=$_POST['score']; if (isset($_POST['score'])){ mysql_query("UPDATE predictions SET score = '$score' WHERE predictionid = '$j'"); } break; } ?> I understand that isset only checks that a varible exists which is why it is setting it to 0. There may be times in the form however when I actually want the user to put 0 in the form field. So I only want it to do the query when the form field has something in it (0 or any number) but not when it is left blank. Any suggestions? Link to comment https://forums.phpfreaks.com/topic/112556-solved-isset-or-empty-or-what/ Share on other sites More sharing options...
MasterACE14 Posted June 30, 2008 Share Posted June 30, 2008 try this: <?php switch($_POST['opt2']) { case "editprediction": //SQL FOR EDITING PREDICTION IN DBASE $score=$_POST['score']; if (isset($_POST['score']) && !empty($_POST['score']) && is_numeric($_POST['score'])){ mysql_query("UPDATE predictions SET score = '$score' WHERE predictionid = '$j'"); } break; } ?> Regards ACE Link to comment https://forums.phpfreaks.com/topic/112556-solved-isset-or-empty-or-what/#findComment-578031 Share on other sites More sharing options...
kenrbnsn Posted June 30, 2008 Share Posted June 30, 2008 The problem with using "empty()" on a field is that it returns "true" if the field contains a "0" (zero). I always use <?php if (strlen(trim(stripslashes($_POST['fld']))) > 0) { ?> to see if a field is not empty. Ken Link to comment https://forums.phpfreaks.com/topic/112556-solved-isset-or-empty-or-what/#findComment-578033 Share on other sites More sharing options...
MasterACE14 Posted June 30, 2008 Share Posted June 30, 2008 i'll keep that in mind, thanks. Link to comment https://forums.phpfreaks.com/topic/112556-solved-isset-or-empty-or-what/#findComment-578035 Share on other sites More sharing options...
Siggles Posted June 30, 2008 Author Share Posted June 30, 2008 try this: <?php switch($_POST['opt2']) { case "editprediction": //SQL FOR EDITING PREDICTION IN DBASE $score=$_POST['score']; if (isset($_POST['score']) && !empty($_POST['score']) && is_numeric($_POST['score'])){ mysql_query("UPDATE predictions SET score = '$score' WHERE predictionid = '$j'"); } break; } ?> Regards ACE Thank you for your help, only it wont accept a 0 in the form and leaves it null. It works in that it leaves field null thogh when field is empty. halfway there! :-) Link to comment https://forums.phpfreaks.com/topic/112556-solved-isset-or-empty-or-what/#findComment-578039 Share on other sites More sharing options...
Siggles Posted June 30, 2008 Author Share Posted June 30, 2008 The problem with using "empty()" on a field is that it returns "true" if the field contains a "0" (zero). I always use <?php if (strlen(trim(stripslashes($_POST['fld']))) > 0) { ?> to see if a field is not empty. Ken Great, that works! Thanks :-) Link to comment https://forums.phpfreaks.com/topic/112556-solved-isset-or-empty-or-what/#findComment-578044 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.