如果两个输入尚不存在,我怎么才能插入数据库?

时间:2016-06-22 13:32:08

标签: php mysql

我想给完成某些任务的用户提供“奖励”,例如达到50级。我还想记录所有内容,以防出现问题并且管理员必须手动插入。

奖励的php链接在每个页面上并检查来自“用户”表的用户是否达到了50级。一旦用户达到50级,它将插入用户名,奖励,时间,原因以及谁将奖励输入一张名为'奖项'的表。

但是如果用户是50级,则每次用户重新加载或转到新页面时,它将继续插入“奖励”表。如果用户尚未获得特定奖励,我该如何更新?

if(empty($_SESSION['user'])){

}else{
    $member_id = htmlentities($_SESSION['user']['id'], ENT_QUOTES, 'UTF-8');

    $db_conn = mysqli_connect("localhost", "root", "password", "a276") or die ("Could not connect to database");
    $set_award_query = mysqli_query ($db_conn, "SELECT * FROM users WHERE id='$member_id'");

    while($row = mysqli_fetch_array($set_award_query, MYSQLI_ASSOC)){
        $member_id = htmlentities($_SESSION['user']['id'], ENT_QUOTES, 'UTF-8');
        $member_username = htmlentities($_SESSION['user']['username'], ENT_QUOTES, 'UTF-8');
        $member_level = $row['level'];
    }

//Award level50
if($member_level == '50'){
    $award_sql = "INSERT INTO `awards` (`awardusername`, `awardby`, `award`, `awardmessage`) VALUES ('$member_username', 'System', 'level50', 'Congratulations on reaching level 50!')";
    $db_conn->query($award_sql);
}else{}

3 个答案:

答案 0 :(得分:1)

您可以在UNIQUE INDEXawardusername列上创建award

ALTER TABLE `awards` ADD UNIQUE INDEX awards_key (awardusername, award);

这将阻止同一用户和级别的第二次INSERT,并将抛出1062错误,您可以轻松处理我相信。

答案 1 :(得分:0)

在奖励表

中创建user_id列

检查$ member_id是否与奖励表中的user_id匹配。

如果没有匹配项,则将user_id($ member_id)存储在奖励表中。

// Get the user_id from the award table (Example, this code won't work)

$user_id = "SELECT user_id FROM awards WHERE user_id = $member_id";

// Check if they match

if( $member_id !== $user_id ):
    // Insert into awards table
endif;

答案 2 :(得分:0)

我找到了解决问题的方法。它可能是一种愚蠢的方式(我不知道),但它有效

    $member_id = htmlentities($_SESSION['user']['id'], ENT_QUOTES, 'UTF-8');

    $db_conn = mysqli_connect("localhost", "root", "password", "a276") or die ("Could not connect to database");
    $set_award_query = mysqli_query ($db_conn, "SELECT * FROM users WHERE id='$member_id'");
    while($row = mysqli_fetch_array($set_award_query, MYSQLI_ASSOC)){
        $member_id = htmlentities($_SESSION['user']['id'], ENT_QUOTES, 'UTF-8');
        $member_username = htmlentities($_SESSION['user']['username'], ENT_QUOTES, 'UTF-8');
        $member_level = $row['level'];
    }

    $check_award_exist_query = mysqli_query ($db_conn, "SELECT * FROM awards WHERE awardusername='$member_username'");
    while($row = mysqli_fetch_array($check_award_exist_query, MYSQLI_ASSOC)){
        $award_username = $row['awardusername'];
        $selected_award = $row['award'];
    }

//Award level50
if($member_username){
    if($member_level == '50'){
        if($selected_award == 'level50'){

        }else{
            $award_sql = "INSERT INTO `awards` (`awardusername`, `awardby`, `award`, `awardmessage`) VALUES ('$member_username', 'System', 'level50', 'Congratulations on reaching level 50!')";
            $db_conn->query($award_sql);
        }
    }else{}
}else{}

感谢所有花时间试图帮助我的人! :)