试图将krajee bootstrap星级的星级评级发送到mysql数据库

时间:2015-07-14 10:31:55

标签: php jquery mysql

我正在使用以下插件

http://plugins.krajee.com/star-rating

我试图将提交的评分发送到数据库但是没有记录这些值。

require_once $doc_root . '/includes/act_initiate_article_xref.php';

    $rating_value = (@$user_article_xref_row['rating'] > 0) ? $user_article_xref_row['rating'] . ' stars' : 'unrated';
    $article_rating = '<span class="line-sep">Rated:</span> ' . $rating_value;
if ($_SESSION['user_id'] == 1 || $_SESSION['user_id'] == 41)    {
    $rating_value_attr = (isset ($user_article_xref_row['rating'])) ? ' value="' . $user_article_xref_row['rating'] . '"' : '';
    $article_rating .= '<span class="line-sep">Your rating: </span> <input id="input-21d" type="number" class="rating"' . $rating_value_attr . ' data-min=0 data-max=5 step=0.5 data-size="xs">';
}

以及act_initiate_article_xref.php的代码

<?

/**
 * reads user_article_xref
 */
$this_routine[] = "includes/act_initiate_article_xref.php";

/**
 * no direct access allowed
 */
$doc_root = $_SERVER['DOCUMENT_ROOT'];
require_once $doc_root . '/includes/act_check_valid_access.php';

$table_title = 'user_article_xref';

$user_id = (isset ($_SESSION['user_id'])) ? $_SESSION['user_id'] : 0;

$fields = '`id`, `hits`, `rating`';
$match_where = '`user_id` = ' . $user_id . ' and `article_id` = ' . $article_id;
$article_result = $db->selectByStrings($fields, $table_title, $match_where, null, 1);
#if ($_SESSION['user_id'] == 1) { echo "<br>21. select $fields from $table_title where $match_where<pre>";print_r($_SESSION);echo "</pre>"; }

/**
 * if there is already a record, update it
 * if there isn't already a record, insert it
 * either way, $user_article_xref_row holds details for any ajax rating update
 */
if ($db->getNumRows($article_result) > 0)   {
    $user_article_xref_row = $db->getNextRow($article_result);
    $hits = $user_article_xref_row['hits'] + 1;
    $pairs_array = array ('hits' => $hits);
    $id_where = '`id` = ' . $user_article_xref_row['id'];
    $update_result = $db->updateByArray($table_title, $pairs_array, $id_where);
    $test = 'update';
} else {
    $hits = 1;
    $user_article_xref_row = array ('user_id' => $user_id, 'article_id' => $article_id, 'hits' => $hits);
    $insert_result = $db->insertByArray($table_title, $user_article_xref_row);
    $test = 'insert';
}
#if ($_SESSION['user_id'] == 1) { echo "<br>37. $test<pre>";print_r($user_article_xref_row);echo "</pre>"; }

?>

1 个答案:

答案 0 :(得分:0)

我知道这篇文章有点旧,但以防万一。在star-rating.js中,我在我的php文件中添加了一个ajax帖子到set.listenClick函数。希望这会有所帮助。

self.listenClick(self.$rating, function(e) {
                if (self.inactive) {
                    return false;
                }
                pos = self.getPosition(e);
                var div_id = self.$element.attr('id');
                self.setStars(pos);
                self.$element.trigger('change').trigger('rating.change', [self.$element.val(), self.$caption.html()]);
                self.starClicked = true;
                var rating = self.$element.val();
                $.ajax({
                type:"POST",
                url:"rating.php",
                data:"div="+div_id+"&rating="+rating,
                success: function(res){
                 console.log("Rating was posted " + div_id + rating);
                                             return res;
                }
            });
            });