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

时间:2018-04-02 11:21:42

标签: php html button edit sql-delete

我的网页上的表格中有多个文本框,这些文本框填写在用户填写的网站表格中。我的功能是能够删除每一行以及编辑我网站上显示的每一行数据。我遇到的问题只是可以编辑/删除表格的最后一行。例如,当我单击表格第一行上的删除按钮时,它会因某种原因而不是第一行删除最后一行。此外,它与更新/编辑按钮相同,只能修改最后一行,而不是我网站上表格最后一行之上的任何内容。

更多信息: 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>";

?>

2 个答案:

答案 0 :(得分:1)

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

  1. 您正在以root身份进行连接。不建议。您应该以MySQL用户身份连接该表上的M.A.D权限(修改,添加,删除)。

  2. 你检查过MySQL&amp;系统/ PHP日志以查看是否报告了任何错误?然后,您可以根据这些错误调整代码。

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

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

答案 1 :(得分:1)

<强>更新

正确封闭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