0

I have started to move me from using the typical outdated mysql to use the PDO extension.

Could someone helped me fix the following code? It has no error messages, but it will obviously not insert a new table in my database. the database is used, varchar, and key is also varchar.

  $id = 1;
$used = '1x2';
$load = $get->prepare("INSERT INTO keys (key,used) VALUES (:id,:used)");
$load->bindValue(':id',$id, PDO::PARAM_STR);
$load->bindValue(':used',$used, PDO::PARAM_STR);


try {
$load->execute();
}catch(exception $e) {
echo 'Error';
echo errorhandle($e);
}
maria
  • 207
  • 5
  • 22
  • 56
  • 1
    `key` and `keys` are reserved words http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html - either rename those or use backticks. – Funk Forty Niner Jun 03 '14 at 16:16
  • Read over [How to squeeze an error message out of PDO](http://stackoverflow.com/questions/3726505/how-to-squeeze-error-message-out-of-pdo) and setup `ERRMODE_EXCEPTION`. By default, PDO _errors silently_. – Michael Berkowski Jun 03 '14 at 16:17
  • oh, i will check it out. Talk about bad luck that I chose that name: p – maria Jun 03 '14 at 16:17
  • I updated the code itself no, still wont work now even after my variablechanges. – maria Jun 03 '14 at 16:20
  • @Malin what variable changes? Your query needs to look like `INSERT INTO \`keys\` (\`key\`,used) VALUES (:key,:used)` (note the backticks around key, keys) – Michael Berkowski Jun 03 '14 at 16:22
  • it should be possible to use NSERT INTO keys (key,used) VALUES (:key,:used) somehow? since in mysql i got inserted rows to key easily. – maria Jun 03 '14 at 16:22
  • @Malin Ok, I see the changes above. It isn't the placeholder `:key` that is your problem, it is the column name `key` and the table name `keys`. Those need to be quoted with backticks as in my example above. – Michael Berkowski Jun 03 '14 at 16:24
  • Add `$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);` right after the connection is opened, replacing `$pdo` with your DB connection, see if it yields anything. – Funk Forty Niner Jun 03 '14 at 16:24
  • See also [When to use single quotes, double quotes, and backticks](http://stackoverflow.com/questions/11321491/when-to-use-single-quotes-double-quotes-and-backticks) for quoting samples... – Michael Berkowski Jun 03 '14 at 16:24
  • i see @MichaelBerkowski, but it still wont work :/ – maria Jun 03 '14 at 16:25
  • I'm questioning `errorhandle($e)` I doubt that's a core function. – Funk Forty Niner Jun 03 '14 at 16:26
  • @Fred-ii- added it and it throwed out: ErrorSQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'keys (`key`,used) VALUES ('1','1x2')' at line 1 – maria Jun 03 '14 at 16:27
  • @Fred-ii-, the function errorhandle($e) works fine, it threw out error. http://malinpedersen.no/PDO/index.php – maria Jun 03 '14 at 16:27
  • 1
    @Malin That means you haven't added the backticks around the two reserved words as in my comment from ~5 minutes ago. `\`keys\`` and `\`key\`` – Michael Berkowski Jun 03 '14 at 16:28
  • I stand corrected then. – Funk Forty Niner Jun 03 '14 at 16:28
  • $load = $get->prepare("INSERT INTO keys (`key`,used) VALUES (:`key`,:used)"); – maria Jun 03 '14 at 16:29
  • `keys` needs backticks too for the INSERT INTO. `key` and `keys` but not backticks for the placeholders leave it as `:key` – Funk Forty Niner Jun 03 '14 at 16:30
  • @Fred-ii- thanks, now i understood it. Im not currently familiar with these words that i cant use, since in the old mysql_query i did use ` and 's on everything. fixed. – maria Jun 03 '14 at 16:32
  • You're welcome, glad this has been resolved, cheers. – Funk Forty Niner Jun 03 '14 at 16:33

0 Answers0