I'm trying to insert multiple rows with only one INSERT INTO statement using the implode function. I use "keluhanpg" and "mkpg" for multiple values. and "mkpg" uses implode because I use multiple selected data and I implode with comma. like this picture: enter image description here
<?php
session_start();
include_once('koneksi.php');
if(isset($_POST['add'])){
$kodepg = $_POST['kodepg'];
$noformpg = $_POST['noformpg'];
$waktupg = $_POST['waktupg'];
$deptpg = $_POST['deptpg'];
$lokasipg = $_POST['lokasipg'];
$merkpg = $_POST['merkpg'];
$tahunpg = $_POST['tahunpg'];
$op = isset($_POST['oppg']) && is_array($_POST['oppg']) ? $_POST['oppg'] : [];
$oppg = implode(", ", $op);
$keluhanpg = isset($_POST['keluhanpg']) && is_array($_POST['keluhanpg']) ? $_POST['keluhanpg'] : [];
$mk = isset($_POST['mkpg']) && is_array($_POST['mkpg']) ? $_POST['mkpg'] : [];
$mkpg = implode(", ", $mk);
$shiftpg = $_POST['shiftpg'];
$statuspg = $_POST['statuspg'];
foreach ($_POST['keluhanpg'] as $key => $value){
$sql= "INSERT INTO tb_pg_cs (kode_pg,noform_pg,waktu_pg,dept_pg,lokasi_pg,merk_pg,tahun_pg,op_pg,keluhan_pg,mk_pg,shift_pg,status_pg) VALUES ('" . $kodepg . "','".$noformpg . "','".$waktupg . "','".$deptpg . "','".$lokasipg . "','".$merkpg . "','".$tahunpg . "','".$oppg . "','".$_POST['keluhanpg'][$key] . "','".$mkpg . "','".$shiftpg . "','".$statuspg . "')";
$sql1 = mysqli_query($connect, $sql);
}
if($sql1){
$_SESSION['success'] = 'Data added successfully';
}else{
$_SESSION['error'] = 'Something went wrong while adding';
}
}
header("location: ".$base_url."index.php?page=pengajuan");
?>
the input successfully exited but the "mkpg" data was merged into one. like this: enter image description here
how to write code to process input correctly?