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

ene's avatar
Level 2

Trying to update post data in php pdo

Am trying to update post after filling the form and clicking on update nothing is been sent to the database and I think my query is good but the form I doubt, am new to php pdo so please assist me on this guys

<?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";

$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();


            <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>

I try editing the form all I noticed after clicking on update the previous data will also get wipe from the database but the post_id isn't affected at all
0 likes
4 replies

Please or to participate in this conversation.