我有这段代码:
<?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: code-week@example.com' . "\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>
它的目的是从学生表中读取所选行的ID,打印它以便我可以看到它从上一页中选择正确,然后根据它,发送电子邮件给所有人选定的ID。我的问题是,如何在新的Mysql数据库表中编写已发送的电子邮件(只是主题和消息部分),以及所选的学生ID?每当我尝试使用普通的INSERT查询时,它表示所选ID无效,假设它为0,并且写入主题和消息,并且在ID列中,它输入假定值-0。如果我有两个或三个人选择(2-3个ID)它写第一个然后我得到一个错误。如果第一个字段已满(ID为0),则输出错误并且不写入任何内容 最终结果应该是一个包含3列的表 - 学生ID,电子邮件主题和电子邮件。目的是通知选定的学生,然后在表格中写下信息,这样我就可以看到女巫学生已经收到通知。
答案 0 :(得分:2)
First of all it's better not to use unescaped POST data in your queries, as it might lead to a sql injection.
As for the 'insert' part - if you have all the necessary fields, primary keys and default values, just use an INSERT statement to save the data
答案 1 :(得分:2)
1)。为is_notified
创建一个列,并将其初始值设置为零。
2)。电子邮件发送给学生后,立即将is_notified
列更新为1。
如果要使用PDO进行多次更新,可以使用: -
<?php
try
{
$s = $conn->prepare("your SQL");
}
catch(PDOException $e)
{
echo $e->getMessage();
}
foreach(loop through the ids here)
{
$s->bindParam(':paramname', $paramvalue);
$s->execute();
}
?>
答案 2 :(得分:1)
INSERT INTO tbl_name (a,b,c) VALUES(1,2,3),(4,5,6),(7,8,9);
答案 3 :(得分:0)
这对我有用:
if(!empty($_POST['Send2'])){
14 $mail= mysqli_query($conn, "SELECT student_id, mail FROM students WHERE student_id IN($_POST[id_mail])");
15 echo "SELECT student_id, mail FROM students WHERE student_id IN($_POST[id_mail]) ";
16 while ($row_mail = mysqli_fetch_assoc($mail)) {
17 echo "$row_mail[mail] ";
18 $to = $row_mail['mail'];
19 $subject = $_POST['head'];
20 $message = $_POST['body'];
21 $headers = 'From: code-week@example.com' . "\r\n" .
22 'Please do not reply to this message' . "\r\n" .
23 'X-Mailer: PHP/' . phpversion();
24 mail($to, $subject, $message, $headers);
25 $sent_mails= mysqli_query($conn, "INSERT INTO sent_mails (id_st, mail_head, content) VALUES ('$_POST[id_mail]', '$subject', '$message') ");
26 if (mysqli_query($conn, $sent_mails)){
27 echo "Success";
28 } else {
29 echo "Error ". $sent_mails . "<br/>" . mysqli_error($conn);
30 }
31 }
32 }