2

I'm having a rather hard time seeing what is going wrong with the following lines of code:

$id = $_POST["id"];
$naam = $_POST["naam"];
echo("$id<br />");
echo("$naam");
$select_posts=$dbh->prepare("UPDATE activiteiten SET naam= :naam where id= :id");
$select_posts->bindParam(':id', $id);
$select_posts->bindParam(':naam', $naam);
$select_posts->execute();

I'm echoing $id and $naam, just to make sure there's something there. Furthermore, the SQL statement is valid in the sense that, when executed in PHPDBAdmin the records get updated. However, using this code no updates nor errors occur.

I've had this working before, yet right now I have no clue as to why it's not functioning as it should.

Jay
  • 21
  • 1
  • 3
  • 2
    First port of call: [How to squeeze error message out of PDO?](http://stackoverflow.com/questions/3726505/how-to-squeeze-error-message-out-of-pdo) – Pekka Feb 06 '11 at 15:24
  • 1
    this par tlooks fine to me. some issue with the connection? can you select something? can you update something else? – The Surrican Feb 06 '11 at 15:25
  • I've no clue to PDO, but is it correct to include the colon in the parameters name in call to `bindParam()`? From what I know from e.g. delphi, the parameter name should be given without the colon. – Patrick Echterbruch Feb 06 '11 at 15:27
  • The table name is really activiteiten with n? – rik Feb 06 '11 at 15:27
  • I've added print_r($dbh->errorInfo()); right after the select_posts->execute(); line. This returns Array ( [0] => 00000 ). The connection is OK as I can select and insert without a problem. I haven't written anything else to update thus far, so I'm not sure on that part. Yes, bindParam() should be used afaik. Indeed, the table name is the plural of the noun 'activiteit' in Dutch. Thank you for your feedback thus far! – Jay Feb 06 '11 at 15:32
  • As far as I can tell the aforementioned error code means the statement was executed succesfully with no errors or warnings. – Jay Feb 06 '11 at 15:56
  • Also check you're connected to the right database. Happened to me, expected changes in my development database, when queries were performed on production one. – meze Feb 06 '11 at 16:12
  • Thank you for your feedback. However, I've already included the database using require_once("include/inc_db.php");. – Jay Feb 06 '11 at 16:16
  • I know you did. I meant you may be connected to one database, and expect changes in another. – meze Feb 06 '11 at 16:24
  • Hmm. I've double checked but I can probably rule this out since I've been working on the same server with the same database all this time, and I haven't created said database on my personal development box. Which probably isn't good practice, but I did this to be sure the app would function on the clients server, which is on the same hosting. – Jay Feb 06 '11 at 16:41
  • Ok. Then I'd check mysql queries log or try to specify data type for id: `$select_posts->bindParam(':id', $id, PDO::PARAM_INT);` – meze Feb 06 '11 at 16:43
  • I've specified the data type as you suggested and checked the logs for any relevant errors but to no avail. I've deliberately created an error just to check whether it would be registered and it did. I'm stumped at this point. – Jay Feb 06 '11 at 16:59
  • weird ;s and if you try without placeholders, something like `$dbh->prepare("UPDATE activiteiten SET naam= '123' where id=YOURID")->execute();` ? – meze Feb 06 '11 at 17:04
  • Yes! That worked! I take it something is going wrong with the bindParam lines? Progress, thank you! :) – Jay Feb 06 '11 at 17:11
  • It's fixed! I've changed the $naam = $_POST["naam"] to $naam = (htmlentities($_POST["naam"])); and now it seems to work! Thank you so much for your suggestions! How do I add positiveness to your reputation? :) – Jay Feb 06 '11 at 17:17
  • I didn't suggest `htmlentities` ;) It SHOULD work without this function, unless you did something to `$id` or found a bug in PDO ;) – meze Feb 06 '11 at 17:22
  • You're right, it does work without htmlentities. It appears the field only updates when only numeric values are entered. Alphabetic characters don't work. It is however a field of type varchar. – Jay Feb 06 '11 at 17:29
  • does field `naam` have (VAR)CHAR type in your database schema?. You might also try `$sth->bindParam(':naam', $naam, PDO::PARAM_STR);`, even though PARAM_STR is the default value – meze Feb 06 '11 at 17:33
  • 1
    Yes it is of type varchar. Right now it seems to be working with the following lines of code: `$id = $_POST["id"];``$naam = $_POST["naam"];``echo("$id
    ");``echo("$naam");``$update=$dbh->prepare("UPDATE activiteiten SET naam= :naam WHERE id= :id");``$update->bindParam(':id', $id);``$update->bindParam(':naam', $naam);``$update->execute();`. In essence only $select_posts has been change to $update as far as I can tell, which is irrelevant. Still not sure what has caused the problem though. However, I do thank you kindly for your suggestions! :)
    – Jay Feb 06 '11 at 17:41
  • Interesting what was a cause of the problem, if renaming a variable helped... Check the rest of your code if it uses `$select_posts` somehow – meze Feb 06 '11 at 17:46
  • Yes, interesting indeed. So far I haven't come across it yet in the rest of the code. To be honest, the `$select_posts` var was introduced by copy/pasting a snippet from a different working project, so it's probably not occurring in the current one. – Jay Feb 06 '11 at 18:01

0 Answers0