SELECT
クエリに関する問題が発生しています。クエリは、このデータベースのすべての行ではなく、1つの結果のみを出力します。SELECTクエリは1つの結果のみを出力します
誰に見えるのですか?
$select_comments_sql = "
SELECT *
FROM home_comments
ORDER BY id DESC
";
if ($select_comments_stmt = $con->prepare($select_comments_sql)) {
//$select_comments_stmt->bind_param("s", $user_id);
$select_comments_stmt->execute();
if (!$select_comments_stmt->errno) {
//echo "error";
}
$select_comments_stmt->bind_result($comment_id, $comment_user_id, $comment_username, $home_comments, $comment_date);
$comment_array = array();
while ($select_comments_stmt->fetch()) {
$comment_array[] = $comment_user_id;
$comment_array[] = $comment_username;
$comment_array[] = $home_comments;
$comment_array[] = $comment_date;
}
if ($home_comments === NULL) {
echo 'No comments found.';
} else {
echo $comment_username. "<br>";
echo $home_comments. "<br><br><br>";
}
}
AJAX
$("#comment-form").on("submit", function (event) {
event.preventDefault();
var home_comment = $("#home_comment").val();
$.ajax({
url: "ajax-php/comment-send.php",
type: "POST",
data: {
"home_comment": home_comment
},
success: function (data) {
// console.log(data); // data object will return the response when status code is 200
if (data == "Error!") {
alert("Unable to post comment!");
alert(data);
} else {
$("#comment-form")[0].reset();
//$('.newsletter-popup').fadeIn(350).delay(2000).fadeOut();
}
},
error: function (xhr, textStatus, errorThrown) {
alert(textStatus + " | " + errorThrown);
console.log("error"); //otherwise error if status code is other than 200.
}
});
});
PHP
$user = new User();
$home_comment = $_POST['home_comment'];
$username = $user->data()->username;
$okay = true;
if ($okay) {
$comment_insert = "
INSERT INTO home_comments
(id, user_id, username, comment, date)
VALUES(?, ?, ?, ?, NOW())
";
$comment_stmt = $con->prepare($comment_insert);
$comment_stmt->bind_param('ssss', $id, $user_id, $username, $home_comment);
$comment_stmt->execute();
}
になりますあなたのwhileループと配列への代入が正しいかどうか確認してください。値を置き換えているようで、最後の値だけがそこにあるはずです。 – Aruna
ループの外側に結果をエコーしているので、最後の結果しか得られません。 –