Trying to fix button or sql query

Viewed 36

I created a similar question but has too confused, hope to get some help now giving this shortcode. I've tried to change all this code from another answer here at StackOverflow. I have able to make the button works but It just removed posts, not from user_id.

Trying to create a button to remove the post.

Select:

$sth = $db->prepare("
    SELECT 
        * 
    FROM 
        users, profile_post 
    WHERE 
        profile_post.user_id = users.user_id AND
        users.user_id = :id
    ORDER BY `profile_post`.`dateupload` DESC
");

Form:

                echo '<form name="delete" action="" method="post"><button><i class="fa fa-trash"><input type="hidden" name="name" value="' . $row["post"] .'"></i></button></form>';

PHP:

<?php
    if ($_SERVER['REQUEST_METHOD'] === 'POST') {
        if(isset($_POST['delete'])){
                $postid1 = $row["post"];
                $stmt = $db->prepare("DELETE FROM `profile_post` WHERE `profile_post`.`post` = $postid1 ");
                echo("Sucess.");
            }
    }
?>
1 Answers

The problem is you are preparing the query (in the wrong way, because of 2 reasons: $db is undeclared and also the query is incorrect), but you have never bound your parameters and executed it:

$db = new mysqli($HOST,$USER,$PASS,$DATABASE); // these variables should be defined based on the database credentials
$stmt = $db->prepare("DELETE FROM `profile_post` WHERE `profile_post`.`post` = ?;");
$stmt->bind_param("i", $postid1);
$stmt->execute();
Related