I am absolute beginner with php and mysql .. i want to put (year, month, demand) in the (table) in database but var_dump
shows Bool(false) and nothing is being passed to the database ... it is also shows successfully registered alert.
here is my full code
<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
}
?>
table
is reserved word and you must change your table name and use below query:
INSERT INTO Table_Name (year, month, demand) VALUES ('$year','$month','$demand')