[英]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: 您可以在
awardusername
和award
列上创建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.