do you have an error in your sql syntax error?

The problem is that you have a rogue mysqli_query() in your code. You need to remove it.

You might have missed it because you have way too much code. You should remove most of it. You really do not need all of it.

Here is what the code should look like:

function createPetition(mysqli $conn, $pName, $pTarget, $pDescription, $userId) {
    $sql = "INSERT INTO petitions (petitionName, petitionTarget, petitionDescription, usersId) VALUES (?, ?, ?, ?);";
    
    $stmt = $conn->prepare($sql);
    $stmt->bind_param("ssss", $pName, $pTarget, $pDescription, $userId);
    $stmt->execute();

    header("location: ./petitionfinal.php?error=none");
    exit();
}

However, having redirection inside of the function is not a good idea. I would strongly recommend to take it out of the function.

function createPetition(mysqli $conn, $pName, $pTarget, $pDescription, $userId) {
    $sql = "INSERT INTO petitions (petitionName, petitionTarget, petitionDescription, usersId) VALUES (?, ?, ?, ?);";
    
    $stmt = $conn->prepare($sql);
    $stmt->bind_param("ssss", $pName, $pTarget, $pDescription, $userId);
    $stmt->execute();
}

// when you call
createPetition($conn, $pName, $pTarget, $pDescription, $userId);

header("location: ./petitionfinal.php?error=none");
exit();

See how much nicer it is. It is so much easier to spot mistakes now.

If you are wondering what about errors then I must point you to How to get the actual mysql error and fix it?

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top