0

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
    }

?>
Ahmed Ragab
  • 37
  • 1
  • 9

3 Answers3

0

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')
ops
  • 2,023
  • 18
  • 21
0

problem solved with changing the whole table to a new one named users as table is a reserved word. and writing a query like

$qu = mysql_query("INSERT INTO users (year, month, demand) VALUES ('$year','$month','$demand')");
Ahmed Ragab
  • 37
  • 1
  • 9
  • it is unlikely that back-ticks (you did net try regular quotes?) on table did not work if changing the table name did - but all fine. –  Apr 30 '15 at 02:58
  • above you use quotes not back-ticks ```````````=back ticks, '''''''''''''=single quote. key to the left of the number 1 on an 'American' keyboard –  Apr 30 '15 at 02:58
  • Go to the `phpmyadmin` and click the table `users` then click the tab `sql` and then click `insert` now copy this query here .... TNQ – ops Apr 30 '15 at 03:00
  • @Dagon OH so sorry for that .. i am a beginner as mentioned thank you for helping me and i will edit the answer to be useful for others – Ahmed Ragab Apr 30 '15 at 03:01
-2

If $qu returns false means that the query have some kind of error and can't execute

In this line

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

you must append strings more like this:

$qu = mysql_query("INSERT INTO table (year, month, demand) VALUES ('".$year."','".$month."','".$demand."')");
Darren
  • 13,050
  • 4
  • 41
  • 79
Sergio Vilchis
  • 332
  • 2
  • 4
  • 16