I want to insert into my mysql database only those rows that have a checked checkbox at the end.
Since i have several tables and table data cells that have the same name, then my data is stored in arrays (product_name[] etc.):
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
<?php
while($res = mysqli_fetch_array($result)) {
echo "<table id='tbl'>
<thead><th name='category[]'>".$res['category_name']."</th></thead>
<tbody>
<tr>
<td><input name='product_name[]' id='name' type='text'></td>
<td><input name='quantity[]' type='text' value='1'></td>
<td><input type='checkbox' name='check[]'></td>
</tr>
</tbody>";
}
?>
</table>
<div class="bottom-btn">
<button id="submit" type="submit">Lisa märgitud külmkappi</button>
</div>
</form>
Do i have to store the checkboxes in array as well (check[])?
My php code for the insert:
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$product_name = $_POST['product_name'];
$quantity = $_POST['quantity'];
// $check = $_POST['check'];
foreach($product_name as $i=>$product_name)
{if(!empty($product_name)){
$sql = "INSERT INTO products (id, product_name, category, quantity, expiration_date, barcode) VALUES ('','$product_name', '', '$quantity[$i]', '', '')";
if($stmt = mysqli_prepare($conn, $sql)){
mysqli_stmt_bind_param($stmt, "ss",$param_product_name, $param_quantity);
$param_product_name = $product_name;
$param_quantity=$quantity;
if(mysqli_stmt_execute($stmt)){
header("location: myFridge.php");
} else{
echo "Something went wrong. Please try again later.";
}
mysqli_stmt_close($stmt);
}
}
}
mysqli_close($conn);
}
So far i have tried this: if(!empty($_POST['check']))
and this if(isset($_POST['check'])
, but those did not work.
How to check if checkbox is checked before inserting data to database?
If You need any more information, then let me know.
Thank You!
Aucun commentaire:
Enregistrer un commentaire