即时通讯试图建立一个评分系统,您可以从1-5星级评分,然后显示平均评分。为什么我的ajax调用不在数据库中存储数据?没有PHP或控制台错误
这个即时通讯使用Ajax,jQuery,PHP,MySQL和HTML ofc。
这是基础代码的脚本和基本的HTML:
<?php
include('includes/config.php');
$post_id = '1';
?>
<div class="ratesite">
<h4>Betygssätt denna webbplats!</h4>
<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>
<?php require_once 'includes/avgrate.php'; ?>
<div id="avg-rate">
<h5>Snittvärdet är <strong><?php echo $rate_value; ?></strong>.</h5>
</div>
</div>
<!-- Script för 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 = 'act=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 : "includes/ajax.php",
data: dataRate,
success:function(){}
});
});
});
</script>
从我可以告诉使用“的console.log”来搜索脚本故障,脚本工作,因为它应该,所以我想错是我的ajax.php中的位置:(即时得到0 PHP错误,并在控制台中没有错误)
<?php
require_once 'config.php';
if($_POST['act'] == 'rate'){
//Kontrollera ifall användaren (IP) redan röstat.
$ip = $_SERVER["REMOTE_ADDR"];
$therate = $_POST['rate'];
$thepost = $_POST['post_id'];
$sql = "SELECT * FROM ratings where ip= '$ip'";
$result = mysqli_query($conn, $sql);
while($data = mysqli_fetch_assoc($result)){
$rate_db[] = $data;
}
if(@count($rate_db) == 0){
mysqli_query("INSERT INTO ratings (id_post, ip, rate)VALUES('$thepost', '$ip', '$therate')");
}else{
mysqli_query("UPDATE ratings SET rate= '$therate' WHERE ip = '$ip'");
}
}
?>
数据库连接是否正常工作,就像我跟阿贾克斯初学者我想通如果有人能找到这个错误,在这里问一个人会很好。
此外,HTML头脚本链接等
<!DOCTYPE html>
<html>
<head>
<meta content="text/html; charset=utf-8" />
<!-- Visa användarnamn som titel i sidfliken -->
<title>Album</title>
<link rel="stylesheet" href="css/stylesheet.css" type="text/css" />
<!-- PIROBOX -->
<!-- -->
<link rel="stylesheet" type="text/css" href="css_pirobox/style_1/style.css"/>
<!--::: OR :::-->
<!-- <link rel="stylesheet" type="text/css" href="css_pirobox/style_2/style.css"/> -->
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/jquery-ui-1.8.2.custom.min.js"></script>
<script type="text/javascript" src="js/pirobox_extended.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$().piroBox_ext({
piro_speed : 900,
bg_alpha : 0.1,
piro_scroll : true //pirobox always positioned at the center of the page
});
});
</script>
</head>
**编辑
我包括像这样的连接:
<?php
$dbhost = 'xxxxx';
$dbuser = 'xxxxx';
$dbpass = 'xxxxx';
$dbname = 'xxxxx';
$conn = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname)
or die('Kunde inte ansluta till databas');
$db_connected = mysqli_select_db($conn, $dbname);
?>
看起来您的更新和插入查询缺少连接变量。一般格式是mysqli_query($ connection,$ query);它看起来像你缺少$连接。这是我对你的问题的快速猜测。 –
[您的脚本存在SQL注入攻击风险。](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php)了解[准备](http: //en.wikipedia.org/wiki/Prepared_statement)[MySQLi]的声明(http://php.net/manual/en/mysqli.quickstart.prepared-statements.php)。 –
您正在假设您的查询正常工作。您需要执行一些错误检查。 –