Problems viewing/editing a date in php/mysql - php

I have a php page that is creating a table from a mysql database. In that table I have an edit button to edit the data and update the mysql database. All is working great except one column that is a date. If I pull the date into a text input it works fine, but I'd prefer this to be a date field ideally to have the calender selector.
When I change the input type to date it doesn't pull the date over - instead it displays dd / mm / yyyy. This is annoying as if I only need to change the other fields and not the date I have to manually add the date again each time I try to update. Could anyone advise how to populate the date box with the date from the mySQL database?
My code (where shipping_date is the affected date):
index.php displaying the table
$sql = "SELECT * FROM final_view1 WHERE freight = 'Fedex' OR freight = 'DHL' OR freight = 'IBC' ORDER BY shipping_date DESC";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["shipping_date"] . "</td><td>" . $row["freight"]. "</td><td>" . $row["tracking_no"] . "</td><td>" . $row["vendor_name"] . "</td><td>" . $row["receiver"] . "</td><td>" . $row["pieces"] . "</td>
<td>" . $row["days_in_transit"] . "</td><td>" . $row["courier_status"] . "</td><td>" . $row["comments"] . "</td><td align=right><a href='courier_edit.php?id=".$row['id']."' class='button-edit1'>Edit</a>&nbsp
<a href='complete.php?id=".$row['id']."' class='button-complete'>Complete</a></td></tr>";
}
echo "</table>";
} else { echo "0 results"; }
$conn->close();
?>
courier_edit.php
<?php
include('db.php');
$result = $conn->prepare("SELECT * FROM final_view1 where id='$ID'");
$result->execute();
for($i=0; $row = $result->fetch(); $i++){
$id=$row['id'];
?>
<form class="form-horizontal" method="post" action="courier_edit_db.php<?php echo '?id='.$id; ?>" enctype="multipart/form-data";">
<table class="styled-table">
<tr class="active-row">
<td><label for="inputPassword">Company</label></td>
<td><input type="text" name="freight" required value="<?php echo $row['freight']; ?>"></td>
<td><label for="inputPassword">Ship date</label></td>
<td> <input type="date" name="shipping_date" required value="<?php echo $row['shipping_date']; ?>"></td>

You said - "If I pull the date into a text input it works fine"
What is the date format written there ?
to fit your date into your <input type="date"> you need to change the date format,
You need to change the date format to standard date format.
I hope you understand what I want to say , try something similar to this -
<input name="any_name" type="date"
value="<?php echo date('Y-m-d',strtotime($data["congestart"])) ?>"/>

Related

Click to insert value into a input of a PHP form

I have created a table with a mix of SQL, HTML and PHP.
See code:
<table>
<tr>
<th>Activated by:</th>
<th>Serialnumber:</th>
<th>Timestamp:</th>
<th>Total consumed energy in kWh:</th>
<th>Total bought enery in kWh:</th>
<th>Energy left in kWh:</th>
</tr>
<?php
if ($serialresult) {
while ($serialrow = mysqli_fetch_assoc($serialresult)) {
$serialnumbers = $serialrow['serial_number'];
$sql = "SELECT * FROM Smartmeterlist WHERE serial_number='$serialnumbers'";
$statsresult = mysqli_query($connection, $sql);
if ($statsresult) {
while ($statsrow1 = mysqli_fetch_assoc($statsresult)) {
$sql = "SELECT * FROM timeframe WHERE times = (SELECT MAX(times) FROM timeframe WHERE serial_number='$serialnumbers')";
$statsresult = mysqli_query($connection, $sql);
if ($statsresult) {
while ($statsrow = mysqli_fetch_assoc($statsresult)) {
echo "<tr><td>" . $statsrow1['activated_by'] . "</td> <td>" . $serialnumbers . "</td><td>" . $statsrow['times'] . "</td><td>" . $statsrow['consumed_energy'] . "</td><td>" . $statsrow['bought_energy'] . "</td><td>" . $statsrow['diff_energy'] . "</td></tr>";
}
}
}
}
}
}
}
?>
Below I create a form to execute an action:
<div class="page-content">
<form action="includes/distributeenergy.inc.php" method="POST">
<input type="Number" step="0.01" name="serial_number"
placeholder="Serial number"> <br> <input type="Number" step="0.01"
name="distribute_energy" placeholder="Assign your energy"> <br>
<div class="row">
<input class="btn" type="submit" name="DE-btn" id="DE-btn"
value="Distribute">
</div>
</form>
</div>
In the table are so called serial numbers of electronic devices.
I want to be able to click on a serial number (hyperlink or similar) and the serial number is used directly as an input for the form. So it is inserted into the "search bar".
Is there a simple way to realize this? Thanks a lot

how to use the date (including time) from database as the value of a hidden field in a php table

