0
votes

I'm having trouble updating a mysql database using PDO. I am not getting any error message when I execute the update query but it's not updating that database. Error reporting is on i think - self::$db->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION ) - and i did get an error at the start indicating i had got a column name wrong and one about a syntax error but since fixing those I'm not getting any error messages. I've tried various things from some of the other similar queries without joy.

I have a class called Events, the constructor executes a select statement without problem. Update is a function in the class and the $array parameter being passed in is $_POST (submitted form data) - am I trying to access this $_POST data from $array the wrong way?

here is the function

function update ($array) {

    $this->db = mydb::getConnection();

    try {

        // prepare statement
        $statement = $this->db->prepare("UPDATE gigs SET who = :who WHERE gig_id = :gig_id");

        // bind parameters
        $statement->bindParam(':gig_id', $array['gig_id'], PDO::PARAM_INT);
        $statement->bindParam(':who', $array['who'], PDO::PARAM_STR);

        // execute statement
        $statement->execute();

    } catch (Exception $ex) {

        throw $ex;

    }

}

Thanks for any suggestions.

1
In your catch block, try echo $ex->getMessage() to see if there's an error. - Rob W
Thanks Rob, I gave that a go but it's not echoing any more clues - Shane
Are you sure your $array keys exist and have values? Try var_dump($array) - Rob W
Also, try catching PDOException just as a trial. - Rob W
I just did that and it seems the keys and values exist. This is what I got... array(12) { ["q"]=> string(3) "388" ["who"]=> string(55) "string text here" - Shane

1 Answers

0
votes

A couple of things to note:

  • You don't need the try ... catch block if you are simply throwing the exception without doing anything
  • I do not think the $this->db->prepare() works. The chaining is fine but you should probably do ($this->db)->prepare() or the following

    $pdo = $this->db;
    $pdo->prepare(...)
    

so, please try the following:

function update ($array) {
    $this->db = mydb::getConnection();

    // prepare statement
    $pdo = $this->db;
    $statement = $pdo->prepare("UPDATE gigs SET who = :who WHERE gig_id = :gig_id");

    // bind parameters
    $statement->bindParam(':gig_id', $array['gig_id'], PDO::PARAM_INT);
    $statement->bindParam(':who', $array['who'], PDO::PARAM_STR);

    // execute statement
    $statement->execute();
}