「Addbox」という名前のチェックボックスが true の行からのみ値を追加しようとしています。以下のコードは、チェックボックスがオンになっていることを識別し、それらの項目のみをユーザーのウィッシュリストに追加します。ただし、チェックボックスが true か false かに関係なく、前の行の数量でそれらを追加します。
チェックボックスのコードは
"input type = 'checkbox' name='Addbox[]' value='" . $row['Item_ID'] . "'
ステートメントコードが
if (isset($_POST['Add'])) {
if(isset ($_POST['Addbox'])){
//counts the size of the array addbox[] so the for loop has an end.
$count = count($_POST['Addbox']);
//starts the sql query string to added to inside the for loop.
$sql = "INSERT INTO `wishlist` (`User_ID`,`Product_ID`,`Quantity`) VALUES ";
//initiate for loop... do this until every row in the array has been looked at essentially.
for ($i=0; $i < $count; $i++) {
//get the values of this row.
$item = $_POST['Addbox'][$i];
$quantity = $_POST['quantity'][$i];
// Add the end of the sql string for each of the loops.
$sql .= "($userid, $item, $quantity)";
//This check is it is the last of the row in the array. If it isnt add a comma and space ready for the next row.
if($i !== $count-1){
$sql .= ", ";
}
}
mysql_query($sql);
// var_dump($sql)
}