var_dump returns 布尔值(假)

var_dump returns bool(false)

我绝对是php和mysql的初学者..我想把(年,月,需求)放在数据库中的(table)但是var_dump 显示 Bool(false) 并且没有任何内容被传递到数据库......它还显示成功注册警报。 这是我的完整代码

<html>
    <head>
        <title>Simulation</title>
    </head>
    <body>
        <h2>Registration Page</h2>
        <a href="home.php"> Click here to go back </a><br/><br/>
        <form action="register.php" method="POST">
           Enter year: <input type="number" name="year" required="required" /> <br/>
           Enter month: <input type="number" name="month" required="required" /> <br/>
           Enter demand: <input type="number" name="demand" required="required" /> <br/>
           <input type="submit" value="Register"/>
        </form>
    </body>
</html>

<?php
    if ($_SERVER["REQUEST_METHOD"] == "POST")
    {
        $year = mysql_real_escape_string ($_POST['year']);
        $month = mysql_real_escape_string ($_POST['month']);
        $demand = mysql_real_escape_string ($_POST['demand']);

        echo " year is" .$year. "<br/>";
        echo " month is" .$month. "<br/>";
        echo " demand is" .$demand;

        mysql_connect("localhost", "root","") or die(mysql_error()); //Connect to server
        mysql_select_db("first_db") or die("Cannot connect to database"); //Connect to database

        $qu = mysql_query("INSERT INTO table (year, month, demand) VALUES ('$year','$month','$demand')"); //Inserts the value to table users
        var_dump ($qu);
        die();
        Print '<script>alert("Successfully Registered!");</script>'; // Prompts the user
        Print '<script>window.location.assign("register.php");</script>'; // redirects to register.php
    }

?>

如果$qu returns false 表示查询有某种错误,无法执行

这一行

$qu = mysql_query("INSERT INTO table (year, month, demand) VALUES ('$year','$month','$demand')");

您必须像这样附加字符串:

$qu = mysql_query("INSERT INTO table (year, month, demand) VALUES ('".$year."','".$month."','".$demand."')");

table 是保留字,您必须更改 table 名称并使用以下查询:

INSERT INTO Table_Name (year, month, demand) VALUES ('$year','$month','$demand')

通过将整个 table 更改为名为 users 的新用户解决了问题,因为 table 是保留字。 并编写查询

$qu = mysql_query("INSERT INTO users (year, month, demand) VALUES ('$year','$month','$demand')");