私が質問する前に、私はAjaxとJqueryのルーキーであることを明確にしたいので、疑問が非常に小さいかケーキの場合は、私を助けてください。申し訳ありません。データがデータベースに挿入されていませんが、PHPとAjaxを使用して取得が正常に動作していますか?
私はAjaxとPHPを使用して自分のEコマースのレビューシステムを作成しようとしています。問題は、データがデータベースに挿入されていないことですが、データベースに手動でデータを挿入すると、私のサイトに完全に表示されます。変数のレビューやuser_reviewに問題があると思われますが、それが何であるか。だから、私が間違いを犯した場所を教えてください。
<div role="tabpanel" class="tab-pane" id="reviews">
<h4>Write your Review</h4>
<form action="" method="post" onsubmit="return post();">
<textarea id="review" class="reviewbox" placeholder="Write Your Review Here....."></textarea>
<button type="submit" class="btn">Submit</button>
</form>
<div id="all_reviews">
<?php
$query = $pdo->prepare("SELECT * FROM reviews WHERE product_id=?");
$query -> bindValue(1, $id);
$query->execute();
while ($row = $query->fetch(PDO::FETCH_ASSOC))
{
$name = $row['user_name'];
$text = $row['review_text'];
$time = $row['post_time'];
?>
<h5>By <?php echo $name; ?></h5>
<p><i>posted on <?php echo $time; ?></i></p>
<p>
<?php echo $text; ?>
</p>
<hr>
<?php
}
?>
</div>
<script type="text/javascript" src="jquery.js">
< script type = "text/javascript" >
function post() {
var review = document.getElementById("review").value;
if (review) {
$.ajax({
type: 'POST',
url: 'post_reviews.php',
data: {
user_review: review
},
success: function(response) {
document.getElementById("all_reviews").innerHTML = response + document.getElementById("all_reviews").innerHTML;
document.getElementById("review").value = "";
}
});
}
return false;
}
</script>
</div>
これは私のpost_reviews.php
です:
<?php
session_start();
require('includes/product.php');
require('includes/connect.php');
$product = new Product;
if(isset ($_GET['id'])) {
$id = $_GET['id'];
$data = $product -> fetch_data($id);
if(isset($_POST['user_review'])){
$review=$_POST['user_review'];
if (isset($_SESSION['logged_in'])) {
$query = $pdo -> prepare("INSERT INTO reviews(product_id,user_name,review_text) VALUES (?,?,?)");
$query -> bindValue(1, $id);
$query -> bindValue(2, $_SESSION['name']);
$query -> bindValue(3,$review);
$query ->execute();
}
else{
$review_msg="Please login to post your review";
}
$query = $pdo->prepare("SELECT * FROM reviews WHERE product_id=?");
$query -> bindValue(1, $id);
$query->execute();
while ($row = $query->fetch(PDO::FETCH_ASSOC)){
$name = $row['user_name'];
$text = $row['review_text'];
$time = $row['post_time'];
?>
<?php if(isset($review_msg)){ ?>
<small style = "color : #aa0000"; ><?php echo $review_msg ?></small>
<br><br>
<?php } ?>
<h5>By <?php echo $name; ?></h5>
<p><i>posted on <?php echo $time; ?></i></p>
<p><?php echo $text; ?></p>
<hr>
<?php
}
}
exit;
}
?>
あなたのajaxコードが機能しているかどうか、あなたのコンソールで確認してください。 –
テーブル構造を共有できますか?また、...あなたの$ _GET ['id']は設定されていないと思うので、あなたのインサートがうまくいきません。 idをajaxデータに渡し、$ _POSTをレビューのために使ったのと同じ方法で使用してください。 – Vladut
ファイル内でprint/writeを使用してコードをデバッグし、挿入クエリを出力してから手動で実行することができます。 – ggupta