简体   繁体   English

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

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

I have multiple text boxes within a table on my web page which is populated from a form on my website users fill out. 我的网页上的表格中有多个文本框,这些表格是根据我网站上用户填写的表格填充的。 I have the feature of being able to delete each row as well as edit each row of data displayed on my website. 我的功能是能够删除每行以及编辑网站上显示的每一行数据。 The problem I'm having with it is only the last row of the table can be edited/deleted. 我遇到的问题是只能编辑/删除表的最后一行。 For example, When I click the delete button on the first row of the table, it deletes the last row for some reason and not the first row. 例如,当我单击表第一行上的删除按钮时,由于某种原因,它将删除最后一行,而不是第一行。 Also, it's the same with the update/edit button, only the last row can be modified and not anything above the last row of the table on my website. 另外,与“更新/编辑”按钮相同,只能修改最后一行,而不能修改我网站上表格最后一行的任何内容。

More information: form_id is the primary key within my database. 详细信息:form_id是数据库中的主键。

My code: 我的代码:

 <?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>";

?>

First off, check these potential issues: 首先,请检查以下潜在问题:

  1. You are connecting as root. 您以root用户身份连接。 Not recommended. 不建议。 You should connect as a MySQL user with MAD rights on that table (modify, add, delete). 您应该以具有该表MAD权限(修改,添加,删除)的MySQL用户身份进行连接。

  2. Have you checked the MySQL & system/PHP logs to see if any errors are being reported? 您是否检查过MySQL和系统/ PHP日志以查看是否报告了任何错误? Then you can adjust your code based on those errors. 然后,您可以根据这些错误调整代码。

  3. Have you attempted to run the delete statement manually to confirm that it deletes the desired row? 您是否尝试过手动运行delete语句以确认它删除了所需的行?

  4. In your code, have you tried using the $sql = DELETE... syntax on your delete statement? 在您的代码中,您是否尝试过在delete语句中使用$ sql = DELETE ...语法?

Update 更新资料

Enclose the form element properly: 正确封装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>";

?>

And for security issue, it's better to wrap variable using mysqli_real_escape_string , for example: 为了安全起见,最好使用mysqli_real_escape_string来包装变量,例如:

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

But this is another question, here is the thread . 但这是另一个问题,这是线程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM