前提・実現したいこと
phpでブログを実装していました。
コメント作成機能を実装中に以下のエラーメッセージが発生しました。
発生している問題・エラーメッセージ
問題 コメントは入力できるがコメント送信ができずエラーメッセージが出てしまう エラーメッセージ Error: SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens ```ここに言語名を入力→HTML/CSS
該当のソースコード/ソースコード
<?php require_once('db_connect.php'); require_once('function.php'); check_user_logged_in(); if (!empty($_POST)) { $post_id = $_POST["post_id"]; if (empty($_POST["name"])) { echo 'ng'; } else if (empty($_POST["content"])) { echo 'ng'; } if (!empty($_POST["name"]) && !empty($_POST["content"])) { $name = $_POST["name"]; $content = $_POST["content"]; $pdo=db_connect(); try { $sql = "SELECT * FROM comments WHERE post_id = :post_id"; $stmt=$pdo->prepare($sql); $stmt->bindparam(':post_id',$post_id); $stmt->bindparam(':name',$name); $stmt->bindparam(':content',$content); $stmt->execute(); header("Location: detail_post.php?id=".$post_id); exit; } catch (PDOException $e) { echo 'Error: ' . $e->getMessage(); die(); } } } else { $post_id = $_GET['post_id']; redirect_main_unless_parameter($post_id); } ?> <!DOCTYPE html> <html> <head> <title> </title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> </head> <body> <h1></h1> <form method="POST" action=""> <input type="hidden" name="post_id" value="<?php echo $post_id; ?>"> :<br> <input type="text" name="name"> <br> :<br> <input type="text" name="content" style="width:200px;height:100px;"><br> <input type="submit" value="submit"> </form> <a href="detail_post.php?id=<?php echo $post_id; ?>"></a> </body> </html> ```試したこと
-SQL文がそもそも間違っていることに気づき訂正しました
$sql = "SELECT * FROM comments WHERE post_id = :post_id";
↓
$sql = "INSERT INTO comments (name,content)values(:name,:content)";
(結果は変わりませんでした)。
回答1件
あなたの回答
tips
プレビュー