我有一个叫做分组的表。因此,当我注册新组时,如果组具有与现有组相同的时间和房间号,则系统应该警告它。 这是我的代码,但它不起作用。拜托,帮助我!
$query1= mysql_query("SELECT * FROM grouping WHERE room_number ='$room_number' AND week_days='$week_days' AND time='$time'");
if(mysql_num_rows($query1) != 0 ) { //CHECK FOR EXISTING TIME AND NAME IN TABLE
mysql_query("INSERT INTO grouping(name, subject, status, week_days, time, room_number, tutorID)
VALUES('$name','$subject','$status','$week_days','$time','$room_number','$tutorID')");
echo "<script>alert('Your group is registered')</script>";
echo "<script>window.open('tutor_main.php','_self')</script>";
} else {
echo "<script>alert('This time is not available')</script>";
echo "<script>window.open('tutor_main.php','_self')</script>";
exit();
}
答案 0 :(得分:0)
我假设您正在尝试测试$ room_number,$ week_days,$ time的值是否已经在'grouping'表中。根据这个假设,这是我建议的方法:
//# Assuming the values we want to test are $room_number, $week_days, $time:
$query1= mysql_query("SELECT * FROM grouping WHERE room_number='$room_number' AND week_days='$week_days' AND time='$time'");
//# Logically if zero rows comes back you are free to insert the data in the database.
if(mysql_num_rows($query1)==0)
{
//# THIS CREATES A NON DUPLICATE RECORD WITH UNIQUE VALUES FOR $room_number, $week_days, $time:
mysql_query
(
"INSERT INTO grouping(name, subject, status, week_days, time, room_number, tutorID)
VALUES('$name','$subject','$status','$week_days','$time','$room_number','$tutorID')"
);
echo "<h2>Your data was added to the database successfully!</h2>";
}
else
{
//# THE QUERY RETURNED MORE THAN ZERO ROWS - YOU HAVE A CONFLICT IN THE DATA
echo "<h2>ERROR! You have a conflict in the data</h2>";
}
我希望这有助于您开始朝着正确的方向前进。