I'm trying to insert data from a multiselect into a database with multiple rows.
I once managed to insert them all into ONE row, each with a comma which is not really good from the database perspective. The data from the multiselect is stored as an array in a variable, which then should be filled into multiple rows in the database with a foreach loop.
$array = implode((array)$_POST['multiselectdata']);
$sql = "INSERT INTO tbl_example (column1, column2) VALUES (?, ?)";
$stmt = mysqli_stmt_init($conn);
if(!mysqli_stmt_prepare($stmt, $sql)){
....
exit();
}
else {
mysqli_stmt_bind_param($stmt, "ii", $array, $id);
mysqli_stmt_execute($stmt);
$result= mysqli_stmt_store_result($stmt);
mysqli_fetch_all($result,MYSQLI_ASSOC);
foreach($result as $row){
echo $row["column1"];
}
exit()
}
As a result in each row there should be 1 value displayed of the array in column1 and in column2 there is always going to be the same id. Currently this only inserts 1 value into the database and only 1 row