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