Jump to content

Help with foreach on checkbox array


Fallen_angel

Recommended Posts

Hi ,

 

I am having a bit of an issue getting a foreach statement i have made to work , essentially what this for each statement is suposed to do is write a row to a database for each one of the check boxes selected here is the code

 

<Form enctype='multipart/form-data' action="process_add_banner.php" method="post">
<input type='hidden' name='send' value='1' />
  <tr><td>Name:</td><td><input type="Text" name="name"><br></td></tr>
            
  <tr><td>Locations</td><td>
                <table width="100%">  
                <tr ><td><INPUT TYPE=CHECKBOX NAME="location[]"  VALUE="None">None</td>
                <td><INPUT TYPE=CHECKBOX NAME="location[]" VALUE="Forums">Fourms</td>
                 <td><INPUT TYPE=CHECKBOX NAME="location[]"  VALUE="Extras">Extras</td>
                 <td><INPUT TYPE=CHECKBOX NAME="location[]"  VALUE="Portal">Portal</td>
                   </tr>
	      </table>
            </td><tr> 
   <tr><td></td><td><input type="Submit" name="submit" value="Submit Information"></td><tr> 
  </form>

 

and the processing code is

 

 

$name=$_POST['name'];
$group[]=$_POST['location[]'] ;

foreach ($group as $loc) {
  $sql = "INSERT INTO database (name,group) VALUES ('$name','$loc')";
  $result = mysql_query($sql);
  }

 

Can someone please tell me where i am going wrong ?  I don't get any syntax errors it just doesn't do the insert . Thanx allot for your assistance

Link to comment
https://forums.phpfreaks.com/topic/50575-help-with-foreach-on-checkbox-array/
Share on other sites

I relised that I made an error with my example I gave i put

 

$group[]=$_POST['location[]'] ;

 

instead of just

 

$group=$_POST['location'] ;

 

 

after doing some more testign i have discovered the issue is when i add a certain feild of my database to the sql script , even if I  vet the value manually it still makes the whoel thing fail so the issue is in part of my sql script that I'm sure i can knuckle out

 

thanx anyways  hehe 

Ok figured it out

 

for some reason my script didn';t liek the collom with the name group no matter what i put in there , however as soon as i changed group to grp it worked so go figure  lol

 

could someone possibly explain that cause i have the script workign now but i'm kinda scratching my head as to what i did wrong before

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.