PHP mailer与SQL一起在特定行上发送电子邮件

PHP mailer与SQL一起在特定行上发送电子邮件,php,html,css,mysql,sql,Php,Html,Css,Mysql,Sql,我不使用日常的php、html、css,但我知道这种编程语言的基础知识。 我想通过按钮在指定行上发送电子邮件。(我附上了一张带有脚本索引页的图片,并用红色按钮标记) 目前,无论我按哪个按钮,代码都会向所有行发送电子邮件。 有人能告诉我怎么才能只从指定行的按钮发送电子邮件吗 我的代码是: <?php require "config.php"; require "common.php"; try { $connection = new PDO($

我不使用日常的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"; ?>

măsurişi acţiuni计划-任务经理
文件:
QS01-323
马苏里计划
版本号:
07
帕吉纳:
1 din 1
数据:
20.02.2021

身份证件
优先酸盐
普罗德斯
重新收集的问题
Cauza
实施活动
负责人电子邮件
特曼
进步
负责任的护理
数据verficarii
效率
编辑
完成 我解决了这个问题。 如果有人需要,下面是代码

<?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'])) 
{
    try {
        $connection = new PDO($dsn, $username, $password, $options);
        $id = $_POST['button_pressed'];

        $sql = "SELECT * FROM tasks WHERE id = :id;";
        $statement = $connection->prepare($sql);
        $statement->bindValue(':id', $id);
        $statement->execute();
        
        $row = $statement->fetch(PDO::FETCH_ASSOC); 
    } catch (PDOException $e) {
        echo $e->getMessage();
    }
    
    $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);
}
?>
<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="<?php echo $row['id']; ?>" />
                </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"; ?>

măsurişi acţiuni计划-任务经理
文件:
QS01-323
马苏里计划
版本号:
07
帕吉纳:
1 din 1
数据:
20.02.2021

身份证件
优先酸盐
普罗德斯
重新收集的问题
Cauza
实施活动
负责人电子邮件
特曼
进步
负责任的护理
数据verficarii
效率
编辑
完成
请去读一读。与您的问题相关的最小代码,以文本形式直接属于您的问题,并且格式正确。不要只是将其转储到外部平台上。首先,您需要使用按钮传递特定的行id。然后,您需要从数据库中仅选择具有该ID的记录作为开始,或者如果出于某种原因仍要循环所有记录,则在循环中,仅当当前记录的ID与您通过表单提交的ID匹配时,才发送邮件。您能举个例子吗?我知道在这一行“foreach($result as$row):”添加数据库中的ID行,对吗?可能类似于
,ID值将用于“if”或“foreach”?很高兴现在解决了这个问题。不过,请注意一下catch(PDOException$e){echo$e->getMessage();}
的逻辑……如果查询失败,您将输出异常消息(在实时系统中,异常消息本身并不十分安全,应该记录),但是,即使出现严重故障,您的代码只是在下面继续,并尝试创建电子邮件。但很明显,它会失败,因为$row不会被定义,所以它还会产生其他错误和警告。这不是一种处理数据库错误的可靠方法。我也不知道您为什么再次创建
$connection
?它已经在脚本的第一部分中创建。您不需要为每个查询建立单独的连接。谢谢。此代码仅在本地公司LAN中的有限连接中使用。我现在已经创建了脚本,可以使用后,没有人会更改它:)
此代码用于有限的连接,仅在本地公司局域网中使用
抱歉,我不确定您为什么认为这与我所说的任何内容有关?你能解释一下吗?编写安全可靠的代码不仅限于面向internet的应用程序。每个应用程序都应该这样写。你不能假设你的局域网内的用户和设备永远不会构成威胁。它不会通过公共IP访问。仅在具有内部IP的LAN上工作。