gpt4 book ai didi

php - 如何在PDo中插入数据

转载 作者:行者123 更新时间:2023-11-29 01:12:24 26 4
gpt4 key购买 nike

我使用 PDO,但无法插入一些数据:

使用这段代码:

$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();

但是数据没有插入到数据库中。所有名称均已检查且有效。 id 作为 AUTO_INCREMENT 字段。

如果我使用这段代码: $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);

我收到错误:
警告:PDOStatement::execute() [pdostatement.execute]:SQLSTATE[42000]:语法错误或访问冲突:1064 您的 SQL 语法有误;查看与您的 MySQL 服务器版本相对应的手册,了解在 C:\Program Files 中第 1 行的“release, label, catalog, date, tracklist, type, status, vote, votes_count) VALUE”附近使用的正确语法\Wamp\www\forthcoming\application\controllers\release.php89

最佳答案

datetypereserved words在 MySQL 中。

用反引号将字段名称括起来:

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

或重命名它们。

关于php - 如何在PDo中插入数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3954489/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com