【问题标题】:How do I delete a record in php MySQL Database [duplicate]如何删除php MySQL数据库中的记录[重复]
【发布时间】:2018-04-02 18:28:22
【问题描述】:

所以我希望能够使用表格中每一行旁边的按钮删除 PHP 中的记录。我在任何地方都找不到可以帮助我解决问题的好教程,而且我真的被困住了,不知道该怎么办。

如果有人可以告诉我该怎么做,这是我的代码:

<!DOCTYPE html>
<html>
<head>
<title>PCSS Grad Gown</title>
<link rel="stylesheet" type="text/css" href="Grad_Gown_Report.css">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script 
src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
 <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js">
</script>

<style>

  body {
      background-image: url("http://www.miguelmontalban.com/wp-content/uploads/2015/11/uSr9bZA-fancy-background-images.jpg");
  }

  td {
      color: white;
  }

  .Grad_Table {
      color :white;
  }
 </style>
</head>
<body>

<?php
    $servername = "Private";
    $username = "Private";
    $password = "Private";
    $dbname = "Private";


    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);

    $order;
} 
   $order = $_GET['order'];
    if (!$order) {
        $order = "name";
    }

    $sql = mysqli_query ($conn, "SELECT * FROM Information ORDER By $order");

    ?>

    <div class="table-responsive" id="grad_table">

        <br>
        <br>

    <table class="Grad_Table" width="100%" border="12px">
        <thead>
            <tr>
                <td><a href="Grad_Gown_Report.php">Name:</a></td>
                <td><a href="Grad_Gown_Report_2.php">Student #:</a></td>
                <td>Homeform #:</td>
                <td>Unit:</td>
                <td>Height:</td>
                <td>Size:</td>
                <td></td>



            </tr>

        </thead>

        <tbody>
            <?php

            while ($rows = mysqli_fetch_assoc($sql)) {
            ?>

            <tr>
    <td><?php echo $rows['Name'];?></td>
    <td><?php echo $rows['Studentnum'];?></td>
    <td><?php echo $rows['Homeform'];?></td>
    <td><?php echo $rows['Unit'];?></td>
    <td><?php echo $rows['Height'];?></td>
    <td><?php echo $rows['Size'];?></td>









            </tr>

            <?php


            }


            ?>
        </tbody>


    </table>

    </div>




</div>
</body>
</html>

我可以完美地从数据库中检索数据,其他一切都完全按照我的意愿运行。我唯一需要的是删除功能。我尝试了很多东西,包括 W3 Schools 的教程,我尝试了面向对象和过程方法,但它们都给了我同样的错误。我真的更喜欢单独的 delete.php 文件,但我对任何可行的东西都很好。

【问题讨论】:

标签: php mysql sql database html-table


【解决方案1】:

创建一个按 ID 删除条目的 PHP 脚本,然后在创建表格行时,输出一个指向具有给定 ID 的脚本的链接。

// Using your code as a reference
while($rows = mysqli_fetch_assoc($sql)) : ?>
  <tr>
    <td>
      <a href="/somepath/delete_entry.php?entry_id=<?php echo $row['Id']; ?>">Delete Entry</a>
    </td>
  </tr>
endwhile;

删除脚本如下所示:

<?php
  // Perform any user auth here ...

  // Connect to mysql here ...

  if(!isset($_GET['entry_id'])) {
    die('You need to provide an ID');
  }

  $id = $_GET['entry_id'];

  // Ensure the ID is an integer or an int-like string
  if(!ctype_digit("$id")) {
    die('Invalid ID provided');
  }

  // We've verified that if $id is a string, it is an int-like string; otherwise, it is actually an integer.
  // Let's convert it to an actual integer
  $id = (int) $id;

  // At this point, if you have users and users own this resource, you might check to make sure they actually have permission to delete the requested resource.

  $query = "DELETE FROM Information WHERE Id = $id;";
  $mysqli->query($query);

  // If this is a rest API, you can just end with HTTP 200 (let the script die); 
  // otherwise, redirect them to the page they should see after performing this action
  header('location: /view_something.php');

但您需要小心 - 我在您的代码中注意到您正在接受用户输入并将其直接输入到您的 SQL 查询中。这使得使用SQL injection attacks 定位您的应用程序变得容易。如果攻击者愿意,一个特制的 HTTP 请求可能会删除您的整个数据库。

简单来说,您需要在将用户输入的字符串放入查询之前对其进行验证和转义,以确保他们不会添加自己的 SQL 来执行您的应用程序授权下的其他任务。

当您使用mysqli 时,您需要查看mysqli_real_escape_string()

您可能会查找 PDO /prepared statements,或者可能采用自动为您完成所有这些工作的框架。


最后:以“普通 PHP 方式”做事使维护和测试代码变得非常困难。我鼓励您查看:

  • Laravel(或任何其他现代 PHP 框架)
  • REST API
  • 面向对象的 PHP 开发
  • PHP 编写器

【讨论】:

  • 以及如何创建按 ID 删除条目的脚本
  • @AsadKhan - 更新了我的答案
  • 我不知道为什么,但它仍然无法正常工作
  • 您遇到的错误是什么?您是否修改了我的代码以适合您的应用程序?
  • 当我点击删除按钮时,它会将我带到我设置模具 ID 的页面
猜你喜欢
  • 2010-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-01
  • 2011-10-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多