douding6266 2015-03-07 18:10
浏览 384
已采纳

Mysqli_query返回false而Mysqli_error返回NULL?

I've been creating a booking system, and creating appointments, but my SQL statement is not working. I've been trying to find a solution but to no avail.

Listed below is my php code. My first SQL statement works perfectly and returns the correct ClientID, however, the second SQL statement does not insert it all into the database. I have done var_dumps on result, returning bool(false), as well as mysqli_error on the result, returning null. My error message at the end only displays the echo'd message, and not the mysqli_error or error number also.

(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());
    }
?>
  • 写回答

1条回答 默认 最新

  • douweng1904 2015-03-07 18:30
    关注
    $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. This sends you down the else branch of the code. Then you coded mysqli_error($result) which equals 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).

    An additional suggestion. Add this code right after your mysqli_connect statement.

    if (mysqli_connect_errno())
      {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
      }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化