$check_day = "select * from timetable where day ='$day'";
$run = mysql_query($check_day);
if(mysql_num_rows($run) >0) {
echo "<script>alert('day $day already exists in our database, please try another one!')</script>";
exit();
}
$query = "insert into timetable (classes, courses, lecturers, time, room, day) values ('$classes','$courses','$lecturers','$time','$room','$day')";
if(mysql_query($query)){
echo "<script>alert('Registration Successful!')</script>";
}
}
答案 0 :(得分:0)
使用mysqli更安全,更好。
首先连接:$db = mysqli_connect('host', 'user', 'password', 'database');
接下来我们要查询:
$query = "insert into timetable (classes, courses, lecturers, time, room, day) values ('$classes','$courses','$lecturers','$time','$room','$day')";
$result = mysqli_query($db, $query);
现在添加了您的查询,您可以提供额外的操作:
//Your extra code here
答案 1 :(得分:0)
答案 2 :(得分:0)
/* As I read all the previous comments and find out that you are using old SQL query. which is a bad practice. */
# Note: Please read what is mysqli, why we use mysqli
# I have correct your query
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$check_day = "SELECT * FROM timetable WHERE day ='".$day."'";
$run = mysqli_query($conn,$check_day);
if(mysqli_num_rows($run) >0) {
echo "<script>alert('day $day already exists in our database, please try another one!')</script>";
exit();
}else{
$query = "INSERT INTO timetable (`classes`, `courses`, `lecturers`, `time`, `room`, `day`) VALUES ('".$classes."','".$courses."','".$lecturers."','".$time."','".$room."','".$day."')";
if(mysqli_query($conn,$query)){
echo "<script>alert('Registration Successful!')</script>";
}
}