删除脚本 PHP MySQL

Delete Script PHP MySQL

我是 PHP MySQL 的初学者,我只想知道如何为每个特定记录创建 DELETE 函数。

这是我 SELECT 记录时的代码。我不知道我将如何插入 DELETE 函数以及代码结构如何。

$res=mysql_query("SELECT * FROM accounts WHERE user_id=".$_SESSION['user']);
$userRow=mysql_fetch_array($res);


$sql = "SELECT * FROM accounts ORDER BY agentFname ASC";

 if($result = mysql_query($sql)){
     if (mysql_num_rows($result) > 0 ) {



             <table id="example" class="table table-striped responsive-utilities jambo_table">

                 <thead>

                      <tr class="headings">

              <?php           

                   echo "<th>ID</th>"; // Primary Key
                   echo "<th>Agent Code</th>"; // Unique Key
                   echo "<th>First Name</th>";  
                   echo "<th>Middle Name</th>";    
                   echo "<th>Last Name</th>";     
                   echo "<th>Contact Number</th>";     
                   echo "<th>Address</th>";     
                   echo "<th>Gender</th>";
                   echo "<th>User Type</th>"; 
                   echo "<th>Action</th>";        
                   echo "</tr>"; 
                   echo "</thead>"; 

           }  while($row = mysql_fetch_array($result))  {

               echo"<tbody>";
               echo " <tr class=\"even pointer\">"; 

               echo "<td>" .$row['user_id']. "</td>";  // Primary Key
               echo "<td>" .$row['agentCode']. "</td>";  // Unique Key
               echo "<td>" .$row['agentFname']. "</td>";
               echo "<td>" .$row['agentMname']. "</td>";
               echo "<td>" .$row['agentLname']. "</td>";
               echo "<td>" .$row['agentContact']. "</td>";
               echo "<td>" .$row['agentAddress']. "</td>";
               echo "<td>" .$row['agentGender']. "</td>";
               echo "<td>" .$row['user_type']. "</td>";
               echo "<td> <a href=\"#.php\"> Delete</a></td>"; // DELETE Function
               echo "</tr>";
                }

            }

看起来你正在尝试做类似

的事情
echo "<td><a href="/yourfile.php?delete=<?php echo $row['user_id'];?>"> Delete</a></td>

然后在 yourfile.php 您将处理删除并重定向回原始页面,但是...

即使您误点击了该记录,也会删除该记录。

你可以在那里制作小表格,每个表格都有一个复选框,或者有一些 js 要求先确认删除...

只需将 href 中的用户 ID 与文件名一起传递即可。

echo "<td> <a href="yourfile.php?id=<?php echo $row['user_id'];?>"> Delete</a></td>";

然后创建您的文件。 例如你的 filename.php

<?php

$id=$_GET['id'];

$sql=mysql_query("DELETE FROM tablename where user_id='$id'");

if($sql > 0)
{
  echo "record successfully deleted";
}
else
{
 echo "errore in deleting";
}

?>