简体   繁体   English

如果只有两个输入不存在,我如何才能插入数据库?

[英]How can i only insert into a database if two inputs doesn't already exist?

I want to give "awards" to users who complete certain tasks such as getting to level 50. I also want to log everything in case something goes wrong and an admin has to manually insert it. 我想向完成某些任务(例如达到50级)的用户提供“奖励”。我还想记录所有内容,以防万一出问题并且管理员必须手动将其插入。

The php for awards is linked on every page and checks if the user from 'users' table has reached level 50. Once the user is level 50 it will insert username, award, time, reason, and who gave the award into a table called 'awards'. 奖励php链接在每个页面上,并检查“用户”表中的用户是否已达到50级。一旦用户达到50级,它将在表格中插入用户名,奖励,时间,原因以及授予奖励的人“奖项”。

But if the user is level 50 it will continue inserting into the 'awards' table every time the user reload or goes to a new page. 但是,如果用户为50级,则每次用户重新加载或进入新页面时,它将继续插入“奖励”表中。 How can i only update if the user doesn't already have the specific award? 如果用户尚未获得特定奖励,我该如何更新?

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{}

You can create an UNIQUE INDEX over awardusername and award columns: 您可以在awardusernameaward列上创建UNIQUE INDEX

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

This will prevent second INSERT for the same user and level and will throw 1062 error which you can handle easily I believe. 这将防止同一用户和同一级别的第二次INSERT插入,并且会引发1062错误,您相信我可以轻松处理该错误。

Create a user_id column in the awards table 在奖赏表中创建一个user_id列

Check if the $member_id matches the user_id in the awards table. 检查$ member_id是否与奖赏表中的user_id相匹配。

Store the user_id($member_id) in the awards table if there is no match. 如果没有匹配项,请将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;

I found a way myself to fix the issue. 我自己找到了解决此问题的方法。 It might be a stupid way or something (I don't know) but it works 这可能是一种愚蠢的方式(或其他)(我不知道),但它确实有效

    $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{}

Thanks to everyone who took their time to try to help me out! 感谢所有花时间尝试帮助我的人! :) :)

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

相关问题 如果数据库中不存在则插入 - INSERT if doesn't already exist in database 我想验证我的数据库“名称和组”中的两列是否存在显示已存在,如果不存在则插入到数据库中 - I want to validate two columns from my database “name and group” if it exists display already exists and if it doesn't exist insert into the database 如果更新多行时该行尚不存在,该如何插入? - How can I insert a row if it doesn't already exist while updating multiple rows? SQL查询 - INSERT,但仅限于记录尚不存在? - SQL Query - INSERT but only if record doesn't already exist? 仅当记录不存在时才如何添加到MySQL表中? - How to add to a MySQL table only if the record doesn't already exist? 仅在MySQL表中尚不存在的情况下插入数据:INSERT IGNORE出现问题 - Inserting data in MySQL table only if it doesn't exist already: trouble with INSERT IGNORE 仅将数据从一个表插入到另一个表中 - Insert data from one table to another only if it doesn't already exist (MYSQL) 如何仅在表中不存在记录时插入记录 - (MYSQL) How to insert a record only if it doesn't exist in a table 仅在值不存在时插入 - Only insert if the value doesn't exist 使用ajax和php插入数据库后,如何清除输入? - how can i clear inputs after insert into database with ajax and php?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM