Skip to content
Advertisement

DELETE rows with prepared query in PHP and MySQL [closed]

This is a part of a webservice call to delete a record in MySQL database But I am getting an error saying “Database Error. Couldn’t delete post!” where is the error in my code.

 <?php
        
            //load and connect to MySQL database stuff
            require("config.inc.php");
            
            //initial query
            $query = 'DELETE FROM messages WHERE id =? AND receiver =?';
            $query_params = array($_GET['id'], $_GET['receiver']);
            //execute query
            try {
                $stmt   = $db->prepare($query);
                $result = $stmt->execute(array());

                $response["success"] = 1;
                $response["message"] = "Post Successfully DELETED!";
                echo json_encode($response);        
            }
                
            catch (PDOException $ex) {
                $response["success"] = 0;
                $response["message"] = "Database Error. Couldn't delete post!";
                die(json_encode($response));
            }
        ?> 

UPDATE:

$stmt->execute(array()) should be replaced to $stmt->execute($query_params) the params are missing

Advertisement

Answer

You have prepared the parameters for you statement, but you never actually use them with the statement.

Try changing:

$result = $stmt->execute(array());

to:

$result = $stmt->execute($query_params);
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement