【发布时间】:2012-10-02 00:54:59
【问题描述】:
我遇到了一个奇怪的问题。我正在使用一段代码,它在我的网站上 100% 有效,但在特定的地方不起作用。这是代码:
$stmt_insert_query = "INSERT INTO mya_events(event_id, artist_id, event_title, date, event_text, event_start, event_duration, genre, soundcloud_preview, event_type, country, ext, clicks, active) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
$stmt_insert = $db->prepare($stmt_insert_query);
$stmt_insert->bindValue(1, $next_avail, PDO::PARAM_INT);
$stmt_insert->bindValue(2, $_id, PDO::PARAM_INT);
$stmt_insert->bindValue(3, $_POST['event_title'], PDO::PARAM_STR);
$stmt_insert->bindValue(4, $event_date, PDO::PARAM_STR);
$stmt_insert->bindValue(5, $_POST['event_text'], PDO::PARAM_STR);
$stmt_insert->bindValue(6, $_POST['event_start'], PDO::PARAM_STR);
$stmt_insert->bindValue(7, $_POST['event_duration'], PDO::PARAM_STR);
$stmt_insert->bindValue(8, 'electronica', PDO::PARAM_STR);
$stmt_insert->bindValue(9, '', PDO::PARAM_STR);
$stmt_insert->bindValue(10, 'dj_event', PDO::PARAM_STR);
$stmt_insert->bindValue(11, 'US', PDO::PARAM_STR);
$stmt_insert->bindValue(12, '', PDO::PARAM_STR);
$stmt_insert->bindValue(13, 0, PDO::PARAM_INT);
$stmt_insert->bindValue(14, 'y', PDO::PARAM_STR);
$stmt_insert->execute();
在这段代码之后,我正在显示一个显示的文本。我还用 print_r($db->errorInfo()); 检查了错误。但没有显示错误。也尝试了 try - catch 但没有。
我从 phpMyAdmin 手动输入了一行,它可以工作。
我哪里错了?我检查了 10 次代码,它很完美。
更新
我在服务器的日志中发现了一个错误 正是我做的地方->执行
[2012 年 10 月 11 日 14:48:15] PHP 致命错误:未捕获异常 'PDOException' 并带有消息 'SQLSTATE[42000]:语法错误或访问冲突:1064 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在 ''event_id'、'artist_id'、'event_title'、'date'、'event_text'、'event_start'、'ev' at line 1' 附近使用正确的语法/home/cubeworx/public_html/electronicdancemusic.net/MYArtist/admin/list_events.php:527 堆栈跟踪:
0 /home/cubeworx/public_html/electronicdancemusic.net/MYArtist/admin/list_events.php(527):PDOStatement->execute()
1 {主}
在第 527 行的 /home/cubeworx/public_html/electronicdancemusic.net/MYArtist/admin/list_events.php 中抛出
【问题讨论】:
-
是的,我没有收到任何错误,并且代码是安全可靠的。
-
我很确定您正在遇到错误,但没有看到它们。我可以在您的代码中看到一个问题,使用不带反引号的
date,这是 mySQL 中的保留字(并且需要反引号)。因此问题的链接 - 从 PDO 获取错误消息可能很麻烦 -
@AdrianTanase:可以肯定的是,您尝试过设置
$db->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );,对吧?不要忘记您可能也在使用不同的数据库。最好将INSERT INTO mya_events更改为INSERT INTO dbName.mya_events。最后:尝试将您的 ini 设置为E_ALL | E_STRICT以避免警告和错误被抑制 -
您有一个名为
date的列,这是一个mysql 保留字。在您的查询中,在该列名称周围加上反引号,或者将其更改为非保留字。