dongzhi5587 2017-07-28 09:38
浏览 58

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

Before I ask you my question I want to clarify that I'm just a rookie to Ajax and Jquery, so please spare me if the doubt is very small or a piece of cake, sorry for that.

I'm trying to create review system for my E-Commerce using Ajax and PHP. The problem is, the data is not inserting in to the database, but if I insert the the data manually in the database it displaying perfectly in my site.I think there is something going wrong with the variable review or user_review but couldn't find what it is.So, could you please tell me where I've done the mistake.

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

This is my 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条回答 默认 最新

  • dongzhan2029 2017-07-28 10:32
    关注

    There is a mistake you have done your script code is not closed

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

    To

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

    That is the reason your ajax is not working.

    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法