sql插入两次

时间:2015-03-09 07:59:00

标签: php sql

你好,我有一个sql语句,对我的记录进行排名,并将排名插入数据库。

我遇到的问题是当我运行脚本时。它删除了旧的排名。并且插入新的排名很好,但它会将每个排名插入两次。因此,将有2个等级1,2个等级2,等等。

问题的解决方案是,如果我删除phpmyadmin中的所有记录。然后运行脚本。它不会重复。

我尝试让脚本删除,调试删除脚本是否正常工作,并删除了所有行。因此我真的很困惑:(

如果你能查看我的代码并给我一些很棒的见解。谢谢

<?php

// Connect to the database we want to insert/update
$Server_Location = "localhost";
$Server_User_Name = "x";
$Server_Password = "y";
$Database_Name = "z";

// Create connection
$Conn_Info = mysqli_connect($Server_Location, $Server_User_Name, $Server_Password, $Database_Name);

// Check connection
if ($Conn_Info->connect_error) {
    die("Connection failed: " . $Conn_Info->connect_error);
} 

if($_GET['Password'] != 'q'){
    die();
}


$Sql_Delete_Duplicate = "DELETE FROM ranks";

if ($Conn_Info->query($Sql_Delete_Duplicate) === TRUE) {
    echo "User Deleted";
} 
else {
    echo "Error User Not Deleted";
    echo $Conn_Info->error;
}




// Overall Games - Rankings (1000)
$Top_1000_Sql = "Select games, overall_games_user.ID as UserID, Name FROM overall_games, overall_games_user WHERE overall_games.User_ID = overall_games_user.ID AND DATE(Date_Updated) = DATE(NOW()) GROUP BY User_ID ORDER BY Games DESC LIMIT 10000";

$Top_1000_Results = $Conn_Info->query($Top_1000_Sql);

$rank = 0;

if ($Top_1000_Results->num_rows > 0) {
    echo $Top_1000_Results->num_rows;
    while($Top_Player = $Top_1000_Results->fetch_assoc()) {

        $rank += 1;
        $User_ID = $Top_Player["UserID"];
        $Games_Played = $Top_Player["games"];

        $Insert_Top_Player_Sql = "INSERT INTO ranks (Rank_Type,Rank, User_ID, games)
                            VALUES ('Total', {$rank}, {$User_ID}, {$Games_Played})";


        if ($Conn_Info->query($Insert_Top_Player_Sql) === TRUE) {
            echo $rank . "-";
        } 
        else {
            echo "Error User Not Added";
            echo $Conn_Info->error;
        }

    }
}

// Yesterday Games - Rankings






// Graph Stats

/*
SELECT AVG(Games) FROM overall_games WHERE DATE(Date_Updated) = DATE(NOW()) AND User_ID IN (SELECT ID FROM overall_games_user WHERE division LIKE('%BRONZE%'));
SELECT AVG(Games) FROM overall_games WHERE DATE(Date_Updated) = DATE(NOW()) AND User_ID IN (SELECT ID FROM overall_games_user WHERE division LIKE('%SILVER%'));
SELECT AVG(Games) FROM overall_games WHERE DATE(Date_Updated) = DATE(NOW()) AND User_ID IN (SELECT ID FROM overall_games_user WHERE division LIKE('%GOLD%'));
SELECT AVG(Games) FROM overall_games WHERE DATE(Date_Updated) = DATE(NOW()) AND User_ID IN (SELECT ID FROM overall_games_user WHERE division LIKE('%PLATINUM%'));
SELECT AVG(Games) FROM overall_games WHERE DATE(Date_Updated) = DATE(NOW()) AND User_ID IN (SELECT ID FROM overall_games_user WHERE division LIKE('%DIAMOND%'));

*/

?>

2 个答案:

答案 0 :(得分:1)

您正在使用Group By而不使用聚合功能,因此请将您的查询更改为:

$Top_1000_Sql = "Select Count(games) as gameCount, overall_games_user.ID as UserID, Name FROM overall_games, overall_games_user WHERE overall_games.User_ID = overall_games_user.ID AND DATE(Date_Updated) = DATE(NOW()) GROUP BY UserID ORDER BY Games DESC LIMIT 10000";

另请注意上述SQL中 GROUP BY UserID 的更改

$ Games_Played变量提取值count:

$Games_Played = $Top_Player["gameCount"];

答案 1 :(得分:0)

  1. 尝试在查询中将VALUES更改为VALUE。

  2. 有时也会发生.htaccess指向索引文件中的所有内容。当然,浏览器会两次请求脚本,一次是脚本,另一个是favicon。

  3. 修复方法是尝试编辑.htaccess以防止在浏览器请求favicon.ico时重定向到索引文件

    尝试这两件事。我希望它能解决你的问题:)请告诉我再说一遍。