mardi 6 décembre 2016

Inserting multiple checkboxes to MySQL PHP: only adding one entry

I have the following checkboxes (excerpt from a form):

<input type="checkbox" name="Cat_Ref_Array[]" value="2" />
      <label for="Cat_Ref_Array[]">Category 2</label>

            <br />
<input type="checkbox" name="Cat_Ref_Array[]" value="3" />
      <label for="Cat_Ref_Array[]">Category 3</label>

            <br />
<input type="checkbox" name="Cat_Ref_Array[]" value="5" />
      <label for="Cat_Ref_Array[]">Category 5</label>

            <br />
<input type="checkbox" name="Cat_Ref_Array[]" value="7" />
      <label for="Cat_Ref_Array[]">Category 7</label>

            <br />
<input type="checkbox" name="Cat_Ref_Array[]" value="13" />
      <label for="Cat_Ref_Array[]">Category 13</label>

...and the following PHP to insert it to a table:

     $chkbox = $_POST['Cat_Ref_Array'];
$i = 0;
 While($i<sizeof($chkbox)){
  $insertSQL = sprintf("INSERT INTO table (Data, `Call`, Cat_Ref) VALUES (%s, %s, %s)",
                       GetSQLValueString($_POST['Data'], "text"),
                       GetSQLValueString($_POST['Call'], "text"),
                       GetSQLValueString($chkbox[$i], "int"));

  $Result = mysql_query($insertSQL, $site) or die(mysql_error());
    $i++;
}

...but it only inserts one record each time, no matter how many checkboxes are ticked?

How come?

Aucun commentaire:

Enregistrer un commentaire