0

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>

4 Answers 4

1

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";
Sign up to request clarification or add additional context in comments.

2 Comments

because I don't have password in myphpadmin. about the date or datetime, I'll go check it out. thanks. :D
If you dont have a passowrd, just have it as null value. It must be mysql_connect("localhost", "root", "");
0

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

1 Comment

IT WORKED!! THANK YOU SO MUCH. Now I have to extract it in csv file. thanks so much again. :D
0

You missed ', it should be

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

Comments

0

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";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.