Php 删除表上的记录
我在删除表中的某个记录时遇到问题。我目前所做的是为删除和编辑创建两个表单。由于某些原因,我无法获取已删除的所需ID 我试图回显从该表单传递的id的值。传递的ID是13 所以基本上我对行做了一个循环。我只是在获取某一行的ID时遇到问题 这是我桌子的截图 下面是我如何使用表单的 customer.phpPhp 删除表上的记录,php,html,Php,Html,我在删除表中的某个记录时遇到问题。我目前所做的是为删除和编辑创建两个表单。由于某些原因,我无法获取已删除的所需ID 我试图回显从该表单传递的id的值。传递的ID是13 所以基本上我对行做了一个循环。我只是在获取某一行的ID时遇到问题 这是我桌子的截图 下面是我如何使用表单的 customer.php <div class="box-body table-responsive"> <form id='edi
<div class="box-body table-responsive">
<form id='edit' action="functions/edit.php" method="post"></form>
<form id='delete' action="functions/delete.php" method="post"></form>
<table id="example1" class="table table-bordered table-striped">
<thead>
<tr>
<th>Actions</th>
<th>ID</th>
<th>User</th>
<th>Contact Number</th>
<th>Date Registered</th>
<th>Email</th>
<th>Organization</th>
<th>Address</th>
</tr>
</thead>
<tbody>
<?php
$Query = mysqli_query($conn, "SELECT * FROM customer");
while($row = mysqli_fetch_array($Query)){
echo "<tr>";
echo "<td><div class='btn-group'>
<button type='button' class='btn btn-info dropdown-toggle' data-toggle='dropdown'>Action
<span class='caret'></span>
<span class='sr-only'>Toggle Dropdown</span>
</button>
<ul class='dropdown-menu' role='menu'>
<li><button form='edit' class='btn' type='submit' name='edit' style='width: 100%;background-color: white;'>Edit</button></li>
<li><button form='delete' class='btn' type='submit' name='delete' style='width: 100%;background-color: white;'>Delete</button></li>
</ul>
</div>
</td>";
echo "<td>". "<input form='delete' type='hidden' name='id' value='" . $row['id'] . "'> " . $row['id'] . "</td>";
echo "<td>". $row['firstname'] . " " . $row['lastname'] . "</td>";
echo "<td>". $row['contact'] . "</td>";
echo "<td>". $row['date_reg'] . "</td>";
echo "<td><span class='label label-success'>" . $row['email'] . "</span></td>";
echo "<td>". $row['organization'] . "</td>";
echo "<td>". $row['address'] . "</td></tr>";
}
?>
</tbody>
<tfoot>
<tr>
<th>Actions</th>
<th>ID</th>
<th>User</th>
<th>Contact Number</th>
<th>Date Registered</th>
<th>Email</th>
<th>Organization</th>
<th>Address</th>
</tr>
</tfoot>
</table>
</div>
行动
身份证件
使用者
联系电话
登记日期
电子邮件
组织机构
地址
行动
身份证件
使用者
联系电话
登记日期
电子邮件
组织机构
地址
下面是我的delete.php的样子
<?php
include('config.php');
$deleteID = $_POST['id'];
//----- Check if user to be deleted is the user in session -----//
echo $deleteID;
// $deleteSQL = mysqli_query($conn, "delete from customer where id = '$deleteID'");
echo "<script type='text/javascript'>alert('Deleted succesfuly');</script>";
//header('Refresh: 0; url=../customers.php');
//
?>
您的delete.php应该是这样的:
<?php
include('config.php');
$deleteID = $_POST['id'];
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$deleteSQL = mysqli_query($conn, "delete from customer where id = '$deleteID'");
if ($conn->query($sql) === TRUE) {
echo "<script type='text/javascript'>alert('Deleted succesfuly');</script>";
//header('Refresh: 0; url=../customers.php');
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
现在,如果在config.php中正确指定了$servername、$username、$password、$dbname
,这将起作用
您这边的编码很差。请给我们看一些代码。我们不能凭空想出答案。更新了帖子。很抱歉谢谢是的,我知道,我只是想在改进代码之前确保将正确的ID传递给delete.php。那么我的答案呢?对你来说,这看起来像是错的吗?