gpt4 book ai didi

php - MySQL无法从网络应用程序中删除

转载 作者:行者123 更新时间:2023-11-29 00:16:42 24 4
gpt4 key购买 nike

我有一个动态表来显示数据库表中的所有条目。每行都有一个删除按钮,我想我可以点击其中的任何一个来删除数据库中的条目。但它不起作用。不过我可以从 phpmyadmin 中删除。这是我的 php 和 sql 代码:

<?php

@$db = new mysqli('localhost', 'xxxx', 'xxx', 'WARD');
if (mysqli_connect_errno()) {
echo 'Error: Could not connect to database. Please try again later.';
exit;
}

$query = "SELECT * FROM PATIENT";
$result = $db->query($query);
$num_results = $result->num_rows;

echo "
<form action=\"dbmgmt.php\" method=\"post\">
";
echo "
<table data-role=\"table\" id=\"table-column-toggle\" data-mode=\"columntoggle\" class=\"ui-responsive table-stroke\">
<thead>
<tr>
<th data-priority=\"2\">
Patient_ID
</th>
<th>
Patient Name
</th>
<th data-priority=\"3\">
Patient IC
</th>
<th data-priority=\"1\">
Phone
</th>
<th data-priority=\"5\">
Address
</th>
<th data-priority=\"5\">
Date Admit
</th>
<th data-priority=\"5\">
Date Discharge
</th>
</tr>
</thead>
<tbody>
";
echo "
</form>
";
//This form is for submission of gymid to the reservation page.
echo "
<form action=\"dbmgmt.php\" method=\"post\">
";
for ($i = 0; $i < $num_results; $i++) {
$row = $result->fetch_assoc();

if (stripslashes($row['Patient_ID']) == !0) {
echo "
<tr>
" . PHP_EOL;
echo "
<td align=\"center\">
" . stripslashes($i + 1) . "
</td>
" . PHP_EOL;
echo "
<td align=\"center\">
" . stripslashes($row['Patient_Name']) . "
</td>
" . PHP_EOL;
echo "
<td>
" . stripslashes($row['Patient_IC']) . "
</td>
" . PHP_EOL;
echo "
<td align=\"center\">
" . stripslashes($row['Phone']) . "
</td>
" . PHP_EOL;
echo "
<td align=\"center\">
" . stripslashes($row['Address']) . "
</td>
" . PHP_EOL;
echo "
<td align=\"center\">
" . stripslashes($row['Date_Admit']) . "
</td>
" . PHP_EOL;
echo "
<td align=\"center\">
" . stripslashes($row['Date_Discharge']) . "
</td>
" . PHP_EOL;
echo "
<td align=\"center\">
<button type=\"submit\" name = \"deleteEntry\" value = \"" . stripslashes($row['Patient_ID']) . "\">
Delete
</button>
</td>
" . PHP_EOL;
echo "
</tr>
" . PHP_EOL;
}
}

echo "
</tbody>
</table>
";
echo "
</form>
";

$delete = NULL;
if (isset($_POST['deleteEntry'])) {
$delete = $_POST['deleteEntry'];
}
$deletequery = "DELETE FROM PATIENT
WHERE Patient_ID = $delete";
$deleteresult = $db->query($deletequery);

$result->free();
$db->close();
?>

更新:我创建了另一个名为 delete.php 的文件并更改了帖子。

<form action=\"delete.php\" method=\"post\">

<?php
@ $db = new mysqli('localhost', 'xxxx', 'xxxx', 'WARD');
if (mysqli_connect_errno()) {
echo 'Error: Could not connect to database. Please try again later.';
exit;
}

$delete = NULL;
if(isset($_POST['deleteEntry'])){ $delete = $_POST['deleteEntry'];}
//$deletequery = "DELETE FROM PATIENT
// WHERE Patient_ID = $delete";
//$deleteresult = $db->query($deletequery);

mysqli_query($db,"DELETE FROM PATIENT
WHERE Patient_ID = $delete");

header ('Location: dbmgmt.php');
$result->free();
$db->close();

?>

最佳答案

查询正在从您的表中删除记录,但要查看它们,您需要第二次刷新页面,因为选择语句在删除之前运行,这就是为什么您看不到它被删除的原因。

为了避免这种情况,最好将删除发送到其他页面,如 delete.php。

编辑:您也可以使用 AJAX 删除它们

编辑2:

替换这个:

  $delete = NULL;
if (isset($_POST['deleteEntry'])) {
$delete = $_POST['deleteEntry'];
}
$deletequery = "DELETE FROM PATIENT
WHERE Patient_ID = $delete";
$deleteresult = $db->query($deletequery);

通过:

 if (isset($_POST['deleteEntry'])) {
$delete = $_POST['deleteEntry'];

$deletequery = "DELETE FROM PATIENT WHERE Patient_ID = '".$delete."' ";
$deleteresult = $db->query($deletequery);
}

关于php - MySQL无法从网络应用程序中删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22742918/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com