Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ene's avatar
Level 2

Unable to update post in php pdo

I have been struggling with this , I couldn't get the query to , when I click the link to the post I have getting the id of the post, but whenever I add that's update query I always end up with error 500 work please kindly assist I will so much appreciate

<?php
if(!isset($_GET['id'])) {
    header('Location: question.php');
    exit();
} else {
    $id = $_GET['id'];
}
//include database connection
   require_once('./includes/connection.php');
if(!is_numeric($id)) {
    header('Location: question.php');
}
$sql = "UPDATE posts SET title = :title, body = :body, category_id = :category_id WHERE post_id = :post_id";

// Prepare the query

$query = $db->prepare($sql);
$query->bindParam(':post_id', $id);
$query->execute();

?>
<?php include 'header.php';?>
<div><?php echo $id ?>
 
    
<?php include 'footer.php';?>
0 likes
6 replies
ene's avatar
Level 2

@Sinnbeck this is my first working with vanilla php I don't know how to get the error , just getting blank error 500

ene's avatar
Level 2

@Sinnbeck I check the php error log I got this

#0 /home/nairamar/domains/pals.com.ng/public_html/edit.php(33): PDOStatement->execute()
#1 {main}
  thrown in /home/nairamar/domains/palg/public_html/edit.php on line 33
2022-12-15 15:40:51.416778 [NOTICE] [2689842] [T0] [105.112.188.163:5404-H3:BFCEDE7A777C8766-40#APVH_www.palsng:443] [STDERR] PHP Fatal error:  Uncaught PDOException: SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens in /home/nairamar/domains/pal/public_html/edit.php:33
Stack trace:
#0 /home/nairamar/domains/palg/public_html/edit.php(33): PDOStatement->execute()
#1 {main}
  thrown in /home/nairamar/domains/palg/public_html/edit.php on line 33
thinkverse's avatar

You're only binding one variable but your query contains 4 bound parameters.

$sql = "UPDATE posts SET title = :title, body = :body, category_id = :category_id WHERE post_id = :post_id";

$query = $db->prepare($sql);
$query->bindParam(':post_id', $id);
// Missing variables
$query->bindParam(':title', $title);
$query->bindParam(':body', $body);
$query->bindParam(':category_id', $category_id);
$query->execute();
ene's avatar
Level 2

@thinkverse @sinnbeck thanks i got no error again, but when i try to update the post, all i am getting in the database is nothing , even the former post am editing has been cleared but no error

  <div class="px-6 py-6 mx-4 mt-4 text-left bg-white md:w-1/3 lg:w-1/3 sm:w-1/3">
  <form action="edit.php" method="POST">
                <label>Title:</label><input type="text" class="w-full dark:bg-black px-4 py-2 mt-2 border rounded-md focus:outline-none focus:ring-1 focus:ring-blue-600" name="title" />
                <label for="body">Body:</label>
                <textarea class="w-full dark:bg-black px-4 py-2 mt-2 border rounded-md focus:outline-none focus:ring-1 focus:ring-blue-600" name="body" cols=50 rows=10></textarea>
                <label>Category:</label>
                <select name="category">
                    <?php
                        $query = $db->query("SELECT * FROM categories");
                        while($row = $query->fetchObject()) {
                            echo "<option value='".$row->category_id."'>".$row->category."</option>";
                        }
                    ?>
                </select>
                <br>
                <br>
                <input class="w-full  px-6 py-3 mt-4 text-white bg-purple-600 rounded-lg hover:bg-blue-900" type="submit" name='submit' value="Submit" />
            </form>

Please or to participate in this conversation.