我正在创建一个表单来批准或拒绝暂停请求。因此,这将显示一个包含时间请求数据行的表。您选中此框并提交表单我希望它更改行的Status
并向用户发送电子邮件,让他们知道它已被接受。我有更新行,但是当它发送电子邮件时,它使用表中最后一行数据的信息。所以我有一个包含以下内容的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>
我的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");
}
?>
如何从已检查的行中获取行数据,而不是特定表中的最后一行?
更新:
我只是把那些隐藏的字段变成了复选框。如果我检查特定行的所有框,我可以让它更新并正确发送电子邮件。我打算隐藏这些复选框,只使用一个选中它们......
答案 0 :(得分:3)
基本上,您的问题是您对所有name
使用相同的input
。如果您在Web浏览器中查看PHP表单的来源,您会注意到许多worker_name
字段,例如每个记录一个。因此,列表中的最后一个输入会在它们发布到您的脚本时覆盖所有以前的输入。
然而,我的建议是仅在复选框中发布ID
。您已在数据库中拥有的其余数据。在action-approve.php
脚本中,您可以从数据库加载名称,休息日等细节并循环遍历它们。
像这样(未经测试):
<?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);
}
}
这样也可以更轻松地验证您的数据,因为目前人们可以轻松地执行SQL injection或其他意想不到的事情。