I have php table and a form field inside the table, that displays the status of some devices. I am displaying values like Date, Voltage, Current. user can choose any value so that the time corresponding to selected value will be used in the next page for average calculation. For average calculation I should choose immediate 5 values to the selected value. so time value from Date column is important (eg. 2019-04-17 16:14:44). The problem is when i am using hidden form value field, I am getting only date (eg. 2019-04-17) not 'time' .
form.php
$sql = "SELECT Date, Voltage, Current FROM DataTable WHERE Date>= (CURDATE() - INTERVAL 90 DAY) ORDER BY Datum DESC";
$result = $conn->query($sql);
$i=1;
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<form action = 'formProcess.php' method =get>";
echo "<tr>
<td>" . $i. "</td>
<td>" . $row["Date"]. "</td>
<td>" . $row["Voltage"] . "</td>
<td>" . $row["Current"]. "</td>
<td>"." <input type=hidden name=getTime value =" .$row["Date"]." </td>
<td>"." <input type=submit name=timesubmit value=select class = classSubmit"." </td>
</tr>";
$i++;
echo "</form>";
}
echo "</table>";
} else { echo "0 results"; }
formProcess.php
include("DBconnect.php");
if(isset ($_GET['timesubmit'])){
echo "selected a value";
$selectedTime =$_GET['getTime'];
// the following echo only gives date, not time !! that is the problem.
echo $selectedTime;
// I wanna use the immediate 5 value, that selected value and immediate 4 values for avg calculation.
$sql = "SELECT * FROM DataTable
WHERE date =>$selectedTime AND ORDER BY Date DESC LIMIT 5 ";
$conn= mysqli_connect( $dbhost, $dbuser, $dbpass, $db );
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$result_5_values = $conn->query($sql);
$i=1;
if ($result_5_values->num_rows > 0) {
while($row = $result_10_values->fetch_assoc()) {
echo "<tr>
<td>" . $i. "</td>
<td>" . $row["Date"]. "</td>
<td>" . $row["Voltage"] . "</td>
<td>" . $row["Current"]. "</td>
</tr>";
$i++;
}
echo "</table>";
} else { echo "0 results"; }
}else { echo "No selection has been made";}
Try this
$sql = "SELECT Date, Voltage, Current FROM DataTable WHERE Date>= (CURDATE() - INTERVAL 90 DAY) ORDER BY Datum DESC";
$result = $conn->query($sql);
$i=1;
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<form action='formProcess.php' method ='get'>";
echo "<tr>
<td>" . $i. "</td>
<td>" . $row["Date"]. "</td>
<td>" . $row["Voltage"] . "</td>
<td>" . $row["Current"]. "</td>
<td>"." <input type='hidden' name='getTime' value='" .$row["Date"]."' /></td>
<td>"." <input type='submit' name='timesubmit' value='select' class='classSubmit'"." </td>
</tr>";
$i++;
echo "</form>";
}
echo "</table>";
} else { echo "0 results"; }
The issue is that the you didn't quote the value of the date, all element attribute value should be quoted. I'm expecting in the web source code of this would look like:
<input type=hidden name=getTime value=2019-04-17 16:14:44>
It should look like this
<input type="hidden" name="getTime" value="2019-04-17 16:14:44">
Input tag is not closed
<input type=hidden name=getTime value =" .$row["Date"]." </td>
it should be
<input type=hidden name=getTime value =" .$row["Date"]."/> </td>
As you have no html quotes, this will give you just the date for value. Have a look at the created html code (you can see that the syntax highlighter of stackoverflow already highlights how the browser sees your html):
<input type=hidden name=getTime value=2019-04-17 16:14:44>
You should add additional quotes for HTML inside PHPs echo:
echo ' <input type="hidden" name="getTime" value="' . $row["Date"] . '" />';
... to create a valid html output:
<input type="hidden" name="getTime" value="2019-04-17 16:14:44" />
To overcome this problem, I would advice you to use single quotes ' for php and " for html. This also has some performance improvements on the php side, but it should not influence your code that much ... ;)

Retrieve values from a database and importing into table rows

Good day guys, I will be glad if you can help me out.
I have a project. and the remaining part of it is where i can pull out data from the database.
Its a tracking application. I want a situation whereby if a user type his/her reference number, it will display the results in a tabular from i.e
Reference Number, Ship date, ship weight, excpected delivery, ship country, ship state, city, zip, e.t.c. I have the following html code:
<form id="track" name="track" method="post" action="tester.php">
<h2>Track your shipment Here</h2>
<p><label> Tracking Reference:
<input type="text" id="reference" name="reference" value="" maxlength="40" required="required" /></label></p>
<div class="button_holder">
<p> <input type="submit" id="track" value="Track Now" maxlength="40" required="required" /></label>
</label></p>
</div>
and my php code is:
<?php
$ref = mysql_real_escape_string($_POST['reference']);
// conmnecting to the database
if(isset($ref)){
$connection = mysql_connect('localhost', 'root', 'wisdom');
mysql_select_db('gday');
$query = "SELECT * FROM shipment WHERE id = '".$ref."' ";
$row = mysql_query($query);
echo "<table border=\"1\"; width='550px'>"; // start a table tag in the HTML
//Creates a loop to loop through results
echo "<tr><td>" . $row['id'] . "</td><td>" . $row['ship_type'] . "</td><td>" . $row['ship_ref'] . "</td><td>" . $row['ship_date'] . "</td><td>" . $row['ship_weight'] . "</td><td>" . $row['expected_delivery'] . "</td></tr>" . $row['ship_country'] . "</td></tr>" . $row['ship_state'] . "</td></tr>" . $row['ship_city'] . "</td></tr>" . $row['ship_zip'] . "</td></tr>" . $row['remark'] . "</td></tr>";
echo "</table>"; //Close the table in HTML
mysql_close(); //Make sure to close out the database connection
}
else{echo " Enter a valid Reference Number";
exit;}
?>
Now, my major problem is this: the following code output just the table without any information inserted, Please what could be done to make sure the rows are filled with data retrieved from the database?
Thanks
Fetch fetch fetch.......
Change it like this:
$res= mysql_query($query);
$row=mysql_fetch_assoc($res);
mysqli_ functions are deprecated, don't use them anymore. Use the MySQLi or PDO libraries instead.

