2016-05-23 72 views
0

私はphp pdoを使用してコメントシステムを作成しようとしています。この本は、エラーである私のコードPDOException(構文エラー)

<?php 
     include_once 'dbConfig.php'; 
     if(isset($_POST['user_comm']) && isset($_POST['user_name'])) 
    { 
     $comment=$_POST['user_comm']; 
     $name=$_POST['user_name']; 
     $insert="insert into comments values('','$name','$comment',CURRENT_TIMESTAMP)"; 
      $stmt = $conn->prepare($insert); 
      $stmt->execute(); 
     $id= $conn->lastInsertId(); 


     $sql = "select name,comment,post_time from comments where name='$name' and comment='$comment' and id='$id''"; 
     $stmt = $conn->prepare($sql); 
     $stmt->execute(); 
     if($rows = $stmt->fetch(PDO::FETCH_ASSOC)) 
     { 
     $name=$rows['name']; 
     $comment=$rows['comment']; 
     $time=$rows['post_time']; 
    ?> 
    <div class="comment_div"> 
     <p class="name">Posted By:<?php echo $name;?></p> 
     <p class="comment"><?php echo $comment;?></p> 
     <p class="time"><?php echo $time;?></p> 
    </div> 
    <?php 
} 
exit; ?> 

では

Fatal error: Uncaught exception 'PDOException' with message '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 ''42''' at line 1' in /home/u998801935/public_html/commento/post_comment.php:15 Stack trace: #0 /home/u998801935/public_html/commento/post_comment.php(15): PDOStatement->execute() #1 {main} thrown in /home/u998801935/public_html/commento/post_comment.php on line 15

を取得しています多分、エラーコードのこの部分からのもので考えていますか?事前にあなたの助けのための

$stmt->execute(); 
    if($rows = $stmt->fetch(PDO::FETCH_ASSOC)) 
    { 

おかげ

+5

あなたのクエリは、あなたがそのエラーThamilan mentiを持っている$ id'' – Thamilan

+0

'ID = '' = IDに$ id'''変更' を持っていますonedしかし、あなたはまた、大きなセキュリティホールを持っています!あなたはSQLインジェクションを広く公開しています! SQL文を準備していますが、パラメータをバインドしていません!ハッキングされないようにパラメータをバインドする方法については、下の私の答えをチェックしてください! – Webeng

答えて

1

変更

$sql = "select name,comment,post_time from comments where 
name='$name' and comment='$comment' and id='$id''"; // Notice extra single quote. 

へ:

$sql = "select name,comment,post_time from comments where 
name='$name' and comment='$comment' and id='$id'"; 
+0

ありがとう、それはエラー – baezl

+0

@バゼルでした、あなたは大歓迎です。 – Pupil

0

例外メッセージが言うように:

1064 You have an error in your SQL syntax;

PHPではなくSQL構文に誤りがあります。 したがって、クエリを確認する必要があります。クエリに終わり

$sql = "select name,comment,post_time from comments where name='$name' and comment='$comment' and id='$id''"; 

あなたはaddidional単一引用符を持っている:

Thamilanが質問の下の彼のコメントで述べたように、あなたはここでエラーが発生しています。

+0

ありがとう、それはエラー – baezl

0

あなたが読むhttp://php.net/manual/en/pdostatement.bindparam.phpand id='$id''"

$sql = "select name,comment,post_time from comments where name= :name and comment= :comment and id= :id"; 
    $stmt = $conn->prepare($sql); 
    $stmt->bindParam(':name', $name,PDO::PARAM_STR); 
    $stmt->bindParam(':comment', $comment, PDO::PARAM_STR); 
    $stmt->bindParam(':id', $id,PDO::PARAM_INT); 
    $stmt->execute(); 

としてより良い使用準備とバインド文を持っていると、余分な'

SQLインジェクションのためのオープンであなたのコードを読むHow can I prevent SQL injection in PHP?

+0

ありがとう、それはエラーだった – baezl

1
それを防ぐために

コードに2つの問題があります(thoあなたはそのうちの1つについて尋ねただけです)。

  1. SQL構文エラーがあります。これは、クエリで使用されているSQL文が正しく書き込まれていないことを意味します。他の人が触れたように、それはあなたに特別なappostropheがあるという事実のためです。 id='$id''id='$id'に変更すると、エラーが修正されます。

  2. あなたのコードはSQLインジェクションに広く開いています。このような攻撃を防ぐために、ステートメントの準備とパラメーターのバインドをコードに実装する必要があります。ステートメントを準備しましたが、パラメーターをバインドしていないため、準備がSQLインジェクションに対して完全に役に立たなくなります。

    変更この:私は良い気分で今日:)午前以来

しかし、これはあなたのエラーの問題を修正するだけでなく、あなたのコードを変更するだろうが、データベースの安全を作成する方法です。

$insert="insert into comments 
values('','$name','$comment',CURRENT_TIMESTAMP)"; 
$stmt = $conn->prepare($insert); 
$stmt->execute(); 
$id= $conn->lastInsertId(); 


$sql = "select name,comment,post_time from 
comments where name='$name' and comment='$comment' and id='$id''"; 
$stmt = $conn->prepare($sql); 
$stmt->execute(); 

これまで:

$insert="insert into comments 
values('',:name,:comment,CURRENT_TIMESTAMP)"; 
$stmt = $conn->prepare($insert); 
$stmt->bindParam(':name', $name); 
$stmt->bindParam(':comment', $comment); 
$stmt->execute(); 
$id= $conn->lastInsertId(); 


$sql = "select name,comment,post_time from 
comments where name=:name and comment=:comment and id=:id"; 
$stmt = $conn->prepare($sql); 
$stmt->bindParam(':name', $name); 
$stmt->bindParam(':comment', $comment); 
$stmt->bindParam(':id', $id); 
$stmt->execute();