简体   繁体   English

从MYSQL表删除PHP表单不删除右行

[英]PHP form delete from MYSQL table dont delete the right row

I have a problem with my delete form. 我的删除表单有问题。 When you press the delete button it will always delete the last inserted row instead of the row you want to delete. 当您按下删除按钮时,它将始终删除最后插入的行,而不是您要删除的行。 I am using a jQuery Ajax SendForm function to do the update on the div when you press delete. 当您按Delete时,我正在使用jQuery Ajax SendForm函数在div上进行更新。 It worked before adding that function. 在添加该功能之前,它可以正常工作。 It's only delete that doesn't work with the SendForm; 只是删除无法与SendForm一起使用; You can do updates and it works just fine. 您可以进行更新,并且效果很好。

<?php     

  foreach ($pdo->query( 'SELECT * FROM Commenta order by commentID desc;'  ) as $row) :
      $luck = $row ['commentID'];
      echo "<tr>";
        echo "<td><i class='fa fa-eye w3-blue w3-padding-tiny'></i></td>";
        echo "<td>".$row['alias']."</td>";
        echo "<td>";
        foreach($pdo->query( 'select realName from SubPlace, CommentSubPlace where SubPlace.name = CommentSubPlace.name and CommentSubPlace.commentID = '.$luck.';' ) as $brow){;
          echo $brow['realName']."</br>";
        };
        echo "</td>";
        echo "<td>".$row['grade']." / 5 </td>";
        echo "<td>".$row['kommentar']."</td>";
        echo "<td>".$row['date']."</td>"; 
        ?>    

        <td class="comment-delete">
          <form id="cucdel">
            <input type="hidden" name="commentID" value="<?php echo $row['commentID']; ?>">
            <button type="button" onclick="SendForm('comments', 'comments', 'cucdel');">radera</button>
          </form>
        </td>
      </tr>

    <?php
  endforeach;
?>

The deletion portion of the code: 代码的删除部分:

<?php
  if (isset($_POST['commentID'])) {
    $sql = "DELETE FROM Commenta WHERE commentID = :commentID";
    $stmt = $pdo->prepare($sql);
    $stmt->bindParam(':commentID', $_POST['commentID'], PDO::PARAM_INT);
    $stmt->execute();
  }
 ?>

This line 这条线

foreach($pdo->query( 'select realName from SubPlace, CommentSubPlace where SubPlace.name = CommentSubPlace.name and CommentSubPlace.commentID = '.$luck.';' ) as $brow){;

while not a syntax error - has no purpose in its semi colon. 虽然不是语法错误-分号没有用。 the line can be made much more clear to you and programmers behind you if changed to: 如果更改为以下内容,则可以使您和后面的程序员更清楚地了解这一行:

// protect yourself from bad data
$luckint = (integer) $luck; 

// lose the confusing and unnecessary semi-colon here
// prepare the query separately
$query = 'select realName from SubPlace, CommentSubPlace where SubPlace.name = CommentSubPlace.name and CommentSubPlace.commentID = '.$luckint;  

// this line *can not* end in a semi-colon - find your error log
foreach ($pdo->query( $query ) as $brow) { 

answer to question 回答问题

the reason it's always deleting the last one is the form id . 它总是删除最后一个的原因是表单id you are always printing the same id to every delete form. 您总是在每个删除表单上打印相同的ID。 the id must be unique - make the form id like this: 该ID必须是唯一的-使表单ID像这样:

<form id="cucdel<?php echo $row['commentID']; ?>">
 <input type="hidden" name="commentID" value="<?php echo $row['commentID']; ?>">
 <button type="button" onclick="SendForm('comments', 'comments', 'cucdel<?php echo $row['commentID']; ?>');">radera</button>
</form>

and then update the 然后更新

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

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