BandonRandon Posted April 6, 2009 Share Posted April 6, 2009 Hello, I've been using php for a while but always been scared of arrays and loops. Well now I'm at the point were I need to learn to use them and that they will make my life easier. I currently have a form set up that has an array of text fields to allow the user to upload multiple catagories. I then loop the posted data into the mysql the only problem is that if a feild in the array was left blank I get a blank row in the SQL table. Here is the code in the form: <td class="add_categories forTableBgRight"> <input name="add_cat[]" type="text" /><br/> <input name="add_cat[]" type="text" /><br/> <input name="add_cat[]" type="text" /><br/> </td> </tr> and here is the sql statement: //check if first catagories is empty if(empty($catagories[0])){ echo "Please add at least one Catagory."; } else { foreach($catagories as $value) { $q = "INSERT INTO " . CAT_TABLE . "(cat_name) VALUES ('$value')"; $result = mysql_query($q); } Thanks, Brooke Quote Link to comment Share on other sites More sharing options...
thepip3r Posted April 6, 2009 Share Posted April 6, 2009 ... foreach($catagories as $value) { if ($value !== "") { $q = "INSERT INTO " . CAT_TABLE . "(cat_name) VALUES ('$value')"; $result = mysql_query($q); } } Quote Link to comment Share on other sites More sharing options...
BandonRandon Posted April 6, 2009 Author Share Posted April 6, 2009 Thanks, I knew it was something simple like that. I've used the if != "" before just didn't think of it here. Also this won't work if someone enters just a space into the category field. But as far as basic error checking it should be fine. Could i add a trim($values) to remove just blank spaces? Quote Link to comment Share on other sites More sharing options...
thepip3r Posted April 6, 2009 Share Posted April 6, 2009 yes...trim $value before the conditional and that will protect u from the issue u just presented. Quote Link to comment Share on other sites More sharing options...
BandonRandon Posted April 6, 2009 Author Share Posted April 6, 2009 Thanks again thepip3r! Sometimes it's the simple things we over look and just need reassurance that they'll work. It now seems to be working fine. I have marked this resolved! 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.