2016-03-01 37 views
-2

即時通訊試圖建立一個評分系統,您可以從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); 
?> 
+2

看起來您的更新和插入查詢缺少連接變量。一般格式是mysqli_query($ connection,$ query);它看起來像你缺少$連接。這是我對你的問題的快速猜測。 –

+0

[您的腳本存在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)。 –

+0

您正在假設您的查詢正常工作。您需要執行一些錯誤檢查。 –

回答

0
<?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作爲你的SQL查詢嗎?注意mysqli_query中的$ conn? $ conn是你的數據庫的指針/連接器。它允許您並行地向不同的服務器運行不同的查詢。

$sql = "SELECT * FROM ratings where ip= '$ip'"; 
    $result = mysqli_query($conn, $sql); 

現在...您的$ conn位於下面的mysqli_fetch_assoc中哪裏?

while($data = mysqli_fetch_assoc($result)){ 
     $rate_db[] = $data; 
    } 

爲什麼以前的mysqli_query沒有$ conn?

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'"); 
    } 
} 

?>

最後,你的console.log將只顯示來自客戶端/瀏覽器的錯誤。你應該在你的服務器上有一個php.log文件,其中包含有關你濫用mysqli_query的錯誤 - 如果你不知道這些文件在哪裏,你只會在練習時帶上額外的工作和頭痛。在達到你的目標的距離內。

祝你好運!

0

從php.net ...

mysqli_query (mysqli $link , string $query [, int $resultmode = MYSQLI_STORE_RESULT ]); 

您不提供$ link爲您的mysqli_query

相關問題