未捕获的PDOException:SQLSTATE [42000]:语法错误或访问冲突:1064

时间:2017-05-29 15:50:21

标签: php mysql html5 pdo transactions

我正在尝试为我正在创建的论坛设置“插入主题/帖子”页面,但是当我通过它运行任何输入时,它会给我以下错误:

  

致命错误:未捕获PDOException:SQLSTATE [42000]:语法错误或访问冲突:1064您的SQL语法出错;检查与MariaDB服务器版本相对应的手册,以便在C:\ xampp \ htdocs \ FORUM \ create_topic.php的第1行''test'附近使用正确的语法:29堆栈跟踪:#0 C:\ xampp \ htdocs \ FORUM \ create_topic.php(29):PDO-> exec('INSERT INTO top ...')在第29行的C:\ xampp \ htdocs \ FORUM \ create_topic.php中抛出#1 {main}

代码在else语句中,如下所示:

} else {
// Start a transaction with the database
$DB_con->beginTransaction();
$query = $DB_con->exec("INSERT INTO topics (topic_subject, topic_date, topic_cat, topic_by) VALUES('" . $_POST['topic_subject']) . ", " . $_SESSION['user_id'] . ")";

if (!$query) {
    echo 'An error occured whilst creating your topic. Please try again.';
    $DB_con->rollback();
} else {
    $topicid = $DB_con->lastInsertId();
    $query = $DB_con->exec("INSERT INTO posts(post_content, post_date, post_topic, post_by) VALUES('" . $_POST['post_content']) . "', NOW(), " . $topicid . ", " . $_SESSION['user_id'] . ")";

    if (!$query) {
        echo "There was a problem inserting your post. Try again later.";
        $DB_con->rollback();
    } else {
        $DB_con->commit();
        echo 'You have successfully created <a href="topic.php?id='. $topicid . '">your new topic</a>.';
    }
}

我认为问题可能是我在我的代码中使用了保留的SQL命令,但我无法发现它。我试图第一次使用交易,整个概念让我失望。 如果这是一个糟糕的问题 - 让我知道,我会接受,更新或改写它!谢谢:))

0 个答案:

没有答案