jeudi 16 mai 2019

why only 1 checkbox values of array inserted into table

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? Pleasee!

php code 1:

 <?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();

 ?>

php code 2 :

<?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;
     }
     }
   }
 ?>

i really need insert all the data i checked insert into database.




Aucun commentaire:

Enregistrer un commentaire