簡體   English   中英

錯誤:SQLSTATE [42000]:語法錯誤或訪問沖突:1064-PHP MYSQL

[英]Error: SQLSTATE[42000]: Syntax error or access violation: 1064 - PHP MYSQL

我了解這是先前詢問的問題的重復。 但是,我遵循了先前的答案,但仍然沒有結果。

我正在使用一條准備好的語句,以使用方法post從html <form>進行評論。 會話中的注釋以及唯一ID將傳遞到頁面addComment.php

這是“ addComment.php”的內容

<?php
    session_start();
    $servername = "localhost";
    $username = "root";
    $password = "root";
    $dbname = "somedatabase";
    try {
        $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
        // set the PDO error mode to exception
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

        // prepare sql and bind parameters
        $stmt = $conn->prepare("INSERT INTO patients( comments ) VALUES ( :comment ) WHERE unique_id = :unique_id");
        $stmt->bindParam( ':comment',           $comment    );
        $stmt->bindParam( ':unique_id',         $unique_id  );
        $comment          =   $_POST[      'comment'     ];
        $unique_id        =   $_SESSION[   'unique_id'        ];
        $stmt->execute();
        //header('Location: newMedicine.php');
    }
    catch(PDOException $e){
        echo "Error: " . $e->getMessage();
    }
    $conn = null;
?>

我已經回聲了

 $comment          =   $_POST[      'comment'     ];
 $unique_id        =   $_SESSION[   'unique_id'        ];

並且它們都可以正常打印。

我得到的錯誤是

錯誤:SQLSTATE [42000]:語法錯誤或訪問沖突:1064您的SQL語法有錯誤。 在第13行的'WHERE unique_id ='JohnDoe20RG2018-01-23 11:43:'附近檢查與您的MySQL服務器版本相對應的手冊以使用正確的語法

數據庫中patients table中的unique_id字段具有相同的值

JohnDoe20RG2018-01-23 11:43:17

我看不到我要去哪里錯了。 我在整個項目中都使用了多個帶有Selects和Inserts的准備好的語句,它們都可以正常工作。

任何幫助,將不勝感激。

如果您要使用ID和注釋創建新記錄,請使用...

$stmt = $conn->prepare("INSERT INTO patients ( unique_id, comments ) 
                    VALUES ( :unique_id, :comment ) ");

如果是現有記錄-

$stmt = $conn->prepare("UPDATE patients SET comments=:comment
                    WHERE unique_id = :unique_id");

暫無
暫無

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

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