Jump to content

[SOLVED] saving an array into database but only non-null values


BandonRandon

Recommended Posts

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

 

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? 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.