I have the following table:
id | message_id | recevier_id
1 | 8 | 2
2 | 9 | 5
3 | 14 | 4
I am sending data to a PHP file to add to the above table. My data is as follows:
messageid = "2" receiver id = "5,6,34"
I am trying to add multiple rows with different "receiver id", so the outcome of the above query should result in :
id | message_id | recevier_id
1 | 8 | 2
2 | 9 | 5
3 | 14 | 4
4 | 2 | 5
5 | 2 | 6
6 | 2 | 34
My current MySQL query looks like this:
<?php
$inputvalues = $_POST;
$errors = false;
$result = false;
session_start();
include_once '../../../includes/database.php';
$uid = $_SESSION['usr_id'];
$sendername = $_SESSION['firstnames'].' '.$_SESSION['surname'];
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
foreach ($inputvalues as $key => $value) {
if(isset($value) && !empty($value)) {
$inputvalues[$key] = $mysqli->real_escape_string( $value );
} else {
$errors[$key] = 'The field '.$key.' is empty';
}
}
if( !$errors ) {
$mysqli->query("
INSERT INTO `message_receiver_map` (`message_id`, `receiver_id`) VALUES ('".$messageid."', '".$inputvalues['receiverid']."');
");
$returnResult = "Success";
}
mysqli_close($mysqli);
echo json_encode(['result' => $returnResult, 'errors' => $errors]);
exit;
?>
How can I achieve this?