1

感谢您查看我的问题。我试图仅在该字段为空时更新我的​​数据库中的值(因此,如果有人尝试注册一个已准备就绪的位置并且将输出错误消息,则不会覆盖现有用户)。我在下面列出了我尝试过的 2 个最新脚本。如果 select 语句不存在,第一个脚本用于更新数据库,但如果在同一天和同一时间输入,则会覆盖用户。谢谢大家!

$sql = ("SELECT `player1` FROM `users` where id = '$id' and Times = '$time'"); 


$result = $conn->query($sql);

if ($result->fetch_assoc === NULL) {
$update_player = ("UPDATE users SET player1 = '$name' where id = '$id' AND Times =   '$time'")

if($update_player){
echo "Date for $name inserted successfully!";
}
}

else {
  echo 'That spot is all ready taken!';
}

//2nd script 

$query=mysql_query("UPDATE users SET 
player1 = isNULL (player1, $name) 
where id = '$id' AND Times = '$time'" );

if($query){

 echo "Data for $name inserted successfully!";
}

else {
 echo 'That spot is all ready taken!';
}
4

2 回答 2

2

以下代码应该可以解决问题:

$query=mysql_query("UPDATE users SET 
player1='$name' 
where id = '$id' AND Times = '$time' AND player1 IS NULL" );

if(mysql_affected_rows() == 1){
  echo "Data for $name inserted successfully!";
}
else {
  echo 'That spot is all ready taken!';
}

请注意,您应该改用pdoormysqli函数。

于 2012-12-27T15:00:45.130 回答
0

试试这个。

 while($row = $result->fetch_assoc) {
    if($row['player1'] == NULL){
      $update_player = ("UPDATE users SET player1 = '$name' where id = '$id' AND Times =   '$time'")
    }
于 2012-12-27T15:02:47.243 回答