因此,我创建了一个接受条目并将其存储在名为Warehouse_Maitenence的SQL数据库中的表单(是的,我知道它的拼写错误,我是故意这样做的)。这组代码的目标是连接到数据库并相应地显示表]
<?php include("nav.php") ?>
<body style='background-color:#C0C0C0'>
<style>
.top{
text-align: Left;
}
.Headers_Inputs{
padding-top: 20px;
padding-bottom: 20px;
border-style: solid;
border-width: 1px;
}
input{
width: 100%;
}
.PRIMARY_TABLE{
padding-left: 20px;
padding-right: 20px;
width: 100%;
}
body{
padding-left: 20px;
padding-right: 20px;
padding-top: 20px;
padding-bottom: 60px;
}
.PRIMARY_TRAY{
border-style: solid;
border-width: 2px;
}
</style>
<body>
<table width="100%" border="1">
<tr>
<th style="text-align: center; font-size: 16px;"> ID </th>
<th style="text-align: center; font-size: 16px;"> MACHINE </th>
<th style="text-align: center; font-size: 16px;"> LABEL </th>
<th style="text-align: center; font-size: 16px;"> CONDITION </th>
<th style="text-align: center; font-size: 16px;"> ACTION </th>
<th style="text-align: center; font-size: 16px;"> ADDRESS </th>
<th style="text-align: center; font-size: 16px;"> DATE </th>
<th style="text-align: center; font-size: 16px;"> COMMENT </th>
<th style="text-algin: center; font-size: 16px;"> DELETE </th>
</tr>
<?php
$username="";
$password="";
$database="";
$servername = "";
$conn = mysqli_connect($servername, $username, $password, $database);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = 'SELECT ID, MACHINE, LABEL, COND, ACTION, ADDRESS, DATE, COMMENT FROM Warehouse_Maitenence ORDER BY `Warehouse_Maitenence`.`MACHINE` ASC, `Warehouse_Maitenence`.`LABEL` ASC, `Warehouse_Maitenence`.`ID` ASC';
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td style='text-align: center; font-size: 16px;' id=".$row["ID"].">".$row["ID"]."</td>";
echo "<td style='text-align: center; font-size: 16px;'>".$row["MACHINE"]."</td>";
echo "<td style='text-align: center; font-size: 16px;'>".$row["LABEL"]."</td>";
echo "<td style='text-align: center; font-size: 16px;'>".$row["COND"]."</td>";
echo "<td style='text-align: center; font-size: 16px;'>".$row["ACTION"]."</td>";
echo "<td style='text-align: center; font-size: 16px;'>".$row["ADDRESS"]."</td>";
echo "<td style='text-align: center; font-size: 16px;'>".$row["DATE"]."</td>";
echo "<td style='text-align: center; font-size: 16px;'>".$row["COMMENT"]."</td>";
echo "<td style='text-align: center; font-size: 16px;'><input type='button' onclick=delete_entry(".$row["ID"].")</td>";
echo "</tr>";
}
} else { echo "0 results"; }
echo "</table>";
mysqli_close($conn);
?>
因此,此级别的代码可以按预期工作,直到删除点为止。我希望能够在表的每一行的末尾加载按钮,然后onclick从表和SQL数据库中删除该行。
对于表,我已经有一个JS,它可以在我一直在处理的另一组代码中工作。
function deleteRow(r) {
var i = r.parentNode.parentNode.rowIndex;
document.getElementById("dataTable").deleteRow(i);
}
我知道onclick我将需要重新连接到数据库(那里没有问题,因为它已经在此代码中),然后运行
DELETE FROM `Warehouse_Maitenence` WHERE `Warehouse_Maitenence`.`ID` = whatever row was selected
但是我不确定如何将所有内容放在一起,因为我没有提交表单。
仅适用于php,我会建议您这样做。将此添加到表的列表中
<td>
<form action="delete.php" method="post" enctype="multipart/form-data">
<input type="text" name="id" value="<?php echo $id; ?>" style="display:none;" />
<input type="submit" name="delete" value="Delete" class="btn btn-danger" />
</form>
</td>
然后创建页面调用delete.php
<?php
if(!isset($_SERVER['HTTP_REFERER'])){ //This is to stop direct access to this delete.php page
header('location: /404.php'); //where to be redirected to
exit;
}
session_start();
include("connect.php"); //your database connection declaration page
if(isset($_POST['delete'])){ //name from the form from table
$id = mysqli_real_escape_string($con, $_POST['id']); //id of the row
$variable = $con->query("DELETE FROM tablename WHERE id = '$id'") or die(mysqli_error($con)); //connecting to the db to the table to delete
if($variable == TRUE){
echo"<script>alert('Deletion Completed!');</script>";
echo"<meta http-equiv='refresh' content='0 url=page.php' />"; //where to be redirected to after deleting.
}else{
echo"<script>alert('Error');</script>";
print_r("Error, Lets go back and Try again");
echo"<meta http-equiv='refresh' content='0 url=page.php' />";
}
exit();
}
?>
我希望能够解决您的问题。!