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? Quote Link to comment 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 Quote Link to comment 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 Quote Link to comment Share on other sites More sharing options...
MasterACE14 Posted June 30, 2008 Share Posted June 30, 2008 i'll keep that in mind, thanks. Quote Link to comment 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! :-) Quote Link to comment 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 :-) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.