简体   繁体   English

为什么我的 ajax 调用不在数据库中存储数据? 没有 PHP 或控制台错误

[英]Why doesn't my ajax call store data in database? No PHP or console errors

im trying to build a rating system of which you can rate from 1-5 stars and then the average rating is displayed.我正在尝试建立一个评分系统,您可以对其进行 1-5 星评分,然后显示平均评分。

For this im using Ajax, jQuery, PHP, MySQL, and HTML ofc.为此,我使用 Ajax、jQuery、PHP、MySQL 和 HTML ofc。

Here is the base code with the script and basic html:这是带有脚本和基本 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>

From what i can tell using 'console.log' to search for a fault in the script, the script is working as it should, so i figure the fault is within my ajax.php here: (Im getting 0 PHP errors, and no errors in console)据我所知,使用“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'");
        }
    } 
?>

The database connection is working properly, as i am a beginner with ajax i figured it would be good to ask someone here if someone could find the fault..数据库连接工作正常,因为我是 ajax 的初学者,我认为最好在这里询问某人是否有人能找到错误..

ALSO, HTML head for the script links etc.此外,脚本链接的 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>

**EDIT **编辑

I am including the connection like so:我包括这样的连接:

<?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);
?>

From php.net...从 php.net...

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

You dont provide a $link for your mysqli_query你没有为你的mysqli_query提供 $link

<?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'];

Notice where you declare $sql as your SQL query?请注意您在哪里将 $sql 声明为您的 SQL 查询? Notice the $conn in mysqli_query ?注意 mysqli_query 中的 $conn 吗? The $conn is your pointer/connector to the database. $conn 是指向数据库的指针/连接器。 It allows you to run different queries to different servers in parallel.它允许您并行地对不同的服务器运行不同的查询。

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

Now... where is your $conn in the mysqli_fetch_assoc below ?现在......你的 $conn 在下面的 mysqli_fetch_assoc 中在哪里?

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

And why is the mysqli_query below not have a $conn when previously you had?为什么下面的 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'");
    }
} 

?> ?>

Lastly, your console.log will show errors only from client/browser.最后,您的 console.log 将仅显示来自客户端/浏览器的错误。 You should have a php.log file on your server that will contain errors about your misuse of mysqli_query - if you don't know where these are, you are only going to bring extra work and head ache on to yourself when in practise you are within reaching distance of your goals.您的服务器上应该有一个 php.log 文件,其中将包含有关您误用 mysqli_query 的错误 - 如果您不知道这些错误在哪里,那么在实践中您只会带来额外的工作和头痛在达到目标的距离内。

Best of luck!祝你好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM