douwen7905 2014-03-28 22:17
浏览 8
已采纳

使用php删除mysql中的行的问题[关闭]

i have one problem that i can't resolve.When i make this code and run it, it always delete last row of mysql database.Please help me guys.Ty :D

I had some problems publishing code so u can find it here jsfiddle.net/xhCLz/
under "javascript" box.

<?php
$con=mysqli_connect("localhost","root","","test");

// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$result = mysqli_query($con,"SELECT * FROM test_mysql");
$postid1 ="1";
while($row = mysqli_fetch_array($result))
  {
    $ime=$row['name'];
    $prezime=$row['lastname'];
    $id_number=$row['id'];
  echo $ime. " " . $prezime. " " .$id_number. "<form action='' method='post'>

<input type='hidden' name='id_number_send' value='<?php echo $id_number?>'/>
<input type='submit' name='send_value' value='delete' />
</form>";

  }
  if(isset($_POST['send_value']))
{

    $delete_row = mysqli_query($con,"DELETE FROM test_mysql WHERE id='$id_number'");
    echo $id_number;

}
  • 写回答

2条回答 默认 最新

  • doujiao2000 2014-03-28 22:23
    关注

    You don't get id from the POST, instead you use the id assign the last time your loop execute.

    You have to get the id of the row you want to delete from the $_POST variable ($_POST['id_number_send']) and sanitise your data before use it in the query to prevent SQL injection (mysqli_real_escape_string).

    you should also do your delete before the loop if you want display all rows without the deleted one.

    if(isset($_POST['send_value']))
    {
        $deleteId = mysqli_real_escape_string($con, $_POST['id_number_send']);
        $delete_row = mysqli_query($con,"DELETE FROM test_mysql WHERE id='$deleteId'");
        echo "ID number: " . $deleteId . " has been deleted.<br><br>";
    }
    $result = mysqli_query($con,"SELECT * FROM test_mysql");
    $postid1 ="1";
    while($row = mysqli_fetch_array($result))
      {
        $ime=$row['name'];
        $prezime=$row['lastname'];
        $id_number=$row['id'];
      echo $ime. " " . $prezime. " " .$id_number. "<form action='' method='post'>
    
    <input type='hidden' name='id_number_send' value='$id_number' />
    <input type='submit' name='send_value' value='delete' />
    </form>";
    
      }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