我是php和mysql的绝对初学者..我想把(年,月,需求)放在数据库中的(表)中,但是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
}
?>
答案 0 :(得分:0)
问题已解决,将整个表更改为名为users的新表是一个保留字。 并编写像
这样的查询$qu = mysql_query("INSERT INTO users (year, month, demand) VALUES ('$year','$month','$demand')");
答案 1 :(得分:-1)
table
是保留字,您必须更改您的表名并使用以下查询:
INSERT INTO Table_Name (year, month, demand) VALUES ('$year','$month','$demand')
答案 2 :(得分:-2)
如果$ qu返回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."')");