php – update with PDO not working, serveral options tried out

I am trying to update a row in a table in mysql database using PDO and taking the data from a form using post method. I have tried a few ways and none of them is working. I don’t know what’s the mistake. if some can suggest something that will be really great.

For example this code does not do the job (id taken from session)…

$u = $_POST;
if( isset($_POST['update']) ) {
    $output = 'table';
    $usr = "update table set one=?, two=?, three=? where id=?";

    $one=$_POST['one'];
    $two=$_POST['two'];
    $three=$_POST['three'];

    $query=$db->prepare($usr);
    if( !$query->execute(array($one, $two, $three)) ) {
        $db->error;
    } else {
        print "update successful";
    }
}

This does not work either (again, id taken from session)…

$u = $_POST;
if( isset($_POST['update']) ) {
    $output = 'table';
    $usr = "update users set one=:one, two=:two, three=:three where id=?";
    $res = $db->prepare($usr);
    if(!$res->execute(array(':one'=>$u['one'],
                            ':two'=>$u['two'],
                            ':three'=>$u['three']))) {
        $error['usr'] = sprintf("%s could not be updated", htmlentities($_POST['firstname']));
        $output = 'form'; }
    else {
        //$status = sprintf("%s created", htmlentities['firstname']);
    }
}

I also tried this http://www.mustbebuilt.co.uk/php/insert-update-and-delete-with-pdo/ and it also didn’t work…

Thanks in advance for suggestions.

Leave a comment