简体   繁体   English

PHP将条目两次提交到数据库

[英]PHP submitting entries to database twice

I have a game where you can submit your score to a database, but for some reason the submission keeps getting triggered twice. 我有一个游戏,您可以在其中将分数提交到数据库,但是由于某种原因,提交一直被触发两次。 Every entry is doubled. 每个条目都加倍。 I've seen similar problems posted here, and the solution had to do with the if/else check at the end, but I don't see a problem. 我在这里看到过类似的问题,解决方案与最后的if / else检查有关,但是我没有看到问题。

Is it my PHP code that's duplicating the entries or my game application? 是我的PHP代码重复了条目还是我的游戏应用程序?

<?php

$servername = "xxx";
$username = "xxx";
$password = "xxx";
$dbname = "xxx";
$port = "xxx";

$link = mysqli_connect($servername, $username, $password, $dbname, $port);


// Security
$playerInitials = mysqli_real_escape_string($link,$_REQUEST['initials']);
$playerEmail = mysqli_real_escape_string($link,$_REQUEST['email']);
$playerScore = mysqli_real_escape_string($link,$_REQUEST['score']);

// Convert Initials to Upper Case
$playerInitialsUC = strtoupper($playerInitials);


$sql = "INSERT INTO xmas (initials, email, score)
VALUES ('$playerInitialsUC', '$playerEmail', '$playerScore')";


if(mysqli_query($link, $sql)){
    echo "Records added successfully.";
} else{
    echo "ERROR: " . mysqli_error($link);
}


mysqli_close($link);

?>

You can try this in your sql query: 您可以在SQL查询中尝试以下操作:

REPLACE does exactly what INSERT does but it won't let sql query double a record. REPLACE的功能与INSERT完全相同,但它不会让sql查询将记录翻倍。

REPLACE into xmas (initials, email, score) values('$playerInitialsUC', '$playerEmail', '$playerScore')

You can tell me if it didn't work or it's not what you want :) 您可以告诉我它是否无效或不是您想要的:)

Or you can add this query to the end of your code to make the rows unique:(not sure about this one): 或者,您可以将此查询添加到代码的末尾以使行唯一:(不确定这一行):

ALTER TABLE xmas ADD UNIQUE( `initials`, `email`, `score`)

Yeah, turns out the my submit button was a little too sensitive and clicks were registering multiple times. 是的,事实证明我的提交按钮过于敏感,点击多次注册。 Thanks everybody. 谢谢大家。

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

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