PHP String to run only after 5 min when page is loaded - php

I have a PHP page with the below code. I do not want to run the code directly after the page is opened. I want it to wait about 5 min and then run the SQL code.
<?php
$link = mysqli_connect("localhost","root","****#","****");
// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
// Attempt select query execution
$sql = "SELECT * FROM ver_truck WHERE sla_client = 'Woodford' ORDER BY RAND() DESC LIMIT 1";
$array = "";
if($result = mysqli_query($link, $sql)){
if(mysqli_num_rows($result) > 0){
echo "<table width='100%' style='font-size: small'>";
echo "<tr align='center'>";
echo "<th style='font-weight: bolder'>Reg</th>";
echo "<th style='font-weight: bolder'>Model</th>";
echo "<th style='font-weight: bolder'>Client</th>";
echo "<th style='font-weight: bolder'>Action</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result)){
echo "<tr align='left'>";
echo "<td align='center' style='color: red; font-weight: bold'>" . $row['reg'] . "</td>";
echo "<td align='center'>" . $row['make'] . "</td>";
echo "<td align='center'>" . $row['sla_client'] . "</td>";
echo "<td align='center'><a href='/cases/verifications/verification_alarm_new.php?imei=" . $row['imei'] . "&type=" . $row['event_desc'] . "&reg=" . $row['name'] . "&event_id=" . $row['event_id'] . " '><button>Action</button></a></td>";
}
echo "</table>";
// Free result set
mysqli_free_result($result);
} else{
echo "No Current Verifications.";
}
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
// Close connection
mysqli_close($link);
?>
Will the sleep() command in the beginning of this code prevent it from running for 5 minutes?

Related

How to make search based on pickup date and return date?

In the car page,i already input all the data into mysql database(car table).So i want to ease the user to find the car that available on pickup date and return date.So,I made search pickup date and return date in search page.But the error is "could not be execute".
This is using mysql and php.
car.php
</body>
<table align='center' width='40%' border='0' cellpadding='0' cellspacing='0'>
<tr>
<form action="search.php" method="post" enctype="multipart/form-data">
<td><input type="date" name="Pickup_date" placeholder="Pickup_date" style='height:38px' required /></td>
<td><input type="date" name="Return_date" placeholder="Return_date" style='height:38px' required /></td>
<td><input type="submit" name="submitbook" value="Search" style='background-color: blue; border: none; color: white; padding: 10px 10px; text-align: center; text-decoration: none; display: inline-block; font-size: 14px;'/></td>
</div>
</form>
</table>
<?php
$link = mysqli_connect("localhost", "root", "", "online_car_rental");
// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
// Attempt select query execution
$sql = "SELECT * FROM car";
if($result = mysqli_query($link, $sql)){
if(mysqli_num_rows($result) > 0){
echo "<table>";
echo "<tr>";
echo "<th>Id</th>";
echo "<th>Name</th>";
echo "<th>Price(RM)</th>";
echo "<th>Colour</th>";
echo "<th>Mode</th>";
echo "<th>Image</th>";
echo "<th>Status</th>";
echo "<th>Add to Booking</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['car_id'] . "</td>";
echo "<td>" . $row['car_name'] . "</td>";
echo "<td>" . $row['car_price'] . "</td>";
echo "<td>" . $row['car_colour'] . "</td>";
echo "<td>" . $row['car_mode'] . "</td>";
echo "<td><img src='" . $row['car_image'] . "' height='100'
width='100'></td>";
echo "<td>" . $row['car_status'] . "</td>";
echo '<input type="hidden" name="pickup_date"
value="'.$pickup_date.'">';
echo '<input type="hidden" name="return_date"
value="'.$return_date.'">';
echo "<td><button onclick=\"booking_car('" . $row['car_id'] .
"')\">Book</button>
</td>";
echo "</tr>";
}
echo "</table>";
// Free result set
mysqli_free_result($result);
} else{
echo "No records matching your query were found.";
}
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
mysqli_close($link);
?>
</html>
search.php
<?php
$link = mysqli_connect("localhost", "root", "", "online_car_rental");
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
$search=$_GET['s'];
$query="SELECT * FROM car WHERE date BETWEEN 'pickup_date' AND
'return_date'";
if($result = mysqli_query($link, $sql)){
if(mysqli_num_rows($result) > 0){
echo "<table>";
echo "<tr>";
echo "<th>Id</th>";
echo "<th>Name</th>";
echo "<th>Price(RM)</th>";
echo "<th>Colour</th>";
echo "<th>Mode</th>";
echo "<th>Image</th>";
echo "<th>Status</th>";
echo "<th>Add to Booking</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['car_id'] . "</td>";
echo "<td>" . $row['car_name'] . "</td>";
echo "<td>" . $row['car_price'] . "</td>";
echo "<td>" . $row['car_colour'] . "</td>";
echo "<td>" . $row['car_mode'] . "</td>";
echo "<td><img src='" . $row['car_image'] . "' height='100' width='100'></td>";
echo "<td>" . $row['car_status'] . "</td>";
echo "<td>" . $row['pickup_date'] . "</td>";
echo "<td>" . $row['return_date'] . "</td>";
echo "<td><button onclick=\"booking_car('" . $row['car_id'] .
"')\">Book</button>
</td>";
echo "</tr>";
}
echo "</table>";
// Free result set
mysqli_free_result($result);
} else{
echo "No records matching your query were found.";
}
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
mysqli_close($link);
?>
</html>
i expect the output is based on pickup_date and return_date.But the output is "ERROR:Could not able to execute".
You Can check with this , When you have two tables you can use inner join query to fetch the data
SELECT columns
FROM table WHERE pickup_date < 'your_pickup_date' AND return_date >= 'your_pickup_date' ORDER
BY car or date;
Without knowing your schema, it's difficult to provide a detailed solution, but an example of a plausible query might be as follows:
"
SELECT c.olumns
, y.ou
, a.ctually
, w.ant
FROM car c
WHERE pickup_date < :return_date
AND return_date >= :pickup_date
ORDER
BY pickup_date
, car_id;
"

Delete Row Using PHP Postgres

could someone help on below code? I have table data on that can allow user to click button to delete the row. I got error said " Undefined index: tenant_id " , Undefined variable: row. Below are delete.php and pg-t-payment-view.php script :
<?php
$db = pg_connect("host=10.0.32.204 port=5432 dbname=postgres user=postgres password=postgres");
$id = $_POST['tenant_id'];
$sql2 ="DELETE FROM payment_ref_tenancy WHERE tenant_name = '$id'";
$result = pg_query($sql2);
$cmdtuples = pg_affected_rows($result);
echo $cmdtuples . " record affected.\n";
if (!$result) {
$errormessage = pg_last_error();
echo "Error with query: " . $errormessage;
exit();
}
pg_close();
header('location:pg-t-payment-view.php');
?>
-------------pg-t-payment-view.php script
<div class="box-body table-responsive no-padding">
<?php
$db = pg_connect("host=10.0.32.204 port=5432 dbname=postgres user=postgres password=postgres");
$sql2 ="select tenant_id,to_char(last_update_time, 'MM-dd-yyyy HH24:MI') as last_update_time , tenant_name, tenant_cost_category, invoice_no,tenant_agreed_cost,
to_char(submission_date, 'MM-dd-yyyy') as submission_date,to_char(cpr_submission_to_finance, 'MM-dd-yyyy') as cpr_submission_to_finance,io,cheque_no
FROM payment_ref_tenancy order by insert_datetime desc limit 10";
$result = pg_query($db,$sql2);
if (!$result) {
$errormessage = pg_last_error();
echo "Error with query: " . $errormessage;
exit();
}
pg_close();
echo "<table class='table table-hover table-striped'>";
echo "<th align='center' >Date</th>";
echo "<th align='center' >Payee</th>";
echo "<th align='center' >Category</th>";
echo "<th align='center' >Cost (RM)</th>";
echo "<th align='center' >Invoice No</th>";
echo "<th align='center' >Payment Submission Date</th>";
echo "<th align='center'>CPR Submission to Finance</th>";
echo "<th align='center'>IO</th>";
echo "<th align='center'>Cheque No</th>";
echo "<th align='center' div style ='color:#ff0000'>Action</th>";
echo "<th align='center'></th>";
echo "<th align='center'></th>";
while($row=pg_fetch_assoc($result))
{ $id = $row['tenant_id'];
echo "<tr>";
echo "<td>" . $row['last_update_time'] . "</td>";
echo "<td>" . $row['tenant_name'] . "</td>";
echo "<td>" . $row['tenant_cost_category'] . "</td>";
echo "<td>" . $row['tenant_agreed_cost'] . "</td>";
echo "<td>" . $row['invoice_no'] . "</td>";
echo "<td>" . $row['submission_date'] . "</td>";
echo "<td>" . $row['cpr_submission_to_finance'] . "</td>";
echo "<td>" . $row['io'] . "</td>";
echo "<td>" . $row['cheque_no'] . "</td>";
echo "<td><a href='pg-t-payment-edit.php'>Edit</a></td>";
echo "<td><a href='delete.php?id=$id'><input type='hidden' name='id' value=$id>Delete</a></td>";
echo "</tr>";}
echo "</table>";
?>
Try to move pg_close(); in pg-t-payment-view.php somewhere after you have already worked with data fetched from database.
So basically after this loop executes: while($row=pg_fetch_assoc($result))
Change name of argument you pass in your delete button to reflect column name or change in delete.php how you get said ID.
<div class="box-body table-responsive no-padding">
<?php
$db = pg_connect("host=10.0.32.204 port=5432 dbname=postgres user=postgres password=postgres");
$sql2 ="select tenant_id,to_char(last_update_time, 'MM-dd-yyyy HH24:MI') as last_update_time , tenant_name, tenant_cost_category, invoice_no,tenant_agreed_cost,
to_char(submission_date, 'MM-dd-yyyy') as submission_date,to_char(cpr_submission_to_finance, 'MM-dd-yyyy') as cpr_submission_to_finance,io,cheque_no
FROM payment_ref_tenancy order by insert_datetime desc limit 10";
$result = pg_query($db,$sql2);
if (!$result) {
$errormessage = pg_last_error();
echo "Error with query: " . $errormessage;
pg_close();
exit();
}
echo "<table class='table table-hover table-striped'>";
echo "<th align='center' >Date</th>";
echo "<th align='center' >Payee</th>";
echo "<th align='center' >Category</th>";
echo "<th align='center' >Cost (RM)</th>";
echo "<th align='center' >Invoice No</th>";
echo "<th align='center' >Payment Submission Date</th>";
echo "<th align='center'>CPR Submission to Finance</th>";
echo "<th align='center'>IO</th>";
echo "<th align='center'>Cheque No</th>";
echo "<th align='center' div style ='color:#ff0000'>Action</th>";
echo "<th align='center'></th>";
echo "<th align='center'></th>";
while($row=pg_fetch_assoc($result))
{ $id = $row['tenant_id'];
echo "<tr>";
echo "<td>" . $row['last_update_time'] . "</td>";
echo "<td>" . $row['tenant_name'] . "</td>";
echo "<td>" . $row['tenant_cost_category'] . "</td>";
echo "<td>" . $row['tenant_agreed_cost'] . "</td>";
echo "<td>" . $row['invoice_no'] . "</td>";
echo "<td>" . $row['submission_date'] . "</td>";
echo "<td>" . $row['cpr_submission_to_finance'] . "</td>";
echo "<td>" . $row['io'] . "</td>";
echo "<td>" . $row['cheque_no'] . "</td>";
echo "<td><a href='pg-t-payment-edit.php'>Edit</a></td>";
echo "<td><a href='delete.php?tenant_id=$id'><input type='hidden' name='id' value=$id>Delete</a></td>";
echo "</tr>";}
pg_close();
echo "</table>";
?>
Change in your delete.php how you get and use ID. If your tenant_id is integer, you can omit quotes from DELETE query.
<?php
$db = pg_connect("host=10.0.32.204 port=5432 dbname=postgres user=postgres password=postgres");
$id = $_GET['tenant_id'];
$sql2 ="DELETE FROM payment_ref_tenancy WHERE tenant_id = '$id'";
$result = pg_query($sql2);
$cmdtuples = pg_affected_rows($result);
echo $cmdtuples . " record affected.\n";
if (!$result) {
$errormessage = pg_last_error();
echo "Error with query: " . $errormessage;
exit();
}
pg_close();
header('location:pg-t-payment-view.php');
?>
Just so you know, this code can be SQL Injected, this is security issue if untrusted people will use it.

