感谢您查看我的问题。我试图只更新我的数据库中的值,如果该字段为空(因此,如果有人试图注册一个已准备就绪的点并且将输出错误消息,则不会覆盖现有用户)。我已经列出了我尝试过的最新脚本中的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!';
}
答案 0 :(得分: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!';
}
请注意,您应该使用pdo
或mysqli
功能。
答案 1 :(得分:0)
试试这个。
while($row = $result->fetch_assoc) {
if($row['player1'] == NULL){
$update_player = ("UPDATE users SET player1 = '$name' where id = '$id' AND Times = '$time'")
}