I need help in inserting the value of a datatable to mysql I used ajax and php here is my code
<?php
$query="SELECT products_code,brand_name,products_name,category_name,subcategory_name,supplier_company,products_status,products_acquisitionprice,products_sellingprice,products_ceiling,products_reorderpoint,products_flooring,products_quantity FROM tblproduct,tblsubcategory,tblcategory,tblsupplier,tblbrand WHERE tblcategory.category_id=tblproduct.category_id && tblsubcategory.subcategory_id=tblproduct.subcategory_id && tblsupplier.supplier_id=tblproduct.supplier_id && tblbrand.brand_id=tblproduct.brand_id order by products_name";
$result=mysqli_query($conn,$query);
?>
<div class="table-responsive container-fluid">
<table id="product_data" class="table table-striped table-bordered">
<thead>
<tr>
<td>Product ID</td>
<td>Product Name</td>
<td>Product Brand</td>
<td>Product Category</td>
<td>Product Sub-Category</td>
<td>Product Supplier</td>
<td>Product Status</td>
<td>Product Acquisition Price</td>
<td>Product Ceiling</td>
<td>Product Re-order Point</td>
<td>Product Flooring</td>
<td>Product Stock</td>
<td>Order On Queue</td>
<td>Total Order Cost</td>
</tr>
</thead>
<?php
$tablecount=0;
while ($row=mysqli_fetch_array($result)) {
$ooq=$row['products_ceiling']-$row['products_quantity'];
$totalcost=$row['products_acquisitionprice']*$ooq;
?>
<tr>
<td class="products_code"> <?php echo$row['products_code']?> </td>
<td class="products_name"> <?php echo$row['products_name']?> </td>
<td class="brand_name"> <?php echo$row['brand_name']?> </td>
<td class="category_name"> <?php echo$row['category_name']?> </td>
<td class="subcategory_name"> <?php echo$row['subcategory_name']?> </td>
<td class="supplier_company"> <?php echo$row['supplier_company']?> </td>
<td class="products_status"> <?php echo$row['products_status']?> </td>
<td class="products_acquisitionprice"> <?php echo$row['products_acquisitionprice']?> </td>
<td class="products_ceiling"> <?php echo$row['products_ceiling']?> </td>
<td class="products_reorderpoint"> <?php echo$row['products_reorderpoint']?> </td>
<td class="products_flooring"> <?php echo$row['products_flooring']?> </td>
<td class="products_quantity"> <?php echo$row['products_quantity']?> </td>
<td class="ooq"> <?php echo$ooq?> </td>
<td class="totalcost"> <?php echo$totalcost?> </td>
</tr>
<?php
$tablecount++;
}
?>
</table>
</div>
<div class="card-footer">
<button type="button" class="btn btn-info btn-sm" name="Order" id="Order" style="background-color: green;">
Order Itemss
</button>
<button type="reset" class="btn btn-danger btn-sm" style="background-color: maroon;">
Clear
</button>
</div>
<div class="container-fluid">
<script>
$(document).ready(function(){
$('#Order').click(function(){
var products_code = [];
var supplier_company = [];
var ooq = [];
var totalcost = [];
$('.products_code').each(function(){
products_code.push($(this).text());
});`enter code here`
$('.supplier_company').each(function(){
supplier_company.push($(this).text());
});
$('.ooq').each(function(){
ooq.push($(this).text());
});
$('.totalcost').each(function(){
totalcost.push($(this).text());
});
$.ajax({
url:"Order.php",
method:"POST",
data:{products_code:products_code, supplier_company:supplier_company, ooq:ooq, totalcost:totalcost},
success:function(data){
alert(data);
$("td[contentEditable='true']").text("");
for(var i=2; i<= count; i++)
{
$('tr#'+i+'').remove();
}
fetch_item_data();
}
});
});
});
</script>
and here is my php:
<?php
//insert.php
include('session.php');
if(isset($_POST["products_code"]))
{
$products_code = $_POST["products_code"];
$supplier_company = $_POST["supplier_company"];
$ooq = $_POST["ooq"];
$totalcost = $_POST["totalcost"];
$sql = '';
for($count = 0; $count<count($products_code); $count++)
{
$products_code_clean = mysqli_escape_string($conn, $products_code[$count]);
$supplier_company_clean = mysqli_escape_string($conn, $supplier_company[$count]);
$ooq_clean = mysqli_escape_string($conn, $ooq[$count]);
$totalcost_clean = mysqli_escape_string($conn, $totalcost[$count]);
if($products_code_clean != '' && $supplier_company_clean != '' && $totalcost_clean != '' && $ooq_clean != '')
{
$sql.="INSERT INTO tblorder VALUES(DEFAULT,$products_code_clean,$ooq_clean,(SELECT supplier_id from tblsupplier where supplier_company='$supplier_company_clean'),now(),$totalcost_clean,'Pending Arrival')";
}
}
if($sql != '')
{
if(mysqli_multi_query($conn, $sql))
{
echo 'Item Data Inserted';
}
else
{
$error="";
echo $conn->$error;
echo $products_code_clean;
echo $ooq_clean;
echo $supplier_company_clean;
echo $totalcost_clean;
}
}
else
{
echo 'All Fields are Required';
}
}
?>