[英]Fatal error: Uncaught PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 PHP8.1.x
I found little problem in my CRUD.我发现我的 CRUD 没有什么问题。 When I run the PHP code in version 7.4.x it work just fine.
当我在 7.4.x 版本中运行 PHP 代码时,它工作正常。 But when i deploy this site on server with PHP ver.
但是当我用 PHP ver 在服务器上部署这个站点时。 8.1.x I getting this error:
8.1.x 我收到此错误:
<pre>Fatal error: Uncaught PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ':id' at line 1 in C:\xampp\htdocs\crud\user.inc.php:53
Stack trace:
#0 C:\xampp\htdocs\crud\user.inc.php(53): PDOStatement->execute()
#1 C:\xampp\htdocs\crud\ajax.php(40): User->getRow('id', '1')
#2 {main} thrown in C:\xampp\htdocs\crud\user.inc.php on line 53</pre>
Do you have any idea, where the problem can be?你知道问题出在哪里吗? Thanks
谢谢
ajax.php: ajax.php:
<?php
$action=$_REQUEST['action'];
if(!empty($action)){
require_once 'user.inc.php';
$obj=new User();
}
//adding user action
if($action=='adduser' && !empty($_POST)){
$name=$_POST['firstname'];
$lastname=$_POST['lastname'];
$email=$_POST['email'];
$photo=$_FILES['photo'];
$playerId=(!empty($_POST['userid']))? $_POST['userid']: "";
$imagename="";
if(!empty($photo['name'])){
$imagename=$obj->uploadPhoto($photo);
$playerData=[
'firstname'=>$name,
'lastname'=>$lastname,
'email'=>$email,
'photo'=>$imagename,
];
}else{
$playerData=[
'firstname'=>$name,
'lastname'=>$lastname,
'email'=>$email,
];
}
$playerId=$obj->add($playerData);
if(!empty($playerId)){
$player=$obj->getRow('id',$playerId);
echo json_encode($player);
exit();
}
}
?>
and here is user.inc.php这是 user.inc.php
// fuction to get single row
public function getRow($field,$value)
{
$sql = "SELECT * FROM {$this->tableName} WHERE {$field}=:{$field}";
$stmt = $this->conn->prepare($sql);
$stmt->execute();
if ($stmt->rowCount() > 0) {
$result = $stmt->fetch(PDO::FETCH_ASSOC);
} else {
$result = [];
}
return $result;
}
I just not think straight.我只是想不直。 The problem was so obvious...
问题太明显了。。。
code with error:错误代码:
// fuction to get single row
public function getRow($field,$value)
{
$sql = "SELECT * FROM {$this->tableName} WHERE {$field}=:{$field}";
$stmt = $this->conn->prepare($sql);
$stmt->execute();
if ($stmt->rowCount() > 0) {
$result = $stmt->fetch(PDO::FETCH_ASSOC);
} else {
$result = [];
}
return $result;
}
fixed code:固定代码:
// fuction to get single row
public function getRow($field, $value)
{
$sql = "SELECT * FROM {$this->tableName} WHERE {$field}=:{$field}";
$stmt = $this->conn->prepare($sql);
$stmt->execute([":{$field}"=> $value]);
if ($stmt->rowCount() > 0) {
$result = $stmt->fetch(PDO::FETCH_ASSOC);
} else {
$result = [];
}
return $result;
}
Honestly I don't know what I was thinking... Sometimes you need to step back away from the project and after you come back it will pop up to you.老实说,我不知道自己在想什么……有时你需要从项目中退后一步,回来后它会突然出现在你面前。 Any way, now its working.
无论如何,现在它的工作。 Thanks
谢谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.