我有一张桌子,上面有这个列:[user_id] [game_id] 我需要在两个玩家加入游戏时关闭游戏。 我用这个代码:Mysql插入冲突
if(mysql_num_rows(mysql_query("SELECT user_id FROM live_games WHERE game_id = '$gid'"))<2){
mysql_query("INSERT INTO live_games (user_id, game_id) VALUES ('$uid', '$gid')");
echo "You have joined the game";
}else{
echo "Table is full";
}
代码将使只有两个玩家注册,但有些时候,也有在网站上的用户太多,这种情况将无法正常工作和三个用户将在表中添加。 我该如何解决?
请不要使用'mysql_ *'函数,它已被弃用(请参阅[*红框*](http://php.net/manual/en/function.mysql-query.php))和容易受到sql注入的影响。使用[* PDO *](http://php.net/manual/en/book.pdo.php)或[* MySQLi *](http://php.net/manual/en/book.mysqli.php) 。 – alfasin