PHP Datepicker in SQL query

so I have this page that shows today's matches by default, it has this datepicker form
<form method="post">
<p> Select Date:<input id="datepicker" type="text" size="8" /> </p>
<input type="submit" value="Submit" name="usub" />
</form>
<?php
if(isset($_POST["usub"])){ $date = $_POST["datepicker"]; }
else{ $date = date('Y-m-d'); }
$data = mysql_query("SELECT * FROM todaysmatches where matchdate='$date'") or die(mysql_error());
echo $_POST["usub"];
echo "<h4>$today Matches</h4> </br>";
//table
if (mysql_num_rows($data)==0){
echo " No Matches";
echo "</br>";
echo "<h4> Sorry About That Check For other Days Or you can Check the Library</h4>";
}
else{
echo "<table border='1'>
<tr>
<th>Match</th>
<th>Tourmanet</th>
<th>Date</th>
</tr>";
while($info = mysql_fetch_array( $data ))
{
echo "<tr>";
echo "<td>" . $info['curmatch'] . "</td>";
echo "<td>" . $info['tournamentname'] . "</td>";
echo "<td>" . $info['matchdate'] . "</td>";
echo "</tr>";
}
echo "</table>";
}
?>
what I want is if the user choose a date in the form it would go in the query and bring the data associated with that date while keeping today as the default one when they first load the page
You need to quote the datevalue in the query as
SELECT * FROM todaysmatches where matchdate= '$today'
$data = mysql_query("SELECT * FROM todaysmatches where matchdate='$today'")
Now to get the datepicker value you need to change a bit in the form and use name attributes as
<form method="post">
<p> Select Date:<input id="datepicker" type="text" size="8" name="datepicker"/> </p>
<input type="submit" value="Submit" name="usub"/>
</form>
Then in PHP you have do as
if(isset($_POST["usub"])){
$date = $_POST["datepicker"];
}
And use it in the query and in the else part you can have the query to get the data from today's date
NOTE : Make sure that the date you are passing from date picker to query is in proper format.

php result within a date range

I am an absolute newbie and still working out the kinks. So spare me if the code looks amateurish.
I have this form.html
The user enters the date range.
form action="report.php" method="post"
FROM DATE <input type="DATE" class="textarea" name="dateStart"><br> </br>
TO DATE <input type="DATE" class="textarea" name="dateEnd">
<input type="submit" class="textarea">
</form>
This gets processed in Report.php now report.php looks like this:
php
$_POST["dater"]=$date;
$con=mysqli_connect("LOCALHOST","xxxxxxx","xxxxxx","xxxxx");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM `xxxxxxx`
dater);
echo "<table border='1'>
<tr>
<th>DATE</th>
<th>MBM</th>
<th>bulkTank</th>
<th>bagsAt40</th>
";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['date'] . "</td>";
echo "<td>" . $row['bulkTank'] . "</td>";
echo "<td>" . $row['bagsAt40'] . "</td>";
echo "</tr>";
}
echo "</table>";
I have no clue what to do next....
its not working, i tried a few things but how can I echo the result within a specific date range. Thanks!
New edit ----
This is what the new code looks like... Form
START DATE
END DATE
Report.php
<?php
$date=$_POST["dateStart"];
$con=mysqli_connect("LOCALHOST","XXXXXXX","++++++","XXXXXX");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM `WeeklyStockTake`
WHERE dateColumn=$date);
echo "<table border='1'> <!--line 32 starts here--!>
<tr>
<th>DATE</th>
<th>MBM</th>
<th>bulkTank</th>
<th>bagsAt40</th>
-----If I process it now I get this error -
Parse error: syntax error, unexpected T_STRING in /home/simples2/public_html/test/report.php on line 32 (I have commented above as to where it starts)
How to I declare the end date?
Thanks and regards!
Dont know if its a typo but php should be <?php
$_POST["dater"]=$date
This should be
$date=$_POST["dateStart"]
Also you should add a POST for dateEnd also
The query should be more like:
SELECT * FROM `xxxxxxx` WHERE dateColumn<=$date

Categories