Search code examples
phphtmlmysqlbuttonsql-delete

Deleting Row with clicking on button PHP PDO (using Pagination) and MySQL


When im clicking the Delete-Button nothing changes and the Record will not be deleted. Where im Doing wrong? I tried some solutions but couldnt solve it still. Would be nice if i could get help.

  //thats the indexcards.php  


  while ($row = $sql->fetch(PDO::FETCH_ASSOC)){ 
    echo'<tr>
    <td>'.$row["ModuleID"] .'</td>

    <td>'.$row["ModuleName"]. '</td> 

    <td>'.$row["IndexcardSuggestionID"]. '</td> 

    <td>'.$row["FrontContent"]. '</td>

    <td>'.$row["BackContent"]. '</td> 

    <td> <a href=../Delete/delete.php?id=". row[\"IndexcardSuggestionID\"]. "\'>DELETE</a></td>

    <td><input type="button" name="accept" value="Accept"></td>

    <td><input type="button" name="conditionally" value="Conditionally"></td> 

    </tr>';



   // And thats the delete.php


    <?php

session_start();

include_once '../../config/connection.php';

$database = new Database();
$db = $database->getConnection();

$id = $_GET['id'];



 $sql = "DELETE FROM IndexcardSuggestions WHERE IndexcardSuggestionsID = $id";


 header('Location: ../Suggestions/indexcards.php');

  ?>

Solution

  • You are not passing ID properly. Note that you can not use PHP variables within single quotes, everything is considered as a string. Also, as noted in the comment, your code is vulnerable to MySQL injection.

    echo'<tr>
            <td>'.$row["ModuleID"] .'</td>
    
            <td>'.$row["ModuleName"]. '</td>
    
            <td>'.$row["IndexcardSuggestionID"]. '</td>
    
            <td>'.$row["FrontContent"]. '</td>
    
            <td>'.$row["BackContent"]. '</td>
    
            <td> <a href=../Delete/delete.php?id=".' . $row["IndexcardSuggestionID"]. '"\'>DELETE</a></td>
    
            <td><input type="button" name="accept" value="Accept"></td>
    
            <td><input type="button" name="conditionally" value="Conditionally"></td>
    
            </tr>';