请有人解释为什么我在下面收到错误消息?

时间:2016-11-03 20:11:22

标签: php mysql

$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>";
}


}

3 个答案:

答案 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)

您使用的是过时的PHP / MySQL功能。

使用新的mysqli_*功能,因为它们更安全,更新。

https://www.w3schools.com/php/php_ref_mysqli.asp

答案 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>";
    }

    }

希望对您有帮助