Vandervidi
Vandervidi

Reputation: 295

UPDATE query in MYsqli+PHP doesnt work

im trying to execute an UPDATE query in Mysqli OOP. I know there are many tutorials in google but nothing worked for me :( I will appreciate if someone could write a simple code with a simple query example that will include all neccesery steps begining with creating a connection object.

this is my code, i tried to echo some things to try to find a problem. outputs are OK except the line "echo "test2"; that echo and the code below are not executed..

<?php
       if ((isset($_POST['title'])) && (isset($_POST['post'])))
       {
            $title= $_POST['title'];
            $post= $_POST['post'];
            $date = date("d-m-Y H:i");
            //UPDATE post
            echo $title, $post, $_SESSION['rec_id'];
            $stmt = $connection->query("UPDATE blog_post SET title=? AND post=? WHERE id=?");
            echo 'test1';
            $stmt->bind_param("ssi", $title , $post, $_SESSION['rec_id']);
            echo 'test2';
            printf("Affected rows (UPDATE): %d\n", $connection->affected_rows);        
       }
    ?>    

Upvotes: 0

Views: 163

Answers (1)

Ben Fortune
Ben Fortune

Reputation: 32117

Your MYSQL is wrong.

UPDATE blog_post SET title=? AND post=? WHERE id=?

Should be

UPDATE blog_post SET title=?, post=? WHERE id=?

You should always use some kind of error checking, like the following,

if(!$stmt = $connection->prepare("UPDATE blog_post SET title=?, post=? WHERE id=?")){
    die($connection->error);
}

You also never called $stmt->execute();.

Also you can't use $stmt = $connection->query(); with prepared statements. Use $stmt = $connection->prepare();.

Upvotes: 2

Related Questions