PHP在评级系统中添加评论框

时间:2016-02-21 15:19:02

标签: javascript php mysql ajax

更新 - 已在代码中添加了javascript来处理第一个答案中建议的注释插入,但遗憾的是没有插入注释,任何人都可以建议为什么注释没有提交到我的数据库?< /强>

我是编码的新手,首先我知道我在我的代码中使用了mysql函数,并且在PHP的未来版本中它们已被弃用,我正在运行5.5。我有几天时间让我当前的代码使用这个想要的功能,这是一个扔掉的原型,所以现在我坚持使用它们。我真的很感激没有关于这些功能的评论,而是在我的实际问题上提出积极的反馈意见:)

我创建了一个按预期工作的评分系统,我想添加一个评论框,以便用户也可以评论我的视频。我已将以下代码添加到索引文件的底部。

<div id="addCommentContainer">
    <p>Please leave a comment on what you thought of the tutorial</p>
    <form id="addCommentForm" method="post" action="" >
        <div>
            <textarea name="body" id="body" cols="60" rows="5"></textarea>
        <br>
            <input type="submit" id="submit" value="Submit" />

我显然在我的数据库中添加了变量和一列来接收注释。但是,我的评分提交,但我的评论没有插入我的数据库。这可能是由于我的评分和评论的帖子有冲突吗?

有人可以建议如何将这些POSTS合并到一起插入吗?我附上了索引和ajax文件。

的index.php

<?php  
    session_start();
    include_once 'dbconnect.php';

    if( !isset( $_SESSION['user'] ) ) header("Location: index.php");

    $res=mysql_query("SELECT * FROM users WHERE user_id=".$_SESSION['user']);
    $userRow=mysql_fetch_array( $res );

        $post_id = 1; 
    ?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script src="https://code.jquery.com/jquery-1.12.0.min.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-20" />
<title>Welcome - <?php echo $userRow['username']; ?></title>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<div id="header">
 <div id="left">
    <label>NHSCT eNISAT Tutorials</label>
    </div>
    <div id="right">
     <div id="content">
         Welcome <?php echo $userRow['forename']; ?>&nbsp;<a href="eNISATVids.php?home">Return to Tutorials page</a>&nbsp;&nbsp;<a href="logout.php?logout">Sign Out</a>
        </div>
    </div>
    <br>
    <br>
    <br>
    <br>
    <br>
<p align="center"><img src="title.jpeg" width="400" height="100" alt="title.jpeg">
<br>
  <link rel="stylesheet" href="style.css" type="text/css" />
        <link type="text/css" rel="stylesheet" href="css/example.css">


    <div class="tuto-cnt">

        <div class="rate-ex1-cnt">
            <div id="1" class="rate-btn-1 rate-btn"></div>
            <div id="2" class="rate-btn-2 rate-btn"></div>
            <div id="3" class="rate-btn-3 rate-btn"></div>
            <div id="4" class="rate-btn-4 rate-btn"></div>
            <div id="5" class="rate-btn-5 rate-btn"></div>
        </div>

        <hr>

        <div class="box-result-cnt">
            <?php
                $query = mysql_query("SELECT * FROM enisat1_rate"); 
                while($data = mysql_fetch_assoc($query)){
                    $rate_db[] = $data;
                    $sum_rates[] = $data['rate'];
                }
                if(@count($rate_db)){
                    $rate_times = count($rate_db);
                    $sum_rates = array_sum($sum_rates);
                    $rate_value = $sum_rates/$rate_times;
                    $rate_bg = (($rate_value)/5)*100;
                }else{
                    $rate_times = 0;
                    $rate_value = 0;
                    $rate_bg = 0;
                }
            ?>
            <hr>
            <h3>This tutorial has been rated <strong><?php echo $rate_times; ?></strong> times.</h3>
            <hr>
            <h3>The average rating is <strong><?php echo $rate_value; ?></strong> stars.</h3>
            <hr>
            <div class="rate-result-cnt">
                <div class="rate-bg" style="width:<?php echo $rate_bg; ?>%"></div>
                <div class="rate-stars"></div>
            </div>
            <hr>
            <center>
    <div id="addCommentContainer">
    <p>Leave a comment on what you thought of the tutorial</p>
    <br>
    <form id="addCommentForm" method="post" action="eNISATVids.php" >
        <div>
            <textarea name="comments" id="comments" cols="50" rows="5"></textarea>
        <br>
            <input type="submit" id="submitcomment" value="Submit" />
    </form>
        </div><!-- /rate-result-cnt -->

    </div><!-- /tuto-cnt -->

    <script>

     $( document ).ready(function() {

        // rating script
        $(function(){ 
            $('.rate-btn').hover(function(){
                $('.rate-btn').removeClass('rate-btn-hover');
                var therate = $(this).attr('id');
                for (var i = therate; i >= 0; i--) {
                    $('.rate-btn-'+i).addClass('rate-btn-hover');
                };
            });

            $('.rate-btn').click(function(){    
                var therate = $(this).attr('id');
                var dataRate = 'submit=rate&post_id=<?php echo $post_id; ?>&rate='+therate; //
                $('.rate-btn').removeClass('rate-btn-active');
                for (var i = therate; i >= 0; i--) {
                    $('.rate-btn-'+i).addClass('rate-btn-active');
                };

                $.ajax({
                    type : "POST",
                    url : "http://localhost/Tna/E1ajax.php",
                    data: dataRate,
                    success:function(){}
                });

            });
        });

        // javascript for handling your comment:
$("#submitcomment").click(function() {
     var comments = $("#comments").val();
     $.ajax({
        url: "http://localhost/Tna/E1ajax.php",
        type: "POST",
        data: { "comments": comments },
        success: function(back)
            {
            alert(back);
            }
        });
});
});  

    </script>

</body>
</html> 

Ajax.php

<?php

session_start();
include_once 'dbconnect.php';

if(!isset($_SESSION['user']))
{
 header("Location: index.php");
}
$res=mysql_query("SELECT * FROM users WHERE user_id=".$_SESSION['user']);
$userRow=mysql_fetch_array($res);

    if($_POST['submit'] == 'rate'){
        //search if the userID has already gave a note
        $userID=$_SESSION['user'];
        $therate = $_POST['rate'];
        $thepost = $_POST['post_id'];
        $comments = $_POST['comments'];

        $query = mysql_query("SELECT * FROM enisat1_rate where user_id= '$userID'"); 
        while($data = mysql_fetch_assoc($query)){
            $rate_db[] = $data;
        }

        if(@count($rate_db) == 0 ){
            mysql_query("INSERT INTO enisat1_rate (id_post, user_id, rate, comments )VALUES('$thepost', '$userID', '$therate', '$comments')");
        }else{
            mysql_query("UPDATE enisat1_rate SET rate= '$therate' WHERE user_id = '$userID'");
        }
    } 
?>

谢谢

0 个答案:

没有答案