[英]Uncaught PDOException: SQLSTATE[42000]: Syntax error or access violation
I don't know why am I gettng the error我不知道为什么我会收到错误
Fatal error: Uncaught PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax;
致命错误:未捕获的 PDOException:SQLSTATE[42000]:语法错误或访问冲突:1064 您的 SQL 语法有错误; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Preprocessor' at line 1 in /var/www/html/phpquiz/result.php:17
检查与您的 MySQL 服务器版本相对应的手册,以获取在 /var/www/html/phpquiz/result.php:17 中第 1 行的“预处理器”附近使用的正确语法
There error occurs when executing the prepared statement ie on executing $cho->execute();
执行准备好的语句时发生错误,即执行
$cho->execute();
<?php
session_start();
if(!isset($_SESSION['score'])){
$_SESSION['score'] = 0;
}
if(isset($_POST['submit'])){
$arr = array("a", "b", "c", "d", "e");
for($i = 1; $i < 6; $i++){
$text = $_POST['que_'.$arr[$i-1]];
echo "$text<br> ";
$cho = $pdo->prepare("SELECT id from choices where `text` = $text");
$cho->execute();
$r = $cho->fetch(PDO::FETCH_ASSOC);
echo $r;
}
}
Your prepare statement is wrong.你的准备语句是错误的。 You need to replace the PHP variable with a placeholder and then pass the actual value to the
execute
function.您需要用占位符替换 PHP 变量,然后将实际值传递给
execute
函数。
$cho = $pdo->prepare("SELECT id FROM choices WHERE `text` = ?");
$cho->execute([$text]);
$r = $cho->fetch(PDO::FETCH_ASSOC);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.