nicole101
nicole101

Reputation: 187

POST query from user input on button click

This is what I'm trying to do. I let the user to input DATEFROM and DATETO. Then when the user click the button, it will execute a query to SELECT * from my table WHERE DATEFROM BETWEEN DATETO. I don't know what's wrong. Please help.

<html>
    <head></head>
    <body>
        <?php
        <form method="POST" action="">
        DATE FROM: <input type="date" name="datefrom"> TO: <input type="date" name="dateto"> <input type="submit" value="Extract excel file" name="extract"></input>
        </form>

        $datefrom = "datefrom";
        $dateto = "dateto";

        if(isset($_POST['extract'])){
            mysql_connect("localhost", "root");
            mysql_select_db("sample");

            $myquery = "SELECT * FROM biometrics WHERE date_created BETWEEN '" . $datefrom . "' AND '" . $dateto . "ORDER BY id_biometrics";


            $result = mysql_query($myquery);

            echo "<table border='1'>
            <tr>
                <th>ID</th>
                <th>Employee Number</th>
                <th>Date Created</th>
                <th>Time Created</th>
                <th>Status</th>
            </tr>";

            while($data = mysql_fetch_row($result))
            {
                echo("<tr>
                <td>$data[0]</td>
                <td>$data[1]</td>
                <td>$data[2]</td>
                <td>$data[3]</td>
                <td>$data[4]</td>
                </tr>");
            }

            echo "</table>";

        }

        ?>
    </body>
</html>

Upvotes: 0

Views: 66

Answers (4)

Krish R
Krish R

Reputation: 22711

Can you try this, You need add space BEFORE ORDER BY

mysql_connect("localhost", "root", "");

$myquery = "SELECT * FROM biometrics WHERE date_created BETWEEN ('" . $datefrom . "' AND '" . $dateto . "') ORDER BY id_biometrics";

Upvotes: 0

Kannan Rajendran
Kannan Rajendran

Reputation: 220

Why there is no password in this line.

mysql_connect("localhost", "root");

Hope the date_created column is in Date or Datetime format. It must not be a int or a varchar or any other thing.

And also add the quotes before ORDER BY.

   $myquery = "SELECT * FROM biometrics WHERE date_created BETWEEN '" . $datefrom . "' AND '" . $dateto . "' ORDER BY id_biometrics";

Upvotes: 1

Ashok Maharjan
Ashok Maharjan

Reputation: 163

you have missed a quote(') in query ,try like this

if(isset($_POST['extract'])){
        mysql_connect("localhost", "root");
        mysql_select_db("sample");

          $datefrom = $_POST['datefrom'];
          $dateto = $_POST['dateto'];

        $myquery = "SELECT * FROM biometrics WHERE date_created BETWEEN '" . $datefrom . "' AND '" . $dateto . "' ORDER BY id_biometrics";


        $result = mysql_query($myquery);

hope this works for you

Upvotes: 0

iLaYa  ツ
iLaYa ツ

Reputation: 3997

You missed ', it should be

"SELECT * 
       FROM biometrics 
       WHERE date_created 
       BETWEEN '" . $datefrom . "' AND '" . $dateto . "' ORDER BY id_biometrics";
                                                       ^
                                                       ^ 

Upvotes: 0

Related Questions