【发布时间】:2021-08-14 18:51:34
【问题描述】:
我正在使用 jquery 星级插件。链接是 (http://irfandurmus.com/projects/jquery-star-rating-plugin/)。我想点击星号,它会给我一个值,所以我可以用 php 将它传递给数据库。 我已经多次破坏插件试图让它工作,但无济于事。它在网站上显示得很好,但我无法将值存储在数据库中。也许我应该修改 js 文件但我不知道如何。这里是:
;(function($){
$.fn.rating = function(callback){
callback = callback || function(){};
// each for all item
this.each(function(i, v){
$(v).data('rating', {callback:callback})
.bind('init.rating', $.fn.rating.init)
.bind('set.rating', $.fn.rating.set)
.bind('hover.rating', $.fn.rating.hover)
.trigger('init.rating');
});
};
$.extend($.fn.rating, {
init: function(e){
var el = $(this),
list = '',
isChecked = null,
childs = el.children(),
i = 0,
l = childs.length;
for (; i < l; i++) {
list = list + '<a class="star" title="' + $(childs[i]).val() + '" />';
if ($(childs[i]).is(':checked')) {
isChecked = $(childs[i]).val();
};
};
childs.hide();
el
.append('<div class="stars">' + list + '</div>')
.trigger('set.rating', isChecked);
$('a', el).bind('click', $.fn.rating.click);
el.trigger('hover.rating');
},
set: function(e, val) {
var el = $(this),
item = $('a', el),
input = undefined;
if (val) {
item.removeClass('fullStar');
input = item.filter(function(i){
if ($(this).attr('title') == val)
return $(this);
else
return false;
});
input
.addClass('fullStar')
.prevAll()
.addClass('fullStar');
}
return;
},
hover: function(e){
var el = $(this),
stars = $('a', el);
stars.bind('mouseenter', function(e){
// add tmp class when mouse enter
$(this)
.addClass('tmp_fs')
.prevAll()
.addClass('tmp_fs');
$(this).nextAll()
.addClass('tmp_es');
});
stars.bind('mouseleave', function(e){
// remove all tmp class when mouse leave
$(this)
.removeClass('tmp_fs')
.prevAll()
.removeClass('tmp_fs');
$(this).nextAll()
.removeClass('tmp_es');
});
},
click: function(e){
e.preventDefault();
var el = $(e.target),
container = el.parent().parent(),
inputs = container.children('input'),
rate = el.attr('title');
matchInput = inputs.filter(function(i){
if ($(this).val() == rate)
return true;
else
return false;
});
matchInput
.prop('checked', true)
.siblings('input').prop('checked', false);
container
.trigger('set.rating', matchInput.val())
.data('rating').callback(rate, e);
}
});
})(jQuery);
mysql数据库存在如下
CREATE TABLE IF NOT EXISTS `rating` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`product_id` int(11) NOT NULL,
`vote` float NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
我想用这个php代码来获取星级值但是没办法。
<?php
function connect() {
$hostname = "localhost";
$username = "root";
$password = "root";
$dbname = "rating";
$con = mysqli_connect($hostname, $username, $password, $dbname);
return $con;
}
function getRatingByProductId($con, $productId) {
$query = "SELECT SUM(vote) as vote, COUNT(vote) as count from rating WHERE product_id = $productId";
$result = mysqli_query($con, $query);
$resultSet = mysqli_fetch_assoc($result);
if($resultSet['count']>0) {
return ($resultSet['vote']/$resultSet['count']);
} else {
return 0;
}
}
if(isset($_REQUEST['type'])) {
if($_REQUEST['type'] == 'save') {
$vote = $_REQUEST['vote'];
$productId = $_REQUEST['productId'];
$query = "INSERT INTO rating (product_id, vote) VALUES ('$productId', '$vote')";
// get connection
$con = connect();
$result = mysqli_query($con, $query);
echo 1; exit;
}
}
?>
【问题讨论】:
-
警告:您对SQL Injections 持开放态度,应该使用参数化的prepared statements,而不是手动构建查询。它们由PDO 或MySQLi 提供。永远不要相信任何形式的输入!即使您的查询仅由受信任的用户执行,you are still in risk of corrupting your data。 Escaping is not enough!
标签: javascript php jquery mysql