我试图只从名为“Addbox”的复选框为真的行中添加值,下面的代码识别复选框何时被勾选,并且只将这些项目添加到用户的愿望清单中。但是,无论复选框是真还是假,它都会将它们与前一行的数量相加。
复选框代码是
"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)
}