dpea85385 2018-07-17 15:50
浏览 41

PHP两次向数据库提交条目

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.

Is it my PHP code that's duplicating the entries or my game application?

<?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);

?>
  • 写回答

2条回答 默认 最新

  • drelgkxl93433 2018-07-17 16:20
    关注

    You can try this in your sql query:

    REPLACE does exactly what INSERT does but it won't let sql query double a record.

    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`)
    
    评论

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探