能够通过单击页面上的按钮删除数据,这也会从数据库中删除数据

Being able to delete data from the click of a button on a page which also deletes the data from the database

我的网页上的 table 中有多个文本框,这些文本框是根据我网站用户填写的表单填充的。我的功能是能够删除每一行以及编辑我网站上显示的每一行数据。我遇到的问题是 table 的最后一行只能是 edited/deleted。例如,当我单击 table 第一行的删除按钮时,它出于某种原因删除了最后一行而不是第一行。还有,和update/edit按钮一样,只能修改最后一行,不能修改我网站上table最后一行以上的任何内容。

更多信息: form_id 是我数据库中的主键。

我的代码:

 <?php
  $con = @mysql_connect("localhost","root","");
  if (!$con){
  die("Can not connect: " . mysql_error());
  }
  mysql_select_db("formsystem", $con);

  if(isset($_POST['update'])){
    $UpdateQuery = "UPDATE form SET form_name='$_POST[name]', form_description='$_POST[description]' WHERE form_id='$_POST[hidden]'";               
    mysql_query($UpdateQuery, $con);
};

if(isset($_POST['delete'])){
$DeleteQuery = "DELETE FROM form WHERE form_id='$_POST[hidden]'";          
mysql_query($DeleteQuery, $con);
};

  $sql = "SELECT * FROM form";

  $myData = mysql_query($sql,$con);

   echo "<table>
   <tr>
   <th>Title</th>
   <th>Description</th>
   <th></th>
   <th></th>
   <th></th>
   </tr>";
   while($record = mysql_fetch_array($myData)){
   echo "<form action=findGroup.php method=post>";
   echo "<tr>";
   echo "<td>" ."<input type=text name=name value='" . $record['form_name'] . "'/> </td>";
   echo "<td>" ."<input type=text name=description value='" . $record['form_description'] . "'/> </td>";
   echo "<td>" ."<input type=hidden name=hidden value='" . $record['form_id'] . "'/></td>";
   echo "<td>" ."<input type=submit name=update value='update" . "'/> </td>";
   echo "<td>" ."<input type=submit name=delete value='delete" . "'/> </td>";
   echo "</tr>";
  }
  echo "</table>";

?>

首先,检查这些潜在问题:

  1. 您正在以根用户身份连接。不建议。您应该以 MySQL 用户身份连接,对该 table 拥有 M.A.D 权限(修改、添加、删除)。

  2. 您是否检查了 MySQL 和 system/PHP 日志以查看是否报告了任何错误?然后你可以根据这些错误调整你的代码。

  3. 您是否尝试过 运行 手动删除语句以确认它删除了所需的行?

  4. 在您的代码中,您是否尝试过在删除语句中使用 $sql = DELETE... 语法?

更新

正确包含 form 元素:

<?php
  $con = @mysql_connect("localhost","root","");
  if (!$con){
    die("Can not connect: " . mysql_error());
  }
    mysql_select_db("formsystem", $con);

    if(isset($_POST['update'])){
      $UpdateQuery = "UPDATE form SET form_name='".$_POST['name']."', form_description='".$_POST['description']."' WHERE form_id='".$_POST['hidden']."';";               
    mysql_query($UpdateQuery, $con);
  };

  if(isset($_POST['delete'])){
    $DeleteQuery = "DELETE FROM form WHERE form_id='".$_POST['hidden']."';";          
    mysql_query($DeleteQuery, $con);
  };

  $sql = "SELECT * FROM form";

  $myData = mysql_query($sql,$con);

  echo "<table>
   <tr>
   <th>Title</th>
   <th>Description</th>
   <th></th>
   <th></th>
   <th></th>
   </tr>";
   while($record = mysql_fetch_array($myData)){
     echo "<form action=findGroup.php method=post>";
     echo "<tr>";
     echo "<td>" ."<input type=text name=name value='" . $record['form_name'] . "'/> </td>";
     echo "<td>" ."<input type=text name=description value='" . $record['form_description'] . "'/> </td>";
     echo "<td>" ."<input type=hidden name=hidden value='" . $record['form_id'] . "'/></td>";
     echo "<td>" ."<input type=submit name=update value='update" . "'/> </td>";
     echo "<td>" ."<input type=submit name=delete value='delete" . "'/> </td>";
     echo "</tr>"
     echo "</form>";
   }
  echo "</table>";

?>

出于安全考虑,最好使用mysqli_real_escape_string包装变量,例如:

"DELETE FROM form WHERE form_id='".mysqli_real_escape_string($_POST['hidden'])."';";

但这是另一个问题,这里是thread.