0

My update statement dosn't seem to be updating my database but I'm unsure why, I've used the same code elsewhere in my script and it works fine.

try
{
    // update the live documents details
    $sth = $conn->prepare("UPDATE docs SET ref = :ref, rev = :rev, updated = :updated WHERE id = :id");
    $sth->bindParam(':ref', $ref);
    $sth->bindParam(':rev', $rev);
    $sth->bindParam(':updated', $date);
    $sth->bindParam(':id', $currentid);
    $sth->execute();
}
catch(Exception $e)
{
    throw new Your_Exception($e->getMessage());
    // or
    throw $e;
}

I've tried manually inputting a query into the database using PHPMyAdmin just to test I have my table names correct and the query does work as expected.

UPDATE docs SET ref =  'FMS',
rev =  'D',
updated = NOW( ) WHERE id =73

So this leaves me thinking I have an error in my PDO statement. Although the try catch block isn't giving any errors.

John Willemse
  • 6,608
  • 7
  • 31
  • 45
twigg
  • 3,753
  • 13
  • 54
  • 96
  • Have you tried printing out the query in your PHP code? – ajtrichards Jun 13 '13 at 11:27
  • If you are binding the string "NOW()" for `updated` you might have problems with that, instead of it, put a PHP datetime format – Royal Bg Jun 13 '13 at 11:27
  • @RoyalBg I'm passing the date like so date_default_timezone_set('UTC'); $date = date("Y-m-d"); Then using the $date variable to pass into the PDO statement – twigg Jun 13 '13 at 11:29
  • @hek2mgl thanks for your help very constructive – twigg Jun 13 '13 at 11:30
  • I just found this funny. Don't take it personal. Imagine you have the namespace `Your` and `My` and `Hello`.. Hihi. That's funny! :) Check the answer of @YourCommonSense. This should help you. – hek2mgl Jun 13 '13 at 11:32
  • [PDO query fails but I can't see any errors. How to get an error message from PDO?](http://stackoverflow.com/a/15990858/285587) – Your Common Sense Jun 13 '13 at 11:34
  • 1
    @hek2mgl Agreed, I'd prefer `throw new My_Exception()` – Fabrício Matté Jun 13 '13 at 11:42

1 Answers1

2

There are all possible reasons

  • there is an error in the query (which have to be thrown)
  • there is no data to match the criteria.
  • the data is already updated - nothing to change.
  • you are checking not the table/database which you were updating.

Please verify all the issues listed.

By the way, to be able to see thrown errors you have to configure PHP properly

Community
  • 1
  • 1
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • Hello, thanks for your help I will look into this now, im connecting to my database like so: try { $conn = new PDO("mysql:host=$dbhost; dbname=$dbname", $dbuser, $dbpass); $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } catch(PDOException $e) { echo $e->getMessage(); } would this be enough to throw errors correctly in my above code? – twigg Jun 13 '13 at 12:08