Search code examples
phpcontent-management-systemsql-deletedelete-row

Why doesn't my delete function work?


I followed a tutorial on Youtube about how to make a CMS system: http://y2u.be/QNxU3Qa6QZs

And i'm stuck at making the delete function, it just refreshes the page without actually deleting something. Can someone help me with this problem?

This is the code i have for the delete page:

<?php 

session_start();

include_once('../includes/connection.php');
include_once('../includes/article.php');

$article = new Article;

if (isset($_SESSION['logged_in'])) {
    if(isset($_GET['id'])) {

        $id = $GET['id'];

        $query = $pdo->prepare('DELETE FROM articles WHERE article_id = ?');
        $query->bindValue(1, $id);
        $query->execute();

        header('Location: delete.php');
    }

$articles = $article->fetch_all();

 ?>
<html>

<head>
    <title> Blog </title>
    <link rel="stylesheet" href="../assets/style.css" />
</head>

<body>
    <div class="container">
        <a href="index.php" id="logo">CMS</a>
        <br><br>

        <h4>Select an Article to delete </h4>

        <form action="delete.php" method="get">

            <select onchange="this.form.submit();" name="id">

                    <?php foreach ($articles as $article) { ?>
                    <option value="<?php echo $article['article_id']; ?>"> 
                    <?php echo $article['article_title'];   ?>

                </option>
                    <?php } ?>

                </select>
        </form>


    </div>
</body>

</html>

<?php
} else {
header('Location: index.php'); 
}

?>

If you have any questions about the code, i'll answer them.

I hope you'll help me find a solution for this problem.

  • Daniel

Solution

  • You have a typo.

    Change

    $id = $GET['id'];
    

    To

    $id = $_GET['id']; #$_GET is the global array not $GET