Select date() HTML5 and match with SQL date - php

I am sooo stuck on this, cannot figure it out. I am trying to have the user select the date using the date selector in HTML5 and then query the sql database to find the date and output the results in a table below. I have been working on this for literally hours, cannot get past it, please help.
HTML
Date for retrieve: <input type="date" name="pickupDate" value="<?php echo $pickupDate;?>">
<span class="error">
</p>
<p>
Show date item for retrieve:
<input type="radio" name="input" value="requestDate1">Request Date
<input type="radio" name="input" value="pickupDate1">Pickup Date
</p>
<p><input type="submit" value="Show"></p>
$requestDate = mysqli_real_escape_string($db, isset($_POST["pickupDate"]));
PHP
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if ($_POST["input"] == "requestDate1") {
$query = "SELECT requestDate FROM request WHERE requestDate = '$requestDate'";
echo "working";

I think your problem is with this selection query:
$query = "SELECT requestDate FROM request WHERE requestDate = '$requestDate'";
Please change it in this way:
$query = "SELECT requestDate FROM request WHERE requestDate = '" . $requestDate . "';";
Or if it's not working then check the date format both on frontend and backend, and if it's needed change the date format on backend to match it with frontend.

This is simple:
SQL uses the YMD HIS format to store date in to it's field, and HTML5 uses DDMMYYYY so if you want to use this date format, your required to change the format, I suggest you go through this answer in stackoverflow for more details...
HTML 5 Date format

Related

How do I use a form to change the SQL query to only display registered dates between a specific range?

The code below is what I have so far, if anyone has any knowledge of how BETWEEN works and could point me in the right direction I would really appreciate it.
<form action="index.php" method="GET">
Starting from:<br>
<input type="date" name="startDate" ><br><br>
Upto:<br>
<input type="date" name="uptoDate" ><br><br>
<input type="submit">
</form>
<?php
// I removed the connection details for obvious reasons
// It does connect and display the data with no problems
$startingFrom = $_GET[startDate];
$upto = $_GET[uptoDate];
// I tried this but it does not seem to work
// $query = "SELECT * FROM table BETWEEN $startingFrom AND $upto";
$query = "SELECT * FROM table";
$result = mysql_query($query);
echo "<table><th>Name</th><th>Registered Date</th>";
while($row = mysql_fetch_array($result)){
echo "<tr><td>" . $row['name'] . "</td><td>" . $row['registered'] . "</td></tr>";
}
echo "</table>";
mysql_close();
?>
If you are returning results between two given dates use this code
SELECT *
FROM table
WHERE date BETWEEN CAST('2015-04-03' AS DATE) AND CAST('2015-05-28' AS
DATE);
With BETWEEN with dates you need to use CAST() to convert the value to appropriate datatype such as DATE or DATETIME. If you are using BETWEEN with DATETIME cast your value to DATETIME:
SELECT *
FROM table
WHERE date BETWEEN CAST('2015-04-03 08:40:09' AS DATETIME) AND CAST('2015-05-28 02:50:09' AS DATETIME);
As a side note, use MySQL improved extension (mysqli) and you are forgetting to close your input fields which is not a good code.
Your query should be:
select * from table where registered >= $startDate and registered <= $endDate;

How to populate the data from mysql using php as per bootstrap date range?

I want to populate the data form mysql database as per date range where our input are the date range of bootstrap like Predefined Ranges mention in this link http://www.daterangepicker.com/#ex4. Actually I populate the data as per defined date from the mysql database.
In my database I have the field name
pi_date as the date field.
My mysql query funtion was
public function DateAsperRange($start,$end)
{
$clause .= " DATE(`pa_date`) BETWEEN '{$start}' AND '{$end}'";
$sql_A = "SELECT *
FROM ". $this->table .""
$sql_A .= $clause;
global $db;
$rowsa = $db->query("SET NAMES UTF8");
$rowsa = $db->query_num_rows($sql_A);
return $rowsa;
}
My Html input name in date picker was
<input type="text" value="" name="daterangepicker_start" class="input-mini active">
<input type="text" value="" name="daterangepicker_end" class="input-mini">
<span>February 24, 2016 - February 24, 2016</span>
You can get the date range value in your required format with using moments.js.
I think this JavaScript gives you the correct format what you can use in your MySQL query:
moment().format("MM/DD/YYYY")

how to display the dob dd mm yyyy values from database to let the users edit it

I don't know if I asked the question properly or not..
Well I am having a form where I have a dob field and dd mm yyyy are taken from users with 3 textfields..
and I took those values and inserted into the db like dis..
$dob=$_POST('yyyy')+"-"+$_POST('mm')+"-"+$_POST('dd');
(Insert bla bla bla)
DOB <input type="text" name="dd">
<input type="text" name="mm">
<input type="text" name="yyyy">
Now I am having another form where I'm letting the users edit their info including the dob and update those..
Now I want to show their current dob in the editinfo page where 3 text fields will be same and i want to display the user's dob date in dd, dob month in mm,and dob year in yyyy field coming from database.
Just like we return the value in a text field like
<input type="text' value="<?php echo $name;?>
I want to do the same..but can't return the value of $dob because it will surely return in yyyy-mm-dd.I want the each value to be displayed in separate fields as I mentioned..
Any help will be deeply appreciated.. Thank you..!!!
You can use explode function to get each values. As you know the returning result from mysql will be in 'yyyy-mm-dd' format .
for example '2015-04-01' is the value . then
<?php
$date = '2015-04-01';
$splited_date_array = explode('-',$date);
$year = $splited_date_array[0];
$month = $splited_date_array[1];
$day = $splited_date_array[2];
?>

How to insert into database with input type=month?

billing.php
Expiry Date: <input type="month" id="ccexpirydate" name="ccexpirydate" value="<?php echo $row['expirydate'] ?>"/>
Firstly, how do I retrieve from database only showing month and year?
dobilling.php
$expiryDate = $_POST['ccexpirydate'];
$query = "INSERT INTO bill (ccexpirydate) VALUES ('" . $expiryDate . "')";
Secondly, after editing the expiry date and submitting the form, how to do I update the database with input type=month? Showing only month and year. As currently, my database would show 0000-00-00.
Date inputs are passed in the standard format. Regardless of what is shown to the user (which will be their local format), your server will receive YYYY-MM.
You can either save this as a CHAR(7) column, or add "-01" to the end of it and keep your current DATE column.
You can concatenate any random date to the variable while inserting into table, like
$expiryDate = $_POST['ccexpirydate'];
$expiryDate = $expiryDate."28"; //any date
And while retrieving and displaying in form, you can use it like:
<input type="month" value="<?php echo date('Y-m', strtotime($table_name['date_field'])); ?>"> //Y-m will give you date like, eg. 2017-11. Input type month will automatically read it as.. November 2017

Printing php page with various values

I have a php page that gets values from my database called print.php which I use to display and print the values from my database.
print.php?id=100 loads the page with values from the database that has the id 100. each record on my database has a field to store the date the record was created. As for now I visit print.php?id=100, print.php?id=101, print.php?id=102, print.php?id=103 and so on to print the same page but with various values.
Is there a way for me to create a button which does this automatically?
This button should grab all records that have the current date and print all those pages.
Sorry for being unclear.
My print.php page echos values from my database like this:
Id: <?php echo $info['id']; ?>
Name: <?php echo $info['name']; ?>
Date: <?php echo $info['date']; ?>
Gender: <?php echo $info['gender']; ?>
So for example visiting print.php?id=100 loads these values:
Id: 100
Name: John
Date: 2013-04-09
Gender: Male
Cisiting print.php?id=101 loads these values:
Id: 101
Name: Sarah
Date: 2013-04-09
Gender: Female
and so on.
When visiting each page I hit CTRL+P to print that information.
Instead of doing this manually (visiting each page and sending it to printer) I would like to have a script which grabs all the records of current date and sends it to my printer.
I'm not sure what you want to archieve, but I think you want a form like this:
<form method="get" action="">
<input type="text" name="id" value="100"/>
<input type="submit"/>
</form>
this would redirect the user to print.php?id=100 or with the value given in the textbox
EDIT:
What I read from the comments on the question is this what you want to archieve:
<form method="get" action="">
<input type="text" name="date" value="<?=date('yyyy-mm-dd');?>"/>
<input type="submit"/>
</form>
this would show a form with a textfield with the date of today printed in.
then you should make a query and fetch it like this:
$date = check_date($_GET['date']);
$query = 'SELECT * FROM values WHERE date="'.$date.'"';
if ($result = $mysqli->query($query)) {
while ($row = $result->fetch_assoc()) {
// print row
}
}
the function check_date() is up to you to create, this checks if the text is a valid date to make sure nobody can hack your database
As I understand (it's not very clear) you want records for a specified date. Then you have to send a parameter for that date but not a single record id.
So, you can send a date with print.php?date=09-04-2013 for example and in you PHP script your query would be:
$date = $_GET['date']; // validate it
$sql = "SELECT * FROM table WHERE date = '$date'";
// fetch result
// print with a foreach loop as you like
Or if you want the current date only you don't need to pass a parameter at all. Just use CURDATE() if your date field contains only the date or NOW() if it contains date and time:
$sql = "SELECT * FROM table WHERE date = CURDATE()";
// fetch result
// print with a foreach loop as you like
More: https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html
Still couldn't understand whether you have a problem with the HTML or with the PHP part.
If you are asking just for html forms and how to create a button you can check here:
http://www.w3schools.com/php/php_forms.asp
It's pretty simple to understand.

Categories