i have manage to insert my checkbox array data into database. the problem is when i check multiple checkbox only the first data in array is inserted in database. i need when i check multiple checkbox, all the data i have check insert into database.can someone help me?
this is my php code:
<?php
include ('connect.php');
$cat = implode(',', $_POST['check']);
if(isset($_POST['submit']))
{
for($i=0; $i<count($cat);$i++)
{
$p_id =$_GET ['sitter'];
$price = $_POST ['price'];
$pickup_date =$_POST ['pickup_date'];
$dropoff_date =$_POST ['dropoff_date'];
$numdays = $_POST ['numdays'];
$total =$_POST ['test'];
$sql2 = "INSERT INTO cat_sitter(sitter_fk,cat_fk, price, date_in, date_out,total_day, total)VALUES ('$p_id','" . $cat[$i] . "','$price','$pickup_date','$dropoff_date','$numdays', '$total')" or die ("Error inserting data into table");
if ($conn->query($sql2) === TRUE) {
echo "<script language='javascript'>alert('Succesfully Book.')
window.location.replace(\"book_page.php\");
</script>";
}else{
echo "error: " . $sql2 . "<br>" . $conn->error;
}
}
}
?>
this is my html code:
<?php
include ('connect.php');
$sql = "SELECT cat_id,name,gender,health_status,neutered,breed,color,age FROM cat WHERE owner_fk = '$id'";
$result3 = mysqli_query($conn,$sql);
$row6 = mysqli_fetch_array($result3);
$cato = $row6['cat_id'];
$result = $conn-> query($sql);
if ($result-> num_rows > 0) {
while ($row6 = $result-> fetch_assoc()) {
//table code//
echo "<td>" ."<input type='checkbox' name= 'check[]' value='$cato'". "</td>";
echo "</tr>";
}
echo "</table>";
}
else{
echo "0 result";
}
$conn-> close();
?>