-1

ここで何が間違っていますか?exec の変数に問題があるのではないかと思いましたが、別の場所で動作する類似コマンドがあります。コメント部分は影響を受けた行として int(1) を返し、このコードは次のエラーを返します。あなたのアドバイスに感謝します。私はただ学んでいます。

"object(PDO)#2 (0) { } Array ( [0] => 42000 [1] => 1064 [2] => You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'try, Wed, 06 Mar 2013 13:25:09 +0200)' at line 1 ) "

コード:

<?php
class gbMsg {
    private $_db;
    private $dbc;

    function __construct(){
        $this->dbc = parse_ini_file($_SERVER["DOCUMENT_ROOT"]."/lock/conect.ini");
        try{
            $this->_db = new PDO($this->dbc["conn"], $this->dbc["user"], $this->dbc["pass"]);
        }catch(PDOException $e){
            echo $e->getMessage();
        }
    }

    function addPost($name, $msg){
        echo var_dump($this->_db);
        $d = date("r");
        $stmt = $this->_db->exec("INSERT INTO gPosts (name, message, date) VALUES ($name,$msg, now())")
        or die(print_r($this->_db->errorInfo(), true));
        echo var_dump($stmt);
    }
}

#   function addPost(){
#       echo var_dump($this->_db);
#       $stmt = $this->_db->exec("INSERT INTO gPosts (name, message, date) VALUES ('Kirill','sec', now())");
#       echo var_dump($stmt);
#   }
#}
4

2 に答える 2

2

ただし、問題を解決するには、ユーザーがパラメーター化されたクエリの方が優れています。

 $stmt = $this->_db->exec("INSERT INTO gPosts (name, message, date)
 VALUES (\"$name\",\"$msg\", now())")

あなたは引用符を忘れました...

パラメータ化されたクエリと準備済みステートメントの詳細については、http: //php.net/manual/en/pdo.prepared-statements.phpを参照してください。

于 2013-03-06T11:35:36.223 に答える
-1

以下のようなものも機能するはずです。IMHO二重引用符をエスケープするよりもはるかにきれいに見えます

$stmt = $this->_db->exec("INSERT INTO gPosts (name, message, date)
 VALUES ('$name','$msg', now())")
于 2014-07-28T14:04:27.890 に答える