Hi Everyone, I am new to PDO PHP - I am getting to grips with the insert statements with bind_param.

What I am a bit confused about is how to update a table where id = $var

I have an image upload script that works fine, as it uploads the image to the server location.
What I am trying to do is update the users db table with the location of the image on the server.

Here is my attemp, the strange thing is, Im not seeing any error messages, but the table field is not being update.
Any help would be great.

    $dbtype     = "mysql";
    $dbhost     = "dbh";
    $dbname     = "dbn";
    $dbuser     = "dbu";
    $dbpass     = "dbp";    
    // database connection
    $conn = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass);   
    // new data
    $img = '$resized_file';
    $author = '$authorid';
    // query
    $sql = "UPDATE table 
            SET image=?,
            WHERE id=?";
    $q = $conn->prepare($sql);
    $q->execute(array($img,$author));

Recommended Answers

All 4 Replies

Here's a code snippet showing you how to check for errors.

Perhaps you need to explicitly specify the type if you are inserting into a blob.

Hi, Im not inserting into a blob, I am trying to insert the image file name? as a string...

Got it to work by doing the following -

    $img = "$resized_file";
    $author = "$authorid";
    // query
    $sql = "UPDATE table SET image = ? WHERE id = ?";
    $q = $conn->prepare($sql);
    $q->execute(array($img,$author));

Just incase someone else is having the same issues... Hope it helps

Oh, table is a reserved word, so if that's really the name of your table, surround it with backticks.

Ah, I missed that comma too...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.