douqi4673 2014-02-24 17:17
浏览 58
已采纳

检查用户是否在数据库中

I have developed a game with Javascript and when the user finishes it, I must save his record in a database. Here you see the code:

$temp = $_POST['playername'];             //username
$text  = file_get_contents('names.txt');  //list with all usernames

//this text file contains the names of the players that sent a record.

$con=mysqli_connect("localhost","username","pass","my_mk7vrlist");

if (stripos(strtolower($text), strtolower($temp)) !== false) {
//if the username is in the list, don't create a new record but edit the correct one   

 mysqli_query($con, "UPDATE `my_mk7vrlist`.`mk7game` SET `record` = '".$_POST['dadate']."' WHERE `mk7game`.`playername` = ".$temp." LIMIT 1 ");

} else {

 //The username is not in the list, so this is a new user --> add him in the database
 mysqli_query($con, "INSERT INTO `mk7game` (`playername`,`record`,`country`,`timen`) VALUES ('".$_POST['playername']."', '".$_POST['dadate']."', '".$_POST['country']."', '".$_POST['time_e']."')");

 file_put_contents("names.txt",$text."
".$temp);
 //update the list with this new name
}

//Close connection
mysqli_close($con);

When I have a new user (the part inside my "else") the code works correctly because I have a new row in my database.

When the username already exists in the list, it means that this player has already sent his record and so I must update the table. By the way I cannot edit the record on the player that has alredy sent the record.

mysqli_query($con, "UPDATE `my_mk7vrlist`.`mk7game` SET `record` = '".$_POST['dadate']."' WHERE `mk7game`.`playername` = ".$temp." LIMIT 1 ");

It looks like this is wrong, and I can't get why. I am pretty new with PHP and MySQL.

Do you have any suggestion?

  • 写回答

2条回答 默认 最新

  • dsjpqpdm620596 2014-02-24 17:23
    关注

    You're missing quotes around $temp in the UPDATE statement:

    mysqli_query($con, "UPDATE `my_mk7vrlist`.`mk7game` 
                        SET `record` = '".$_POST['dadate']."' 
                        WHERE `mk7game`.`playername` = '".$temp."' 
                                                       ^         ^
                        LIMIT 1 ") or die(mysqli_error($con));
    

    However, it would be better to make use of prepared statements with parameters, rather than inserting strings into the query.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