2017-10-21 158 views
-6

删除记录所以我希望能够利用表格中各列旁边的一个按钮来删除记录的PHP。我无法在任何地方找到一个很好的教程,可以帮助我解决问题,而且我很困难,不知道该怎么做。我如何在PHP MySQL数据库

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

<!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学校的教程,我尝试了面向对象和程序方法,但他们都给了我同样的错误。我真的更喜欢一个单独的delete.php文件,但我对任何工作都很好。

+1

https://开头计算器。 com/search?q =删除+记录+ PHP –

回答

0

创建,通过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 /预处理语句,或者通过一个框架,可以自动做这一切为您服务。


最后:做事情的“香草PHP方式”使得维护和测试代码非常困难。我建议你看看:

  • Laravel(或任何其他现代PHP框架)
  • 的REST API
  • 面向对象的PHP开发
  • PHP作曲
+0

以及如何创建删除条目的脚本ID –

+0

@AsadKhan - 更新了我的答案 –

+0

我不知道为什么,但它仍然不起作用 –