-1

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>

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

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

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

<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:

<?php

$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?

Alexandre
  • 474
  • 2
  • 14
user1259798
  • 171
  • 1
  • 16
  • ***Please [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php).*** [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Dec 06 '16 at 13:49
  • Can you show the array in `$chkbox`. I don't think you're getting what you expect there. – Jay Blanchard Dec 06 '16 at 13:50

1 Answers1

0

Why not just a simple for each?

$chkbox = $_POST['Cat_Ref_Array'];

foreach($chkbox as $value){
....
}
Tim S.
  • 391
  • 1
  • 5