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();
}
?>
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.
Comments