views:

47

answers:

3

I use PDO and i can't insert some data:

use this code:

$sql  = 'INSERT INTO `releases` (id, artists, release, label, catalog, date, tracklist, type, status, vote, votes_count) ';
$sql .= 'VALUES (:id, :artists, :release, :label, :catalog, :date, :tracklist, :type, :status, :vote, :votes_count)';

$query = $this->db->prepare($sql);

$query->bindParam(':id', 0, PDO::PARAM_INT);
$query->bindParam(':artists', implode('|||', $data['artists']), PDO::PARAM_STR);
$query->bindParam(':release', $data['release'], PDO::PARAM_STR);
$query->bindParam(':label', $data['label'], PDO::PARAM_STR);
$query->bindParam(':catalog', $data['catalog'], PDO::PARAM_STR);
$query->bindParam(':date', $data['date'], PDO::PARAM_STR);
$query->bindParam(':tracklist', $data['tracklist'], PDO::PARAM_STR);
$query->bindParam(':type', $data['type'], PDO::PARAM_STR);
$query->bindParam(':status', $data['status'], PDO::PARAM_INT);
$query->bindParam(':vote', 0, PDO::PARAM_INT);
$query->bindParam(':votes_count', 0, PDO::PARAM_INT);

$query->execute();

but data don't inserted to database. all names checked and valid. id as AUTO_INCREMENT field.

if i use this code: $this->db->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING ); $sql = 'INSERT INTO releases (artists, release, label, catalog, date, tracklist, type, status, vote, votes_count) '; $sql .= 'VALUES (:artists, :release, :label, :catalog, :date, :tracklist, :type, :status, :vote, :votes_count)';

$query = $this->db->prepare($sql);

$array = array(':artists'   => implode('|||', $data['artists']),
               ':release'   => $data['release'],
               ':label'     => $data['label'],
               ':catalog'   => $data['catalog'],
               ':date'      => $data['date'],
               ':tracklist' => $data['tracklist'],
               ':type'      => $data['type'],
               ':status'    => $data['status'],
               ':vote'      => 0,
               ':votes_count' => 0);

$query->execute($array);

i receive error:
Warning: PDOStatement::execute() [pdostatement.execute]: SQLSTATE[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 'release, label, catalog, date, tracklist, type, status, vote, votes_count) VALUE' at line 1 in C:\Program Files\Wamp\www\forthcoming\application\controllers\release.php on line 89

A: 

bindParam uses reference semantics. I don't think you can use it on a constant, like you're doing with your PDO::PARAM_INT lines.

I never use bindParam, and I recommend that you use bindValue instead. Or better yet - Pass an associative array to execute.

troelskn
PDO::bindParam() and PDO::bindValue() are for different things. PDO::bindParam() allows you to bind a variable to a placeholder. The variable can then change value before, or after, the statement is executed. This allows you to prepare a statement once and execute it many times, changing the variable values. PDO::bindValue() only allows you to bind a static value to a placeholder. Both are perfectly good and used for different things. The third argument to each method is a PDO constant (like PDO::PARAM_INT) that tells PDO what kind of value the bound variable or value contains.
Jeremy
+1  A: 

Hello,

if ID is an auto_increment, why specify it in your insert query. This is not needed and may be the reason you don't see your inserted line.

Arnaud F.
so i can write$sql = 'INSERT INTO releases (artists, release, label, catalog, date, tracklist, type, status, vote, votes_count) ';$sql .= 'VALUES (:artists, :release, :label, :catalog, :date, :tracklist, :type, :status, :vote, :votes_count)';`?
Yes you can write this !
Arnaud F.
A: 

date and type are reserved words in mySQL.

Surround the field names with backticks:

INSERT INTO `releases` (`id`, `artists`, `release`, `label`, `catalog`,
`date`, `tracklist`, `type`, `status`, `vote`, `votes_count`) ';

or rename them.

Pekka
thank you very much! it works!