Javascript 星级系统-连接js与PHP和MySQL
我有这个 以及连接以下各项的PHP文件(rating.PHP):Javascript 星级系统-连接js与PHP和MySQL,javascript,php,mysql,system,rating,Javascript,Php,Mysql,System,Rating,我有这个 以及连接以下各项的PHP文件(rating.PHP): <?php // Error function that stops script processing with die function error($message){ $output = json_encode( array( 'result' => 'error', 'msg' => $message )
<?php
// Error function that stops script processing with die
function error($message){
$output = json_encode(
array(
'result' => 'error',
'msg' => $message
)
);
die($output);
}
// Round to the nearest 0.5
function roundToNearestHalf($number){
return round($number * 2) / 2;
}
// Instantiate database connection
$db = new mysqli('localhost', 'root', 'XXX', 'XXX');
// Check that the connection worked
if($db->connect_errno > 0){
error('Unable to connect to database [' . $db->connect_error . ']');
}
// Verify that we have enough post items
if(count($_POST) > 1){
error('Too many post items received.');
}
// Check that the rating was entered
if(!isset($_POST['rating']) || $_POST['rating'] == ''){
error('No rating value provided.');
}
// Valid the rating amount that was entered.
if(!preg_match("/[0-5](?:\.5)/", $_POST['rating']) && $_POST['rating'] < 0 && $_POST['rating'] > 5){
error('Invalid rating provided.');
}
// Check if the user has rated before
$sql = <<<SQL
SELECT `ratingid`
FROM `ratings`
WHERE `ip` = '{$_SERVER['REMOTE_ADDR']}'
SQL;
if(!$result = $db->query($sql)){
error('There was an error running the query [' . $db->error . ']');
}
// Tell the user that they have voted already.
if($result->num_rows){
error('That IP address has already voted, please try using another IP.');
}
// Store the user's rating.
$rating = $db->escape_string($_POST['rating']);
$sql = <<<SQL
INSERT INTO `ratings`
(`rating`, `ip`)
VALUES ('{$rating}', '{$_SERVER['REMOTE_ADDR']}')
SQL;
if(!$db->query($sql)){
error('Unable to insert rating to database [' . $db->error . ']');
}
// Get the average rating
$sql = <<<SQL
SELECT AVG(`rating`) AS `rating`
FROM `ratings`
SQL;
if(!$result = $db->query($sql)){
error('There was an error running the query [' . $db->error . ']');
}
// Fetch the average rating
$data = $result->fetch_assoc();
$rating = $data['rating'];
// Output the average rating for the front end to handle
$output = json_encode(
array(
'result' => 'success',
'rating' => roundToNearestHalf($rating)
)
);
echo $output;
?>
您的评分表没有为哪个项目评分的字段?好吧,现在我只是测试是否收到数据,解决了这个问题后,我将把我的表与产品和估价联系起来。这可能是因为没有将它们关联到first?你能帮我吗?感谢错误消息所指的行是什么?文件结尾:“?>”-“未找到元素”rating.php:93:3
CREATE TABLE `rating` (
`ratingid` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
`rating` FLOAT UNSIGNED NOT NULL,
`time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`ip` VARCHAR( 39 ) NOT NULL,
UNIQUE (`ip`)
) ENGINE = INNODB
<?php
// Error function that stops script processing with die
function error($message){
$output = json_encode(
array(
'result' => 'error',
'msg' => $message
)
);
die($output);
}
// Round to the nearest 0.5
function roundToNearestHalf($number){
return round($number * 2) / 2;
}
// Instantiate database connection
$db = new mysqli('localhost', 'root', 'XXX', 'XXX');
// Check that the connection worked
if($db->connect_errno > 0){
error('Unable to connect to database [' . $db->connect_error . ']');
}
// Verify that we have enough post items
if(count($_POST) > 1){
error('Too many post items received.');
}
// Check that the rating was entered
if(!isset($_POST['rating']) || $_POST['rating'] == ''){
error('No rating value provided.');
}
// Valid the rating amount that was entered.
if(!preg_match("/[0-5](?:\.5)/", $_POST['rating']) && $_POST['rating'] < 0 && $_POST['rating'] > 5){
error('Invalid rating provided.');
}
// Check if the user has rated before
$sql = <<<SQL
SELECT `ratingid`
FROM `ratings`
WHERE `ip` = '{$_SERVER['REMOTE_ADDR']}'
SQL;
if(!$result = $db->query($sql)){
error('There was an error running the query [' . $db->error . ']');
}
// Tell the user that they have voted already.
if($result->num_rows){
error('That IP address has already voted, please try using another IP.');
}
// Store the user's rating.
$rating = $db->escape_string($_POST['rating']);
$sql = <<<SQL
INSERT INTO `ratings`
(`rating`, `ip`)
VALUES ('{$rating}', '{$_SERVER['REMOTE_ADDR']}')
SQL;
if(!$db->query($sql)){
error('Unable to insert rating to database [' . $db->error . ']');
}
// Get the average rating
$sql = <<<SQL
SELECT AVG(`rating`) AS `rating`
FROM `ratings`
SQL;
if(!$result = $db->query($sql)){
error('There was an error running the query [' . $db->error . ']');
}
// Fetch the average rating
$data = $result->fetch_assoc();
$rating = $data['rating'];
// Output the average rating for the front end to handle
$output = json_encode(
array(
'result' => 'success',
'rating' => roundToNearestHalf($rating)
)
);
echo $output;
?>