can anyone help me how to pass the results of separately to other page when schedule is clicked

1.
<?php
$sql = "(SELECT id,Firstname, Lastname, MI, Course, Date_Enrolled FROM listofstudents )";
$result = mysqli_query($con, $sql);
$num_rows = mysqli_num_rows($result);
if ($num_rows > 0){ // build a table to show results
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td align='center'>" . $row['Firstname'] . "</td>";
echo "<td align='center'>" . $row['Lastname'] . "</td>";
echo "<td align='center'>" . $row['MI'] . "</td>";
echo "<td align='center'>" . $row['Course'] . "</td>";
echo "<td align='center'>" . $row['Date_Enrolled'] . "</td>";
echo "<td align='center'> <a href='schedule.php?name=".$row['Firstname']."lastname=". $row['Lastname'] . " mi=". $row['MI'] ."'>Schedule</a></td>";
echo "</tr>";
}
} else{
echo "No rows returned.";
}
?>
List item
You are sending data in url witch is not safe
You can send tha ID and on the requested page you can the get the row by sql query.

Data from Database into the table (can't figure out how to do layout)

This is my code (horrible one):
<?php
include 'connect/con.php';
$result = mysqli_query($con,"SELECT id, vidTitle FROM newsvid");
$result1 = mysqli_query($con,"SELECT imgCover, vidSD FROM newsvid");
$result2 = mysqli_query($con,"SELECT published FROM newsvid");
echo "<table width=\"600\" border=\"1\"><tbody>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo '<td width=\"10%\">'.$row['id'].'</td>';
echo "<td width=\"90%\">" . $row['vidTitle'] . "</td>";
echo "</tr>";
}
echo "</tbody></table>";
echo "<table width=\"600\" border=\"1\"><tbody>";
while($row = mysqli_fetch_array($result1)) {
echo "<tr>";
echo "<td width=\"40%\">" . $row['imgCover'] . "</td>";
echo "<td width=\"60%\">" . $row['vidSD'] . "</td>";
echo "</tr>";
}
echo "</tbody></table>";
echo "<table width=\"600\" border=\"1\"><tbody>";
while($row = mysqli_fetch_array($result2)) {
echo "<tr>";
echo "<td >" . $row['published'] . "</td>";
echo "</tr>";
}
echo "</tbody></table>";
mysqli_close($con);
?>
</body>
</html>
The question is how to show data from database in this layout:
--------------------------------
-id----------vidTitle-----------
--------------------------------
-imgCover------vidSD------------
--------------------------------
----------published-------------
So every time I will add more data , another block like I showed before will add up under existing one.
........................................................................................
There's no need to write 3 queries. You could do that with only one select, and then put all the echos inside a while. That way you're writing, it would run all the ids and titles first, then it would put a table after the table, with cover and vidSD.
Try to make a single query:
SELECT id, vidTitle, imgCover, vidSD, published FROM newsvid
That way you will have, on each row returned from database, all the information about the same row.
Now, running a while is the same as you're doing, just adapting some HTML:
echo "<table width='600' border='1'><tbody>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo '<td width=\"10%\">'.$row['id'].'</td>';
echo "<td width=\"90%\">" . $row['vidTitle'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td width=\"40%\">" . $row['imgCover'] . "</td>";
echo "<td width=\"60%\">" . $row['vidSD'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td colspan='2'>" . $row['published'] . "</td>";
echo "</tr>";
}
echo "</tbody></table>";
You may want to order it too. Adding ORDER BY id DESC, would do that.

Updating multiple MySql entrys

I've been working on creating an internal site for our company. I haven't had many issues until now. I have been able to retrieve and insert data into my database, but now for some reason when I try to UPDATE an entry, the database can't be selected for some strange reason. I've attached a copy of my code thus far. I don't know what I am missing. Thank you!
This is my code for looking up the information in the database:
<?php
session_start();
$transport = mysqli_connect("localhost", "user", "pw", "db_name");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
<?php
$raw_date = $_POST['appt_date'];
$date = date("Y-m-d", strtotime($raw_date));
if ($raw_date == '') {
echo "Please go back and pick a date";
exit;
}
$sql = "SELECT * FROM appointments WHERE date = '".$date."' ORDER BY appttime";
$result = mysqli_query($transport, $sql);
$i=0;
echo "<h2 align='center'>Schedule for $raw_date</h2>";
echo "<table border='2' style='width: 100%; margin: auto; border-width: 1px'><tr><th>Resident Name</th><th>APT #</th><th>Appt. Time</th><th>Location Phone</th><th>Location Name</th><th>Address</th><th>City</th><th>Zip</th><th>Bus or Car</th><th>Escort Name</th><th>Transfer</th><th>Comments</th><th>Dparting Times</th></tr>";
echo "<form name='update_times' method='post' action='depart.php'>\n";
while($row = mysqli_fetch_array($result))
{
echo "<input type='hidden' name='id[$i]' value=" . $row['id'] . "";
echo "<tr>";
echo "<td align='center'>" . $row['r_name'] . "</td>";
echo "<td align='center'>" . $row['room'] . "</td>";
echo "<td align='center'>" . date("g:i A", strtotime($row['appttime'])) . "</td>";
echo "<td align='center'>" . $row['apptphone'] . "</td>";
echo "<td align='center'>" . $row['l_name'] . "</td>";
echo "<td align='center'>" . $row['address'] . "</td>";
echo "<td align='center'>" . $row['city'] . "</td>";
echo "<td align='center'>" . $row['zip'] . "</td>";
echo "<td align='center'>" . $row['buscar'] . "</td>";
echo "<td align='center'>" . $row['escort_name'] . "</td>";
echo "<td align='center'>" . $row['transfer'] . "</td>";
echo "<td align='center'>" . $row['comments'] . "</td>";
echo "<td align='center'><input name='out[$i]' style='width: 70px' type='text' value='" . date("g:i A", strtotime($row['depart'])) . "' /></td>";
echo "</tr>";
++$i;
}
echo "<input type='submit' value='Set Depart Times'>";
echo "</form>";
echo "</table>";
$_SESSION['sessionVar'] = $raw_date;
?>
This is the update code:
<?php
session_start();
$transport = mysqli_connect('localhost', 'user', 'pw', 'db_name');
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
<?php
$size = count($_POST['out']);
$i=0;
while ($i < $size)
{
$departing = $_POST['out'][$i];
$departing = date("H:i:s:u",strtotime($departing));
$id = $_POST['id'][$i];
$sql = "UPDATE transport.appointments SET depart = $departing WHERE id = $id";
mysqli_query($transport, $sql) or die ("Error in query: $sql");
echo "Depart times updated!";
++$i;
}
mysql_close($transport);
?>
For some reason the update code doesn't want to select my database. Thank you again!
The MySQL UPDATE syntax is:
UPDATE table_name SET column1=value1,column2=value2,... WHERE some_column=some_value;
It looks like you've tried to reference your database and your table in the UPDATE query: transport.appointments. I might be wrong, but I can't find anything on the internet saying that is valid syntax.
Try just referencing the table:
$sql = "UPDATE appointments SET depart = $departing WHERE id = $id";

Categories