【发布时间】:2021-06-06 02:42:50
【问题描述】:
我不使用日常的 php、html、css,但我知道这种编程语言的基础知识。 我想通过按钮在指定行上发送电子邮件。 (我附上了我的脚本索引页的图片,并用红色标记了按钮)
目前,无论我按下哪个按钮,代码都会向所有行发送电子邮件。 有人可以解释一下如何仅在指定行上从按钮发送电子邮件吗?
我的代码是:
<?php
require "config.php";
require "common.php";
try {
$connection = new PDO($dsn, $username, $password, $options);
$sql = "SELECT * FROM tasks";
$statement = $connection->prepare($sql);
$statement->execute();
$result = $statement->fetchAll();
} catch(PDOException $error) {
echo $sql . "<br>" . $error->getMessage();
}
if (isset($_GET['id'])) {
try {
$connection = new PDO($dsn, $username, $password, $options);
$id = $_GET['id'];
$sql = "INSERT INTO oldtasks SELECT * FROM tasks WHERE id = :id; DELETE FROM tasks WHERE id = :id;";
$statement = $connection->prepare($sql);
$statement->bindValue(':id', $id);
$statement->execute();
$task = $statement->fetch(PDO::FETCH_ASSOC);
} catch(PDOException $error) {
}
}
?>
<?php
if(isset($_POST['button_pressed']))
{
foreach ($result as $row) :
$responsabil = $row['responsabil'];
$produs = $row['produs'];
$prob_recl = $row['prob_recl'];
$cauza = $row['cauza'];
$act_impl = $row['act_impl'];
$termen = $row['termen'];
$from = "tasks@plastec-romania.ro";
$headers = "From:" . $from;
$to = $responsabil;
$subject = 'Reminder - ' .$produs ;
$message = "Reminder: \n\nProdus: " . $produs . "\nProblema reclamata: " . $prob_recl . "\nCauza: " . $cauza . "\nActiuni de implementat: " . $act_impl . "\nTermen: " . $termen . "\n\nTasks: http://192.168.0.8/tasks";
mail($to, $subject, $message, $headers);
endforeach;
}
?>
<title>Plan de măsuri şi acţiuni - Task manager</title>
<?php require "templates/header.php"; ?>
<table border="1px" width="100%">
<tr>
<td width="100px" style="font-weight: bold;">Document:</td>
<td width="100px" align="right">QS01-323</td>
<td rowspan="8" align="center" style="font-size: 40px; font-weight: bold; background-color: #C1CBD1;">Plan de masuri</td>
<td rowspan="8" align="center" width="300px"><img src="img/logo.png" width="290px"></img></td>
<tr>
<tr>
<td width="100px" style="font-weight: bold;">Versiune:</td>
<td width="100px" align="right">07</td>
<tr>
<tr>
<td width="100px" style="font-weight: bold;">Pagina:</td>
<td width="100px" align="right">1 din 1</td>
<tr>
<tr>
<td width="100px" style="font-weight: bold;">Data:</td>
<td width="100px" align="right">20.02.2021</td>
<tr>
</table>
<p align="center">
<span style="padding-right: 150px;"><a href="create.php"><strong><i class="fas fa-plus-circle"></i> Adauga un task nou</strong></a></span>
<span><a href="completed.php"><i class="fas fa-clipboard-check"></i> Task-uri inchise</a></span>
</p>
<table border="1px;">
<thead>
<tr>
<th width="0" style="visibility: hidden; display: none; background-color: #C1CBD1;">ID</th>
<th width="5%" style="background-color: #C1CBD1"><i class="fas fa-exclamation-triangle"></i><br />Prioritate</th>
<th width="10%" style="background-color: #C1CBD1"><i class="fab fa-product-hunt"></i><br />Produs</th>
<th width="10%" style="background-color: #C1CBD1"><i class="fas fa-exclamation"></i><br />Problema reclamata</th>
<th width="10%" style="background-color: #C1CBD1"><i class="fas fa-briefcase-medical"></i><br />Cauza</th>
<th width="10%" style="background-color: #C1CBD1"><i class="fas fa-exclamation-circle"></i><br />Actiuni de implementat</th>
<th width="10%" style="background-color: #C1CBD1"><i class="fas fa-user-alt"></i><br />Responsabil email</th>
<th width="5%" style="background-color: #C1CBD1"><i class="fas fa-hourglass-half"></i><br />Termen</th>
<th width="5%" style="background-color: #C1CBD1"><i class="fas fa-tasks"></i><br />Progres</th>
<th width="10%" style="background-color: #C1CBD1"><i class="fas fa-user"></i><br />Responsabil verificare</th>
<th width="5%" style="background-color: #C1CBD1"><i class="fas fa-calendar-day"></i><br />Data verficarii</th>
<th width="5%" style="background-color: #C1CBD1"><i class="fas fa-check-circle"></i><br />Eficienta</th>
<th width="5%" style="background-color: #C1CBD1"><i class="fas fa-edit"></i><br />Edit</th>
<th width="5%" style="background-color: #C1CBD1"><i class="fas fa-clipboard-check"></i><br />Complete</th>
</tr>
</thead>
<tbody>
<?php foreach ($result as $row) : ?>
<tr>
<td align="center" width="0" style="visibility: hidden; display: none;"><?php echo escape($row["id"]); ?></td>
<td align="center" width="5%" class="importanta<?php echo escape($row["importanta"]); ?>"></td>
<td align="center" width="10%"><?php echo escape($row["produs"]); ?></td>
<td align="center" width="10%"><?php echo escape($row["prob_recl"]); ?></td>
<td align="center" width="10%"><?php echo escape($row["cauza"]); ?></td>
<td align="center" width="10%"><?php echo escape($row["act_impl"]); ?></td>
<td align="center" width="10%"><?php echo escape($row["responsabil"]); ?><br />
<form action="" method="post">
<input type="submit" value="Send details to embassy" />
<input type="hidden" name="button_pressed" value="1" />
</form>
</td>
<td align="center" width="5%"><?php echo escape($row["termen"]); ?></td>
<td width="5%">
<progress max="100" value="<?php echo escape($row["progres"]); ?>" class="v<?php echo escape($row["progres"]); ?>"></progress><br /><center><?php echo escape($row["progres"]); ?>%</center>
</td>
<td align="center" width="10%"><?php echo escape($row["resp_verifica"]); ?></td>
<td align="center" width="5%"><?php echo escape($row["data_verificarii"]); ?></td>
<td width="5%">
<progress max="100" value="<?php echo escape($row["eficienta"]); ?>" class="v<?php echo escape($row["eficienta"]); ?>"></progress><br /><center><?php echo escape($row["eficienta"]); ?>%</center>
</td>
<td align="center" width="5%"><a href="update-single.php?id=<?php echo escape($row["id"]); ?>"><i class="fas fa-edit"></i></a></td>
<td align="center" width="5%"><a href="?id=<?php echo escape($row["id"]); ?>"><i class="fas fa-clipboard-check"></i></a></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<br />
<?php require "templates/footer.php"; ?>
【问题讨论】:
-
请阅读How to Ask和minimal reproducible example。与您的问题相关的最少代码直接属于您的问题,以文本形式和格式正确。不要只是将其转储到外部平台上。
-
您需要首先使用您的按钮传递一个特定的行 ID。然后您需要从数据库中仅选择具有该 ID 的记录开始,或者如果您出于某种原因仍想遍历所有记录,则在循环内仅在当前记录的 id 匹配时发送邮件您通过表单提交的 ID。
-
你能给我举个例子吗?我知道在这一行“foreach ($result as $row) :”从数据库中添加 ID 行,对吧?
-
可能类似于
<input type="hidden" name="button_pressed" value="<?php echo $row["id"]; ?>" />,然后在 PHP 中使用该值来决定从数据库中获取哪一行,然后只为那一行发送一封电子邮件。 -
而ID值会用在“if”还是“foreach”?