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

 

Link to comment
Share on other sites

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? 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.