MYSQL PDO Prepared statement UPDATE doesn't update [duplicate]

Tags: php mysql pdo
By : An00bi

i've a little problem with the UPDATE statement. I've read some topics on stackoverflow and others websites but i can't figure out why I'm not able to update my row.

Actually i'm trying to build a little version of Reddit (just for practice) and this script has to change data inside a row (classic edit post page). Thats my code:

$topic_id = $_GET['id'];

    try {
        include ('../script/db_connect.php');
        $conn = new PDO("mysql:host=$db_servername;dbname=$db_name", $db_user, $db_pass);

        $stmt = $conn->prepare("UPDATE topic SET title = :title, text = :text, category = :category WHERE id = :topic_id");
        $utitle = $_POST["title"];
        $utext = $_POST["myTextArea"];
        $category = $_POST["xxx"];

        $stmt->bindParam(':title', $utitle);
        $stmt->bindParam(':text', $utext);
        $stmt->bindParam(':category', $category);


            $conn = null;
            header("Location: ../index.php");
    catch(PDOException $e){
        echo "Error: " . $e->getMessage();

I don't have any errors so must be something with the update statement but i can't find what's the problem.

EDIT: Marked as duplicate by "Your Common Sense", what? Can you show me why and how this post is a "duplicate"?

I just cant figure out how resolve my problem and you just marked that as duplicate instead of help, ok.

By : An00bi

If PDO statement returns FALSE, it means that query failed. You have to set PDO in the proper error reporting mode to be aware of the error.

Put this line in your code right after connect


After getting the error message, you have to read and comprehend it. It sounds too obvious, but learners often overlook the extreme helpfulness of the error message. Yet most of time it explains the problem pretty straightforward. Say, if it says that particular table doesn't exist, you have to check spelling, typos, letter case, credentials and such. Or, if it says there is an error in SQL syntax, then you have to examine your SQL. And the problem spot is right before the query part cited in the error messaage.

You have to also trust the error message. If it says that number of tokens doesn't match number of bound variables then it is so. Same goes for absent tables or columns. Given the choice, whether it's your own mistake or the error message is wrong, always stick to the former. Again it sounds condescending, but hundreds of questions on this very site prove this advise extremely useful.

Note that in order to see PDO errors, you have to be able to see PHP errors in general. To do so, you have to configure PHP depends on the site environment:

  • on a development server it is very handy to have errors right on the screen, for which displaying errors have to be turned on:

  • while on a live site, all errors have to be logged, but never shown to the client. For this, configure PHP this way:

    ini_set('display_errors', 0);
    ini_set('log_errors', 1);

Note that error_reporting should be set to E_ALL all the time.

Also note that despite the common delusion, no try-catch have to be used for the error reporting. PHP will report you PDO errors already, and in a way better form. An uncaught exception is very good for development, yet if you want to show a customized error page, still don't use try catch for this, but just set custom error handler. In a nutshell, you don't have to treat PDO errors as something special but regard them as any other error in your code.

This video can help you solving your question :)
By: admin