I have this code:
<?php
session_start();
$page_name = 'Send Emails';
require_once('header.php');
if(!empty($_POST['send'])){
$id_mail= "";
foreach ($_POST['selected'] as $key => $value) {
echo "$key - $value <br>";
$id_mail .= "$value, ";
}
$id_mail= substr($id_mail, 0, -2);
}
if(!empty($_POST['Send2'])){
$mail= mysqli_query($conn, "SELECT mail FROM students WHERE student_id IN($_POST[id_mail])");
echo "SELECT mail FROM students WHERE student_id IN($_POST[id_mail]) ";
while ($row_mail = mysqli_fetch_assoc($mail)) {
echo "$row_mail[mail] ";
$to = $row_mail['mail'];
$subject = $_POST['head'];
$message = $_POST['body'];
$headers = 'From: [email protected]' . "\r\n" .
'Please do not reply to this message' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
}
}
?>
<form name='mail2' method='post' action='send_email.php'><br/>
<input type="hidden" name="id_mail" value="<?php echo $id_mail; ?>">
<br/>
<input type='text' name='head' placeholder='header'>
<br/>
<br/>
<textarea name='body' rows="4" cols="50" placeholder='body'></textarea><br/>
<br/>
<input class='btn btn-danger'type='submit' name="Send2" value='Send'>
</form>
It's purpose is to read the ID of a selected row from a table of students, print it so I can see it's selected correct from the previous page, then based on it, send an e-mail to all the people with a selected ID. My question is, how can I then write the sent email (just the subject and message part), combined with the selected students ID's in a new Mysql DB table? Every time I've tried to do it with a normal INSERT query, It says the selected ID is invalid, assumes it is 0, and writes the subject and message and in the ID column, it inputs the assumed value - 0. If I have 2 or 3 people selected (2-3 ID's ) it writes the first one and then I get an error. If the first field is full (ID is 0 for it), it outputs an error and writes nothing.
The end result should be a table with 3 columns - student id, email subject and e-mail message. The purpose is to notify selected students and then write the message in a table, so I can see witch students have been notified.
<?php error_reporting(E_ALL); ini_set('display_errors', 1);then the rest of your code, to see if it yields anything, as well asor die(mysqli_error($conn))tomysqli_query().