简体   繁体   English

Mysqli_query返回false,而Mysqli_error返回NULL?

[英]Mysqli_query returns false and Mysqli_error returns NULL?

I've been creating a booking system, and creating appointments, but my SQL statement is not working. 我一直在创建预订系统并创建约会,但是我的SQL语句不起作用。 I've been trying to find a solution but to no avail. 我一直在寻找解决方案,但无济于事。

Listed below is my php code. 下面列出的是我的PHP代码。 My first SQL statement works perfectly and returns the correct ClientID, however, the second SQL statement does not insert it all into the database. 我的第一个SQL语句可以正常工作并返回正确的ClientID,但是,第二个SQL语句并未将其全部插入数据库中。 I have done var_dumps on result, returning bool(false), as well as mysqli_error on the result, returning null. 我对结果做了var_dumps,返回bool(false),对结果做了mysqli_error,返回null。 My error message at the end only displays the echo'd message, and not the mysqli_error or error number also. 我的错误消息最后仅显示echo'd消息,也没有mysqli_error或错误号。

(Note: some values are changed/removed to protect data) (注意:为了保护数据,某些值已更改/删除)

<?php
    session_start();
    if(! $_SESSION['Username']) {
        header("location:Index.php");
    }    
    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "";
    $tablename = "appointmentinformation";
    $tablenamed = "clientinformation";

    $connection = mysqli_connect("$servername", "$username", "$password", "$dbname") or die("Could not connect to the database");

    $clientusername = $_SESSION['Username'];
    $sql = "SELECT ClientID FROM $tablenamed WHERE Username = '$clientusername' LIMIT 1";
    $results = mysqli_query($connection, $sql);
    if (! $results) {
        echo ("Could not select the data : " . mysql_error());
    } else {
        $datarows = mysqli_fetch_row($results);
        $clientid = $datarows[0];
    }

    $date = $_POST["Date"];
    $month = $_POST["Month"];
    $year = $_POST["Year"];
    $time = $_POST["Time"];
    $length = $_POST["Length"];

    $date = stripslashes($date);
    $month = stripslashes($month);
    $year = stripslashes($year);
    $time = stripslashes($time);
    $length = stripslashes($length);

    $date = mysqli_real_escape_string($date);
    $month = mysqli_real_escape_string($month);
    $year = mysqli_real_escape_string($year);
    $time = mysqli_real_escape_string($time);
    $length = mysqli_real_escape_string($length);

    $query = "INSERT INTO appointmentinformation (ClientID, Length, Date, Month, Year, Time, Price) VALUES ('$clientid', '$length', '$date', '$month', '$year', '$time', '$price')";
    $result = mysqli_query($connection, $query);
    if ($result) {
        header("Location:UserCP.php");
    } else {
        echo ("Could not insert data : " . mysqli_error($result) . " " . mysqli_errno());
    }
?>
$query = "INSERT INTO appointmentinformation (ClientID, Length, Date, Month, Year, Time, Price) VALUES ('$clientid', '$length', '$date', '$month', '$year', '$time', '$price')";
    $result = mysqli_query($connection, $query);
    if ($result) {
        header("Location:UserCP.php");
    } else {
        echo ("Could not insert data : " . mysqli_error($result) . " " . mysqli_errno());
    }

The result returned from mysqli_query is null. mysqli_query返回的结果为空。 This sends you down the else branch of the code. 这会将您发送到代码的else分支。 Then you coded mysqli_error($result) which equals mysqli_error(null). 然后,您编码了mysqli_error($ result)等于mysqli_error(null)。

The documentation I have read says to initialize a variable with a "link" to the query. 我读过的文档说要使用与查询的“链接”初始化变量。 You did this with: 您这样做的原因是:

 $connection = mysqli_connect("$servername", "$username", "$password", "$dbname") or die("Could not connect to the database");

You now want to code it as mysqli_error($connection) and mysqli_errno($connection). 现在,您要将其编码为mysqli_error($ connection)和mysqli_errno($ connection)。

An additional suggestion. 另一个建议。 Add this code right after your mysqli_connect statement. 在mysqli_connect语句之后立即添加此代码。

if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM