如何删除单行使用php


how to delete single row using php

亲爱的请帮帮我
我得到一些错误在我的代码

这是我的view.php页面

<html>
   <body>
      <table  style="border:#333 solid"  border="2">
         <tr>
            <td>ID:</td>
            <td>NAME:</td>
            <td>EMAIL:</td>
            <td>MOBILE:</td>
            <td>EDIT:</td>
            <td>DELETE:</td>
         </tr>
<?php
include_once('connect.php');
$query="select * from emp";
$conn=mysqli_query($con,$query);
if(isset($conn))
{
  echo"This is all data of table.";
}
else
{
  die("query is not execute". mysqli_error());  
}   
 while($row=mysqli_fetch_array($conn))
 {
?>
  <tr>
    <td><?php echo $row['emp_id']; ?></td>
    <td><?php echo $row['emp_name']; ?></td>
    <td><?php echo $row['emp_address'] ?></td>
    <td><?php echo $row['emp_salary'] ?></td>
    <td><a href="edit.php?id=<?php echo $row['emp_id'];?>"><font   color="#FF0000">EDIT</font></a></td>
    <td><a href="delete.php?id=<?php echo $row['emp_id'];?>"><font   color="#FF0000">DELETE</font></a></td>
  </tr>
 <?PHP
 }
 ?>
       </table>
    </body>
 </html>

这是我的delete.php文件:

  <?php 
  include_once('connect.php');
  $id=$_GET['emp_id'];
  $query="delete from emp where emp_id='$id'";
  $del=mysqli_query($con,$query);
  if($del)
  {
    echo"record has been deleted";  
  }
  else
  {
    die("Record is not deleted it is query error.".mysqli_error()); 
  }
  ?>

如何在delete.php文件中访问view.php的ID以及如何在我的db表中删除单行。我不知道如何访问view。php的id。在这个程序中,我没有删除选定的行。请告诉我如何删除数据。

您正在发送edit.php?id=,因此需要使用GET['id']从查询字符串中获取id

$id = $_GET['id'];

修改delete.php中的代码如下

 <?php 
 include_once('connect.php');
 $id=$_GET['id'];
 $query="delete from emp where emp_id='$id'";
 $del=mysqli_query($con,$query);
 if($del)
 {
     echo"record has been deleted";   
 }
 else
 {
     die("Record is not deleted it is query error.".mysqli_error());  
 }
 ?>

还请认识到,在查询中使用$_GET数据只是在自找麻烦,特别是当它没有正确转义时。你正在打开你自己的SQL注入攻击。

如果要使用mysql_*函数,请使用mysql_real_escape_string,请注意它在PHP 5.5中已被弃用。

你也可以选择使用MySQLi或PDO,因为它们都使用预处理语句,如前面的问题所提到的,我如何在PHP中防止sql注入?