Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP和SQL删除表项_Php_Sql_Sql Delete - Fatal编程技术网

使用PHP和SQL删除表项

使用PHP和SQL删除表项,php,sql,sql-delete,Php,Sql,Sql Delete,因此,我创建了一个表单,它接收条目并将它们存储在一个名为Warehouse_Maitenence的SQL数据库中(是的,我知道这是错误的,我是故意这样做的)。这组代码的目标是连接到数据库并相应地显示表 <?php include("nav.php") ?> <body style='background-color:#C0C0C0'> <style> .top{ text-align: Left; } .Headers_Inputs{ pad

因此,我创建了一个表单,它接收条目并将它们存储在一个名为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需要重新连接到数据库(这里没有问题,因为它已经在代码中了),然后运行

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>


在你的
deleteRow
函数中使用Ajax调用,并将要删除的ID传递给你使用Ajax调用的delete函数。老实说,我还没有专门使用Ajax,我几乎不习惯SQL语言我明天将对此进行测试,但这很有意义,您可以操纵基本代码,在每行的末尾生成一个表单,并将表单推送到包含删除所需ID变量的位置。然后如果我想让用户重新加载原始页面(因为这是一个会话,所以我只在代码末尾使用header。所以我不得不做了一些相当大的修改,以使其生效,但如果有人知道如何阅读您提供的内容。我确实调整了您的代码,通过在表创建中添加另一个带有表名的输入框,使其更具动态性。值得思考,谢谢您这是dr让我发疯。我很高兴能帮助你警告:你对参数化的预处理语句非常开放,应该使用参数化的预处理语句,而不是手动生成查询。它们由或提供。永远不要相信任何类型的输入!即使你的查询只由受信任的用户执行。使用
die(mysqli_error($conn))是一个非常糟糕的主意;
在您的代码中,因为它可能会泄漏敏感信息。有关详细说明,请参阅本文:
<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>
    <?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();
}
?>