为什么我会收到语法错误或访问冲突?

时间:2016-05-05 19:15:56

标签: mysql sql pdo

当我尝试向数据库插入内容时,我收到语法错误。我有以下代码,但我真的不知道我做错了什么。

if(isset($_POST['imadbID']) || isset($_POST['comment'])|| isset($_POST['Title']) || isset($_POST['Year'])
|| isset($_POST['released']) || isset($_POST['Runtime'])|| isset($_POST['Actors'])||isset($_POST['Country']) || isset($_POST['Language'])
|| isset($_POST['Plot']) || isset($_POST['Poster']) || isset($_POST['Director']) || isset($_POST['Writer']) || isset($_POST['imdbRating']) || isset($_POST['tst'])){
    $imdbid=json_decode($_POST['imdbID']); $comment=json_decode($_POST['comment']);
    $title=json_decode($_POST['Title']);   $year=json_decode($_POST['Year']);   $released=json_decode($_POST['Released']);
$runtime=json_decode($_POST['Runtime']);   $actor=json_decode($_POST['Actors']);  $country=json_decode($_POST['Country']);
$lang=json_decode($_POST['Language']);   $plot=json_decode($_POST['Plot']);   $poster=json_decode($_POST['Poster']);
$director=json_decode($_POST['Director']);  $writer=json_decode($_POST['Writer']);  $rating=json_decode($_POST['imdbRating']);
 $test=json_decode($_POST['tst']);
    }
try {
$sql="INSERT INTO films (title,length,description,releasedate,bannerpath,language,imdbid,country,rating)
VALUES ($title,$runtime,$plot,$released,$poster,$lang,$imdbid,$country,$rating)";
$conn->exec($sql);
$film_id=$conn->lastInsertId($sql);
}
catch(PDOException $e) {
    echo 'ERROR: ' . $e->getMessage();
}

我浏览了与此相关的其他问题和答案,但似乎无法找到解决方案。

我得到的错误

  

错误:SQLSTATE [42000]:语法错误或访问冲突:1064您   您的SQL语法有错误;检查对应的手册   您的MySQL服务器版本,以便在附近使用正确的语法   ',,,,,,, 6.5)'在第2行

1 个答案:

答案 0 :(得分:3)

缺少引号可能是语法错误的原因,但是您以不安全的方式使用PDO,您可能会回到使用mysql_*

试试这个..

$sql=$conn->prepare("INSERT INTO films (title,length,description,releasedate,bannerpath,language,imdbid,country,rating)
VALUES (:t,:r,:p,:re,:po,:s,:i,:c,:ra)");
$sql->execute(array(":t"=>$title, ":r"=>$runtime, ":p"=>$plot, ":re"=>$released, ":po"=>$poster,":s"=>$lang,":i"=>$imdbid, ":c"=>$country, ":ra"=>$rating));
相关问题