【问题标题】:when i refresh will delete all records当我刷新将删除所有记录
【发布时间】:2016-02-22 19:26:55
【问题描述】:

当我单击 drop 时,我遇到了功能无法正常工作的问题,所以这里有什么问题 我只需要在单击 drop 时删除记录,所以请提供任何帮助 函数 drop 中的查询是否正确?

     <?php 
     $sql="SELECT * FROM administrators";
      $record=mysql_query($sql);

    function drop($id,$email){
  $q="DELETE FROM administrators WHERE id='$id' AND email='$email'";
  mysql_query($q);
  }
  ?>
    <title>Admins Table</title>
    <style>
   table, th, td {
    border: 2px solid black;
    border-collapse: collapse;
    }  
  #creating {
   font:bold;
   font-size:1.6em;
   }
  </style>
  <a href='cadmin.php'  id='creating'>Create New Admin</a><br/><br/>
  <table width="920" border="1" cellpadding="1" cellspacing="1">
  <tr>
  <th>Number</th>
  <th>First Name</th>       
  <th>Last Name</th>
  <th>Phone Number</th>
  <th>Gender</th>
  <th>Email</th>
  <th>Address</th>
  <th>Update</th>
  <th>Delete</th>
  </tr>


  <?php

     while($row=mysql_fetch_assoc($record)){
echo'<tr>';
echo'<td>'.$row['id'].'</td>';
echo'<td>'.$row['first_name'].'</td>';
echo'<td>'.$row['last_name'].'</td>';
echo'<td>'.$row['phone'].'</td>';
echo'<td>'.$row['gender'].'</td>';
echo'<td>'.$row['email'].'</td>';
echo'<td>'.$row['address'].'</td>';
echo'<td>'."<a href='' onclick=''>Edit</a>".'</td>';
echo'<td>'."<a href='' onclick='".drop($row['id'],$row['email'])."'>    Drop</a>".'</td>';
 echo'</tr>';
  }

  ?>

 </table>

   //code here

【问题讨论】:

  • 我推荐使用mysqli
  • 我推荐使用 PDO ;)
  • 糟糕的 HTML 和 PHP 代码。发现很多错误。您不能只通过onclick 调用 PHP 函数。请了解服务器端脚本和客户端脚本的区别。
  • @joee 见this tutorial

标签: php html


【解决方案1】:

试试这个代码

 <?php 
$sql="SELECT * FROM administrators";
$record=mysql_query($sql);


function drop($id,$email){
$q="DELETE FROM administrators WHERE id='$id' AND email='$email'";
mysql_query($q);
header("Refresh:0");
}
if (isset($_GET['drop'])) {
  $id=$_GET['id'];$email=$_GET['email'];
drop($id,$email);
}
?>
<title>Admins Table</title>
<style>
table, th, td {
border: 2px solid black;
border-collapse: collapse;
}  
#creating {
font:bold;
font-size:1.6em;
}
</style>
<a href='cadmin.php'  id='creating'>Create New Admin</a><br/><br/>
<table width="920" border="1" cellpadding="1" cellspacing="1">
<tr>
<th>Number</th>
<th>First Name</th>       
<th>Last Name</th>
<th>Phone Number</th>
<th>Gender</th>
<th>Email</th>
<th>Address</th>
<th>Update</th>


   <th>Delete</th>
  </tr>
 <?php

  while($row=mysql_fetch_assoc($record)){
   echo'<tr>';
   echo'<td>'.$row['id'].'</td>';
   echo'<td>'.$row['first_name'].'</td>';
   echo'<td>'.$row['last_name'].'</td>';
   echo'<td>'.$row['phone'].'</td>';
   echo'<td>'.$row['gender'].'</td>';
   echo'<td>'.$row['email'].'</td>';
   echo'<td>'.$row['address'].'</td>';
   echo'<td>'."<a href='' onclick=''>Edit</a>".'</td>';
    echo'<td>'."<a href='?     drop=ss&id=".$row['id'].'&email='.$row['email']."'>              Drop</a>".'</td>';
 echo'</tr>';
 }

?>

【讨论】:

    猜你喜欢
    • 2011-11-06
    • 2014-05-11
    • 1970-01-01
    • 2018-07-02
    • 1970-01-01
    • 2011-04-13
    • 2014-02-18
    • 1970-01-01
    • 2014-12-07
    相关资源
    最近更新 更多