need help
can anyone correct my code for inserting data into two tables using foreach based on the checkbox that I checked
<?php
$con = mysqli_connect('localhost', 'root', '', 'a.karat');
if(isset($_POST['simpan']))
{
$poNo = $_POST['poNo'];
$prNo = $_POST['prNo'];
$cabang = $_POST['cabang'];
$supplier = $_POST['supplier'];
$tanggal_po = $_POST['tanggal_po'];
$tanggal_kirim = $_POST['tanggal_kirim'];
$note = $_POST['note'];
$ppn = $_POST['ppn'];
$grandtotal = $_POST['grandtotal'];
$query = mysqli_query($con,"INSERT INTO po (poNo,prNo,cabang,supplier,tanggal_po,tanggal_kirim,note,ppn,grandtotal)VALUES('$poNo', '$prNo','$cabang', '$supplier', '$tanggal_po', '$tanggal_kirim', '$note', '$ppn','$grandtotal') mysqli_connect_error()");
$check=$_POST['check'];
foreach($check as $i)
{
$prcode=$_POST['productCode'.$i];
$prname=$_POST['productName'.$i];
$qty=$_POST['qty'.$i];
$harga=$_POST['harga'.$i];
$diskon=$_POST['diskon'.$i];
$total=$_POST['total'.$i];
$query = mysqli_query($con,"insert into detail_po (poNo,productCode,productName,qty,harga,diskon,total) value ('$poNo', '$prcode', '$prname', '$qty', '$harga','$diskon','$total',)mysqli_connect_error()");
}
if($query)
{
?>
<script>
alert("success");
</script>
<?php
}
}
?>
what am I missing, there is no error messages
thanks in advance
Remove mysqli_connect_error()
from your query. This is causing the query to fail. Also your insert statement is incorrect. Replace value
with values
. There is extra comma in your second query.
Proper way to check for any errors in query is
$query = mysqli_query($con,"insert into detail_po
(poNo,productCode,productName,qty,harga,diskon,total)
values ('$poNo', '$prcode', '$prname', '$qty', '$harga','$diskon','$total')") or die(mysqli_error($con));
I think you will get an error with this line
$query = mysqli_query($con,"insert into detail_po (poNo,productCode,productName,qty,harga,diskon,total) value ('$poNo', '$prcode', '$prname', '$qty', '$harga','$diskon','$total',)mysqli_connect_error()");
Notice your comma after the '$total',? Remove that and try to run again.
First mysqli_connect_error()
function is used to check MySQL connection is established or not and it should be at top, where you have database connection. Remove it from INSERT queries
.
Once suggestion
Rather than using second query in FOREACH
loop you can create bulk INSERT
query
$query = mysqli_query($con,"INSERT INTO po (poNo,prNo,cabang,supplier,tanggal_po,tanggal_kirim,note,ppn,grandtotal)VALUES('".$poNo."', '".$prNo."','".$cabang."', '".$supplier."', '".$tanggal_po."', '".$tanggal_kirim."', '".$note."', '".$ppn."','".$grandtotal."') mysqli_connect_error()");
$query = mysqli_query($con,"insert into detail_po (poNo,productCode,productName,qty,harga,diskon,total) value ('".$poNo."', '".$prcode."', '".$prname."', '".$qty."', '".$harga."','".$diskon."','".$total."',)mysqli_connect_error()");