PHP form delete from MYSQL table dont delete the right row

lanarn

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. It worked before adding that function. It's only delete that doesn't work with the 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();
  }
 ?>
WEBjuju

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. you are always printing the same id to every delete form. the id must be unique - make the form id like this:

<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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Beginner: Delete row from Table and Database PHP/HTML/MySQL

From Dev

Delete selected row from table in mysql

From Dev

Delete an entire row from a table, using MySQL

From Dev

Delete selected row from table in mysql

From Dev

jQuery onclick delete row from mysql table

From Dev

PHP button delete row from table not working

From Dev

Delete row from database with a button in table (PHP)

From Dev

MySQL Delete row with PHP

From Dev

How to delete a row from mysql in php

From Dev

how to delete the specified row from html table and also in mysql table using php

From Dev

Mysql table replace or delete row

From Dev

Mysql table replace or delete row

From Dev

mySQL - Can't delete table row with PHP (not showing an error)

From Dev

Delete mysql Table row by clicking submit button php

From Dev

Delete a row from Parse Table

From Dev

Delete a row from sqlite table

From Dev

Delete row from dynamic table

From Dev

Error Delete row from php

From Dev

Delete row from a XML in PHP

From Dev

Delete right row from a table model that is sorted differently than the table view

From Dev

How to delete data from html form php mysql

From Dev

How to delete data from html form php mysql

From Dev

MySQL delete data from a table

From Dev

MySQL delete data from a table

From Dev

MySql delete from other table

From Dev

Delete row from table conditional on a related table

From Dev

Multiple Delete from table in PHP

From Dev

Delete all rows from MySQL table using PHP

From Dev

delete a specific row of a table using php