簡體   English   中英

致命錯誤:未捕獲的 PDOException:SQLSTATE [42000]:語法錯誤或訪問沖突:1064 PHP8.1.x

[英]Fatal error: Uncaught PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 PHP8.1.x

我發現我的 CRUD 沒有什么問題。 當我在 7.4.x 版本中運行 PHP 代碼時,它工作正常。 但是當我用 PHP ver 在服務器上部署這個站點時。 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>

你知道問題出在哪里嗎? 謝謝

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


?>

這是 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;
    }

我只是想不直。 問題太明顯了。。。

錯誤代碼:

// 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;
    }

固定代碼:

// 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;
    }

老實說,我不知道自己在想什么……有時你需要從項目中退后一步,回來后它會突然出現在你面前。 無論如何,現在它的工作。 謝謝

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM