vendredi 26 avril 2019

insert loop checkbox data in database in php

i am trying to insert data in the database but only those data which are checked. Its working fine with update query but don't know why its is not working for insert query. Suppose, when i check 10 options and click on 'Move All' button, it is working fine but when i replace the update query with insert query, it is inserting blank data in database.

Here is my full code:

<?php 
include("config.php");
if(isset($_POST['save'])){
  $checkbox = $_POST['check'];
  for($i=0;$i<count($checkbox);$i++){
  $del_id = $checkbox[$i]; 

  $firm_name_sms=$_POST['firm_name_sms'];
  $position_sms=$_POST['position_sms'];
  $city_sms=$_POST['city_sms'];
  $catagory_name_sms=$_POST['catagory_name_sms'];
  $PrimaryMobile_no_sms=$_POST['PrimaryMobile_no_sms'];

  $bulksms="INSERT INTO bulk_sms (firm_name_sms, position_sms, city_sms, catagory_name_sms, PrimaryMobile_no_sms) VALUES('$firm_name_sms', '$position_sms', '$city_sms', '$catagory_name_sms', '$PrimaryMobile_no_sms')";
  $smsquery=mysqli_query($conn, $bulksms);
}
}


$sql="SELECT * FROM inventory_details where status ='0' AND role='0' limit 0,100";  
$query=mysqli_query($conn, $sql);

?>


<form method="post" action="">
<div class="container">
  <?php
$i=0;
$today_date=date("Y-m-d H:i:s", time()); 
    while($row=mysqli_fetch_assoc($query)){
    $id = $row['id'];
    $inventorybackground = $row['inventorybackground'];
    $inventorycolor = $row['inventorycolor'];
    $firm_name = $row['firm_name'];
    $position = $row['position'];
    $city = $row['city'];
    $catagory_name = $row['catagory_name'];
    $PrimaryMobile_no = $row['PrimaryMobile_no'];
  ?>

    <div class="row" style="background-color: <?php echo $inventorybackground; ?> !important; color: <?php echo $inventorycolor; ?>; padding-top: 10px;">
        <div class="col-md-1 inventory-data">
          <input type="text" class="form-control" name="id_sms" value="<?php echo $id;?>" disabled>
        </div>

        <div class="col-md-3 inventory-data">
          <input type="text" class="form-control" name="firm_name_sms" value="<?php echo $firm_name;?>" disabled>
        </div>

        <div class="col-md-1 inventory-data">
          <input type="text" class="form-control" name="position_sms" value="<?php echo $position;?>" disabled>
        </div>

        <div class="col-md-2 inventory-data">
          <input type="text" class="form-control" name="city_sms" value="<?php echo $city;?>" disabled>
        </div>

        <div class="col-md-2 inventory-data">
          <input type="text" class="form-control" name="catagory_name_sms" value="<?php echo $catagory_name;?>" disabled>
        </div>

        <div class="col-md-2 inventory-data">
          <input type="text" class="form-control" name="PrimaryMobile_no_sms" value="<?php echo $PrimaryMobile_no;?>" disabled>
        </div>

        <div class="col-md-1 inventory-data"><input type="checkbox" id="checkItem" name="check[]" value="<?php echo $row["id"]; ?>"></div>
    </div>

<?php
$i++;
}
?>

</div>

<p align="center"><button type="submit" class="btn btn-success" name="save" style="margin-bottom: 150px;">Move All</button></p>
</form>
<script>
$("#checkAl").click(function () {
$('input:checkbox').not(this).prop('checked', this.checked);
});
</script>

when i replace the insert query i.e

  $firm_name_sms=$_POST['firm_name_sms'];
  $position_sms=$_POST['position_sms'];
  $city_sms=$_POST['city_sms'];
  $catagory_name_sms=$_POST['catagory_name_sms'];
  $PrimaryMobile_no_sms=$_POST['PrimaryMobile_no_sms'];

  $bulksms="INSERT INTO bulk_sms (firm_name_sms, position_sms, city_sms, catagory_name_sms, PrimaryMobile_no_sms) VALUES('$firm_name_sms', '$position_sms', '$city_sms', '$catagory_name_sms', '$PrimaryMobile_no_sms')";
  $smsquery=mysqli_query($conn, $bulksms);

with update query i.e.

$bulksms="UPDATE inventory_details SET role='3' WHERE id='".$del_id."'";
  $smsquery=mysqli_query($conn, $bulksms);

the it is working fine. Please help me out, Thank you




Aucun commentaire:

Enregistrer un commentaire