0

I have a form where I collect a bunch of basic information (name, email, etc), I then post that to a database. On that form, I use the php date function to insert a "day", "month", and "year" database column so it will show me what date the form data was submitted.

I want to output this to a table (so I can check it without logging into the db), and I want that table to default to the current date. I thought something like this would work, but it doesn't:

$query="SELECT * FROM form_data WHERE day='echo date("d");' AND month='echo date("m");' AND year='echo date("Y");'";

Ultimately, I want to have some date select boxes where the table is displayed, so I can select any date I want, submit, and return the relevant data to the table. But the first step is just getting the table to default to display today's current data.

Much appreciated if anybody can help.

2
  • i would suggest to use prepared statements, and PDO. Commented Aug 20, 2012 at 3:11
  • 1
    Would probably be better to use a mysql date field too. Commented Aug 20, 2012 at 3:18

4 Answers 4

2

To make the thread complete I am gonna go ahead and suggest mysqli::prepare. As it is stated here it prepares the query and returns a handle to it.
It is to prevent SQL injection, lower the query parsing overhead, and improve code readability. All and all it is a better practice to use prepare.
And this is how you do it:

$database = new mysqli('localhost', 'username', 'password', 'database');
if ($stmt = $database->prepare("SELECT * FROM form_data WHERE day=? AND month=? AND year=?")) {
  $stmt->bind_param('sss', date("d"), date("m"), date("Y"));
  $stmt->execute();
  $result = $stmt->get_result();
  $stmt->close();
}
$database->close();

In the code above $result contains the rows read from database.

Here is the PDO version:

$stmt = $database->prepare("SELECT * FROM form_data WHERE day=? AND month=? AND year=?")
$stmt->execute(array("".date("d"), "".date("m"), "".date("Y")));
Sign up to request clarification or add additional context in comments.

1 Comment

Or use PDO, with named parameters.
1

You can't do echo date("d"); in an sql statement or you would be effectively putting a statement inside a statement.

You must save your echo date("d"); into a variable and then do something like

$myDay = date("d");
$myMonth = date("m");
$myYear = date("Y");
$query="SELECT * FROM form_data 
        WHERE day='$myDay' 
        AND month='$myMonth' 
        AND year='$myYear'";

or use separating . to shoot them in like

 $query="SELECT * FROM form_data 
         WHERE day='".date("d")."' 
         AND month='".date("m")."' 
         AND year='".date("Y")."'";

3 Comments

Yep, this works. Thank you! It shouldn't be too hard now to create select inputs on the table page that modifies the date variables that are set.
Just don't forget to select the answer @Paul I live and breathe on points :)
I wouldn't ever forget! Makes me wait 5min ;). Thanks again, I actually tried what you suggested earlier but my syntax must have been off.
0

Something like

$query="SELECT * FROM form_data WHERE day='" . date("d") . "' AND month='" . date("m") . "' AND year='" . date("Y") . "'";

should work.

Good way to debug your queries is to output them as a string to see how PHP interpreted it.

echo "SELECT * FROM form_data WHERE day='" . date("d") . "' AND month='" . date("m") . "' AND year='" . date("Y") . "'"

Also note that this method is subject to SQL Injection.

3 Comments

That returns a blank table (there is data for today).
@Paul This is no different than Jared's answer.
Perhaps I didn't enter it properly, or it didn't fully save in the ftp. I'd still give this rep if I had the ability to. Thanks for the answer!
0

I use this and it works for me select * from record where date1=CURDATE() I hope this is what you want to do

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.