我正在尝试使用PHP更新数据库中的表。
我使用循环来获取表中的所有数据。然后,添加"批准"用户可以单击的每行前面的按钮,如果他们想要批准请求,则会将状态更改为“已批准”。
现在我被卡住了,我无法找到更新状态字段的方法,而且我对JavaScript不满意。
我想让按钮名称动态,但我不能
Output image
这是循环代码:
while($row = mysqli_fetch_array($result))
{
echo "<tr bgcolor='#cccccc'>";
echo "<td>" . $row['nursename'] . "</td>";
echo "<td>" . $row['dr'] . "</td>";
echo "<td>" . $row['natureofreq'] . "</td>";
echo "<td>" . $row['clinic'] . "</td>";
echo "<td>" . $row['clinicfloor'] . "</td>";
echo "<td>" . $row['qitem'] . "</td>";
echo "<td>" . $row['moreinfo'] . "</td>";
echo "<td>" . $row['user_check'] . "</td>";
echo "<td>" . $row['id'] . "</td>";
echo "<td bgcolor='#009FE3'>" . $row['rstatus'] . "</td>";
echo '<form name="submit" method="post" action="">';
echo "<td>" . '<button id="approve" name="approve"> Approve</button>' . "</td>";
echo '</form>';
echo "</tr>";
}
这是更新代码,无效:
$id=$row['id'];
if(isset($_POST['approve']))
{
$allowed = mysqli_query($n," UPDATE newrequest SET rstatus = 'Approved' WHERE id = '$id' ");
}
我将不胜感激。
答案 0 :(得分:2)
$url_of_current_page = $_SERVER['REQUEST_URI'];
if(isset($url_of_current_page)){
$_SESSION['url_of_current_page'] = $url_of_current_page;
}
$approve_row = '';
while($row = mysqli_fetch_array($result)){
$approve_row .= "<tr bgcolor='#cccccc'>
<td>" . $row['nursename'] . "</td>
<td>" . $row['dr'] . "</td>
<td>" . $row['natureofreq'] . "</td>
<td>" . $row['clinic'] . "</td>
<td>" . $row['clinicfloor'] . "</td>
<td>" . $row['qitem'] . "</td>
<td>" . $row['moreinfo'] . "</td>
<td>" . $row['user_check'] . "</td>
<td bgcolor='#009FE3'>" . $row['rstatus'] . "</td>
<td>
<a href='action.php?id=" . $row['id'] . "'>
<button>Approve</button>
</a>
</td>
</tr>";
}
if(isset($approve_row)){
echo $approve_row;
}
动作代码。如果您的操作页面不是action.php,请告诉我页面的名称。
if(isset($_GET['id'])){
$id = $_GET['id'];
mysqli_query($n," UPDATE newrequest SET rstatus = 'Approved' WHERE id = '$id' ");
if(isset($_SESSION['url_of_current_page'])){
header( "Location: ". $_SESSION['url_of_current_page'] );
exit();
}
}
答案 1 :(得分:0)
您似乎没有从您丢弃的代码
向数据库发送任何数据