无法在php中使用此语句从mysql中删除记录

时间:2017-05-23 06:17:46

标签: php mysql

我在删除记录时遇到问题。当我点击删除它通过动作没有错误出现,但记录仍然存在。我不确定是什么问题。

的index.php

    //connect to database with PDO 
  $db = new PDO ('mysql:host=localhost;dbname=database_here;charset=utf8mb4', 'username_here','password_here');

<div class="container">
        <h2 class="panel panel-primary show_hide_e">E</h2>
        <!--Create a query to find all records starting with a and loop through them and show them bellow-->
        <?php $sql = "SELECT id, title, type, price, image, description, date FROM collection where title RLIKE '^[E]' ORDER BY type || title asc";
        $stmt->execute();

        ?><?php //start of the while loop ?>
        <?php while( $row = $stmt->fetch(PDO::FETCH_ASSOC) ) 
     { 
        $image_id   = $row['image'];
        $id = $row['id'];
        echo "<h4 class='slidingDiv_e'>". 
                '<img src="https://localhost/assets/images/'.$image_id.'" width="84" height="104" />'.
                "<br>".
                $row["type"].

                " - Title:". $row["title"]. 

                " / ".

                $row["description"]. 

                " / Price: ". $row["price"].

                " / Date: ".  $row["date"].

                "<br>".
                "<a class='btn btn-primary' href='https://localhost/update.php?id=$id'>Update</a>". 
                ' '. 
                "<a class='btn btn-danger' href='https://localhost/delete.php?id=$id'>Delete</a>".
                '<hr>'.
            "</h4>";   
     }

?>
</div>

delete.php

//connect to database with PDO 
  $db = new PDO ('mysql:host=localhost;dbname=database_here;charset=utf8mb4', 'username_here','password_here');

//delete from database
$stmt=$db->prepare("DELETE FROM collection WHERE id=:id");
$stmt->bindParam(":id",$id,PDO::PARAM_INT);
$stmt->execute();

1 个答案:

答案 0 :(得分:0)

$stmt=$db->prepare("DELETE FROM collection WHERE id=:id");
$stmt->bindParam(":id",$_GET['id'],PDO::PARAM_INT);
$stmt->execute();

试试这个