数据没有插入数据库,但使用PHP和Ajax检索工作正常吗?

时间:2017-07-28 09:38:33

标签: php jquery mysql ajax

在我问你的问题之前,我想澄清一下,我只是阿贾克斯和Jquery的新手,所以如果怀疑很小或者是小菜一碟,请饶恕我,对不起。

我正在尝试使用Ajax和PHP为我的电子商务创建评论系统。问题是,数据没有插入数据库,但如果我在数据库中手动插入数据,它在我的网站中完美显示。我认为变量审查或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;
    }
?>

4 个答案:

答案 0 :(得分:1)

你的脚本代码没有关闭,这是一个错误

 <script type="text/javascript" src="jquery.js">

 <script type="text/javascript" src="jquery.js"></script>

这就是你的ajax无法正常工作的原因。

答案 1 :(得分:0)

首先检查你的ajax工作与否点击按钮

答案 2 :(得分:0)

var review = document.getElementById(&#34; review&#34;)。value;

$.ajax({
    type: "POST",
    url: "post_reviews.php",
    data: review,
    cache: false,
    dataType: 'json',
    success: function(response){
        document.getElementById("all_reviews").innerHTML = response + document.getElementById("all_reviews").innerHTML;
        document.getElementById("review").value = "";
    }


});

return false; 

答案 3 :(得分:0)

现在我明白了。您正在提交表单。

 <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>

尝试用以下内容替换您的表单:

 <form action="" method="post">
  <textarea id="review" class="reviewbox" placeholder="Write Your Review 
             Here....."></textarea>
  <button type="button" onclick="post()" class="btn">Submit</button>
</form>

如果按钮类型是&#34;按钮&#34;它不会提交表格。现在单击它将调用您的函数并执行ajax调用。