Search code examples
phpmysqliprepared-statementmysql-error-1064

error in my SQL syntax, although the data I provide matches


I am trying to delete a certain row from my database based on loginName, userName, and title.

However the following error occurs:

You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near (...)

I have tried deleting only based on loginName, based on userName and based on title, looked up multiple different posts regarding to the problem, but none of them have provided a solution to my problem. I also checked whether the database column names were wrong, which wasn't the case either. I checked whether the includes folder was wrong, or whether I had mistaken the database table cell name, these were all correct, so I am kind of lost right now, and therefore i'm creating this post.

my php script looks like this:

$loginName = $_SESSION['loginName'];
$userName = $_SESSION['userName'];
$id = $_POST['id']; //this is for the hiddenTitle 
include_once 'dbConn.php';
$sqli = "DELETE * FROM posts WHERE loginName = ? AND name = ? AND hiddenTitle = ?";
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt, $sqli)) {
    var_dump($stmt);
} else {
    mysqli_stmt_bind_param($stmt, 'sss', $loginName, $userName, $id);
    mysqli_stmt_execute($stmt);
}

this is how I submit my data:

function deletePostBtn(del) {
console.log(del.id); //this works
let id = del.id;
let data = {id: id};
$.ajax({
    url: 'deletePost.php',
    type: 'POST',
    data: data,
    success: function(response) {
        console.log(response); //sends back the error
    }
});
}   

So what I am trying to achieve: Delete the row where loginName = $_POST['loginName'], name = $_POST['userName'] and hiddenTitle = $_POST['id']


Solution

  • You have DELETE * FROM .... You should have DELETE FROM ...