PHP从复选框中获取MYSQL数据

时间:2023-01-28 11:15:23

I am creating a form to either approve or deny a time-off request. So this will bring up a table with rows of data of time off requests. You check the box and on submitting the form I want it to change the Status of the row AND send an email to the user letting them know it has been accepted. I have it updating the row, but when it sends the email it is using the information from the LAST row of data in the table. So I have a PHP form containing the following:

我正在创建一个表单来批准或拒绝暂停请求。因此,这将显示一个包含时间关闭请求数据行的表。您选中此框并提交表单我希望它更改行的状态并向用户发送电子邮件,让他们知道它已被接受。我有更新行,但是当它发送电子邮件时,它正在使用表中最后一行数据的信息。所以我有一个包含以下内容的PHP表单:

    <form name="bulk_action_form" action="" method="post"/>
    <tr>
            <th></th>
            <th>Username</th>
            <th>Employee Name</th>
            <th>First Day Off</th>
            <th>First Day Back</th>
            <th>Status</th>
            <th>Date of Request</th>

    </tr>
    </thead>
    <?php
        if(mysqli_num_rows($query) > 0){
            while($row = mysqli_fetch_assoc($query)){
    ?>

    <tr>
        <td align="center"><input type="checkbox" name="checked_id[]" class="checkbox" value="<?php echo $row['ID']; ?>"/>
            <input type="hidden" name="worker_name" value="<?php echo $row['worker_name']; ?>"/>
            <input type="hidden" name="Name" value="<?php echo $row['Name']; ?>"/>
            <input type="hidden" name="FirstDayOff" value="<?php echo $row['FirstDayOff']; ?>"/>
            <input type="hidden" name="FirstDayBack" value="<?php echo $row['FirstDayBack']; ?>"/></td>

        <td><?php echo $row['worker_name']; ?></td>
        <td><?php echo $row['Name']; ?></td>
        <td><?php echo $row['FirstDayOff']; ?></td>
        <td><?php echo $row['FirstDayBack']; ?></td>
        <td><?php echo $row['Status']; ?></td>
        <td><?php echo $row['_sfm_form_submision_time_']; ?></td>
    </tr> 
    <?php } }else{ ?>
        <tr><td colspan="5">No records found.</td></tr> 
    <?php } ?>
<input type="submit" class="btn btn-success" name="bulk_approve_submit" formaction="action-approve.php" value="Approve Request">

    </form>

And my action-approve.php looks as follows.

我的action-approve.php如下所示。

<?php
session_start();
include_once('dbConfig.php');
if(isset($_POST['bulk_approve_submit'])){

$idArr = $_POST['checked_id'];

    foreach($idArr as $id){
        mysqli_query($conn,"UPDATE Request_off SET Status='Approved' WHERE ID=".$id);

// Get Parameters about the row that was selected
$worker_name = $_POST['worker_name'];
$Name = $_POST['Name'];
$FirstDayOff = $_POST['FirstDayOff'];
$FirstDayBack = $_POST['FirstDayBack'];
// Email Parameters 
$to      = $worker_name.'@somewhere.com, request-off@somewhere.com';
$subject = 'APPROVED: Time off request for '.$Name;
$message = 'Dear '.$Name.','. "\r\n\n" .'Your time off request from '.$FirstDayOff.' to '.$FirstDayBack.' has been approved!'. "\r\n\n" .'Note: Please keep a copy of this email for your records.';
$headers = 'From: request-off@somewhere.com';

        mail($to, $subject, $message, $headers);
    }
    $_SESSION['success_msg'] = 'Request has been approved successfully.';
    header("Location: Request_off-admin.php?Status=Pending");
}
?>

How would I go about getting the row data from the checked row, NOT the last row in the particular table?

我如何从选中的行获取行数据,而不是特定表中的最后一行?

UPDATE:

更新:

I just took those hidden fields and turned them into checkboxes. If I check all of the boxes for a particular row, I can make it update and send the email correctly. I'm going to look into hiding those checkboxes and using just one to select them all...

我只是把那些隐藏的字段变成了复选框。如果我检查特定行的所有框,我可以让它更新并正确发送电子邮件。我打算隐藏那些复选框,只用一个来选择它们......

1 个解决方案

#1


3  

Essentially your problem is that you're using the same name for all your inputs. If you look at the source of your PHP form in your web browser you will notice lots of worker_name fields, for instance - one for each record. So the last input in the list is overwriting all the previous ones when they're posted to your script.

基本上你的问题是你为所有输入使用相同的名称。如果您在Web浏览器中查看PHP表单的来源,您会注意到许多worker_name字段,例如每个记录一个。所以列表中的最后一个输入是在它们发布到您的脚本时覆盖所有以前的输入。

My suggestion, however, would be to only POST the IDs in the checkboxes. The rest of the data you already have in your database. In the action-approve.php script you can load the name, days off, etc details from the database and loop over them.

不过,我的建议是只在复选框中发布ID。您已在数据库中拥有的其余数据。在action-approve.php脚本中,您可以从数据库加载名称,休息日等细节并循环遍历它们。

Something like this (untested):

像这样(未经测试):

<?php

foreach($idArr as $id){
    mysqli_query($conn,"UPDATE Request_off SET Status='Approved' WHERE ID=".$id);

// Get Parameters about the row that was selected
$newQuery = mysqli_query($conn, "SELECT * FROM Request_off WHERE ID = $id");
if(mysqli_num_rows($newQuery) > 0){
    while($_newRow = mysqli_fetch_assoc($newQuery)){
        $worker_name = $_newRow['worker_name'];
        $Name = $_newRow['Name'];
        $FirstDayOff = $_newRow['FirstDayOff'];
        $FirstDayBack = $_newRow['FirstDayBack'];

        // Email Parameters 
        $to      = $worker_name.'@somewhere.com, request-off@somewhere.com';
        $subject = 'APPROVED: Time off request for '.$Name;
        $message = 'Dear '.$Name.','. "\r\n\n" .'Your time off request from '.$FirstDayOff.' to '.$FirstDayBack.' has been     approved!'. "\r\n\n" .'Note: Please keep a copy of this email for your records.';
        $headers = 'From: request-off@somewhere.com';

        mail($to, $subject, $message, $headers);
    }
}

This will also make it easier to validate your data, because currently people can easily do SQL injection or other unexpected things.

这样也可以更轻松地验证您的数据,因为目前人们可以轻松地执行SQ​​L注入或其他意外的事情。

#1


3  

Essentially your problem is that you're using the same name for all your inputs. If you look at the source of your PHP form in your web browser you will notice lots of worker_name fields, for instance - one for each record. So the last input in the list is overwriting all the previous ones when they're posted to your script.

基本上你的问题是你为所有输入使用相同的名称。如果您在Web浏览器中查看PHP表单的来源,您会注意到许多worker_name字段,例如每个记录一个。所以列表中的最后一个输入是在它们发布到您的脚本时覆盖所有以前的输入。

My suggestion, however, would be to only POST the IDs in the checkboxes. The rest of the data you already have in your database. In the action-approve.php script you can load the name, days off, etc details from the database and loop over them.

不过,我的建议是只在复选框中发布ID。您已在数据库中拥有的其余数据。在action-approve.php脚本中,您可以从数据库加载名称,休息日等细节并循环遍历它们。

Something like this (untested):

像这样(未经测试):

<?php

foreach($idArr as $id){
    mysqli_query($conn,"UPDATE Request_off SET Status='Approved' WHERE ID=".$id);

// Get Parameters about the row that was selected
$newQuery = mysqli_query($conn, "SELECT * FROM Request_off WHERE ID = $id");
if(mysqli_num_rows($newQuery) > 0){
    while($_newRow = mysqli_fetch_assoc($newQuery)){
        $worker_name = $_newRow['worker_name'];
        $Name = $_newRow['Name'];
        $FirstDayOff = $_newRow['FirstDayOff'];
        $FirstDayBack = $_newRow['FirstDayBack'];

        // Email Parameters 
        $to      = $worker_name.'@somewhere.com, request-off@somewhere.com';
        $subject = 'APPROVED: Time off request for '.$Name;
        $message = 'Dear '.$Name.','. "\r\n\n" .'Your time off request from '.$FirstDayOff.' to '.$FirstDayBack.' has been     approved!'. "\r\n\n" .'Note: Please keep a copy of this email for your records.';
        $headers = 'From: request-off@somewhere.com';

        mail($to, $subject, $message, $headers);
    }
}

This will also make it easier to validate your data, because currently people can easily do SQL injection or other unexpected things.

这样也可以更轻松地验证您的数据,因为目前人们可以轻松地执行SQ​​L注入或其他意外的事情。