Provide a link in an sql query based on ID - php

So I'm having trouble figuring out how to show a link to a new page based on ID. I've created a review page of all timecards submitted based on employee. It works fine, but in case the employee wants to review an individual record and change specific things in the record is where i'm having trouble with. Below is a snippit of code i'm using to display the information. Because the timecard has MANY fields in I don't need to waste the whole page showing all the information until the employee actually submits the specific card he wants.
$result = mysql_query("SELECT ID,employee,date FROM tcardsubmit WHERE employee = '$empname");
echo "<table border='1'>
<tr>
<th>ID</th>
<th>EMPLOYEE NAME</th>
<th>DATE</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['ID'] . "</td>";
echo "<td>" . $row['employee'] . "</td>";
echo "<td>" . $row['date'] . "</td>";
echo "</tr>";
}
echo "</table>";
That query shows all I really need the employee to see. What i'd like is for the ID number to carry over to a page called 'review.php' but the ID number carries over with it, then I can just use GET ID and pull all the data based on the ID on the review page. Any suggestions?

make a little change on your table
echo "<table border='1'>
<tr>
<th>ID</th>
<th>EMPLOYEE NAME</th>
<th>DATE</th>
<th>ACTION</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['ID'] . "</td>";
echo "<td>" . $row['employee'] . "</td>";
echo "<td>" . $row['date'] . "</td>";
echo "<td>Review</td>";
echo "</tr>";
}
echo "</table>";

In the loop add another column and do
echo '<td>View</td>';
In review.php
$id = (int) $_GET['id'];
And query..
SELECT <fields> FROM tcardsubmit WHERE ID = $id
Make sure to escape id .. I did it by casting to an int, but you can also do mysql_real_escape_string

Related

how to dynamically insert datediff into webpage to keep updated in real time (now)

I can do a request in phpmyadmin to show the number of days between todays date and a date selected by a user:
SELECT DATEDIFF(CURDATE(), date_instructed) AS DAYS FROM date_tester
.. but i think this wont update all the table and will only update when a new record is created in the table.
So can anyone suggest the best way to dynamically update/count the days in a column by using a date entered by a user .
Should it even be put in a column? or should i use php to echo the result for each row on the webpage.
If I should be manipulating the ‘date_instructed’ with a script and then entering it in the html table seperatly is their a link to show me how to do this?
Many thanks
<?php
include 'dbh.php';
?>
<table class="table table-sm table table-bordered
table table-hover table table-striped "> <!--, table table-inverse-->
<thead class="thead-inverse">
<tr class="warning">
<th>First Name</th>
<th>Last</th>
<th>Date Instructed</th>
<th>Days On Market</th>
</tr>
</thead>
<?php
$query = "SELECT first_name, last_name, date_instructed , days_onmarket
FROM date_tester";
$result = mysqli_query($conn, $query);
while ($date_tester = mysqli_fetch_assoc($result)) {
echo "<tr>";
echo "<td>" . $date_tester['first_name'] . "</td>";
echo "<td>" . $date_tester['last_name'] . "</td>";
echo "<td>" . $date_tester['date_instructed'] . "</td>";
}
?>
</tbody>
</table>
</div>
From what i understand this is what you need:
$query = "SELECT first_name, last_name, date_instructed , days_onmarket, DATEDIFF(CURDATE(), 'date_instructed') AS DAYS FROM date_tester";
$result = mysqli_query($conn, $query);
while ($date_tester = mysqli_fetch_assoc($result)) {
echo "<tr>";
echo "<td>" . $date_tester['first_name'] . "</td>";
echo "<td>" . $date_tester['last_name'] . "</td>";
echo "<td>" . $date_tester['date_instructed'] . "</td>";
echo "<td>" . $date_tester['DAYS'] . "</td>";
}

How to cancel a booking for a specific row using php and mysql

I have a table with each row having a cancel link. I would like to cancel a booking for the specific rows. May I know how to start?
The codes of the table is as below.
$result = mysqli_query($con, $sql);
if (mysqli_num_rows($result) > 0) {
echo "<table border = 1>
<tr>
<th>Custid</th>
<th>Venue</th>
<th>Firt Name</th>
<th>Date</th>
<th>Time</th>
<th>Menu Type </th>
<th>No.of table </th>
<th>Total price </th>
<th>Amount Paid </th>
<th>Make Payment </th>
<th>Cancel Booking</th>
</tr>";
while($row = mysqli_fetch_assoc($result)) {
echo "<tr>";
echo "<td>" . $row['Customer_id']. " </td>";
echo "<td>" . $row['Venue_name']. " </td>";
echo "<td>" . $row['First_name']. "</td>";
echo "<td>" . $row['Date']. "</td>";
echo "<td>" . $row['Time']. "</td>";
echo "<td>" . $row['Menu_type']. "</td>";
echo "<td>" . $row['no_of_table']. "</td>";
echo "<td>" . $row['total_price']. "</td>";
echo "<td>" . $row['total_amt_paid']. "</td>";
echo"<td><a href='Payment.php'>Payment</a></td>";
echo"<td><a href='Cancel.php'>Cancel</a></td>";
echo "</tr>";
}
echo "</table>";
}
You need to pass the Cancel.php script some knowledge of what it should delete, so change
echo "<td><a href='Cancel.php'>Cancel</a></td>";
to
echo '<td>Payment</td>';
And then in the Cancel.php script you should receive that value in the $_GET array
<?php
$Customer_id = $_GET['Customer_id'];
If you want to cancel a specific booking you'll need something specific about each booking, like a booking id for example. If we assume you already have that in your table then you can pass this unique value (specific to the row) to your cancel.php and payment.php pages.
To do this we will use a get request and a query string. This will mean your original line changes from this:
echo "<td><a href='Cancel.php'>Cancel</a></td>";
to this one below. We can add the query string unique to each row like this: (assuming it might be called booking_id)
echo "<td><a href='Cancel.php?booking_id=".$row['booking_id']."'>Cancel</a></td>";
You can now retrieve this value on the Cancel.php page like this:
$booking_id = $_GET['booking_id'];
It's worth reading up about SQL injection and consider adding some cleaning method for your string to avoid this. If you are a little rusty on GET and POST then you can check this explanation out.

Mysql+php, how to make a link that shows all data from a record when you press the "headline"

As the title says, and this is not probably a question, more a hint how to do it.
I have table with four fields, id (auto+primary), firstname, lastname, age.
On my index page I select *, then I make the firstname to a link which goes to antoher page which I want to show all data for that record.
For the moment I have to do it manually "select * from " where id="2". (the other page)
How do I make the link to autodetect "the id from that record set" and only display data from that record.
You can see my curreny project here,
http://www.adamskymotorkylare.se/business/
as you can see, when you click the "firstname" it will always display data where id=2 ( paris hilton ), what I want it to do, it when I click "jack" it will select * from where id="1"
Thanks in advance, Jack
"index page"
$result = mysqli_query($con,"SELECT * FROM persons");
echo "<table width=100%>
<tr>
<th>ID</th>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>Gender</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
$id = $row['id'];
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td> <a href='view_more.php?id= . $id .'>" . $row['firstname'] . "</a> </td>";
echo "<td>" . $row['lastname'] . "</td>";
echo "<td>" . $row['age'] . "</td>";
echo "<td>" . $row['gender'] . "</td>";
echo "</tr>";
}
echo "</table>";
"viewmore page"
$id = $_get['id'];
$result = mysqli_query($con,"SELECT * FROM persons
WHERE id='$id'");
echo "<table width=100%>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>Gender</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td> <a href='#'>" . $row['firstname'] . "</a> </td>";
echo "<td>" . $row['lastname'] . "</td>";
echo "<td>" . $row['age'] . "</td>";
echo "<td>" . $row['gender'] . "</td>";
echo "</tr>";
}
echo "</table>";
Try this, on your page that indexes the users:
while($row = mysqli_fetch_array($result))
{
$id = $row['id'];
$firstName = $row['firstName'];
echo ('' . $firstName . '');
}
This means if someone clicks the first name they will be sent to user_account.php (can replace this obviously) and you will pass in the ID via the URL (user_account.php?id=123).
On the User Account page you want to do the following:
// GET ID FROM THE URL
$id = $_GET['id'];
$result = mysqli_query($con,"SELECT (WHATEVER YOU WANT) FROM (YOUR TABLE) WHERE id = $id");
Notes:
Replace variables and query with the details you need.
I hope that goes some way to helping.
UserFirstName
$id is user record id first column of your table so when you send it to view_more.php page you can get clicked id $_GET['id']; and get user records for that id and link is user name
and $id you get form data base the way you get first name
index.php
<?php
$sql =mysql_query("select * from `table`");
while($row = mysql_fetch_array($sql)){
?>
<?php $id = $row['id']; ?>
<?php echo ucwords($row['Firstname']); ?>
<?php
}
?>
on index.php anchor tag will send the user id to detail.php
detail.php
<?php
$id = $_REQUEST['cid'];
$sql =mysql_query("select * from `table` where id='".$id."'");
while($row = mysql_fetch_array($sql)){
?>
<tr>
<th>First Name:</th>
<th>Last Name:</th>
<th>Age Name:</th>
<th>Gender:</th>
</tr>
<tr>
<td><?=$row['firstname']?></td>
<td><?=$row['lastname']?></td>
<td><?=$row['age']?></td>
<td><?=$row['gender']?></td>
</tr>
<?php
}
?>
on detail.php $id get the user id from index.php. After that id helps in query and while loop which is get and show the user data from database.

PHP fetch comments from MySQL and edit/enable/disable to show on comments page

I have a comments/testimonials form which saves data into MySQL database testimonials. Now through admin page I want to edit and enable or disable the comments which should show on my comments page. I don't know how to do this.
$query=mysql_query("SELECT * FROM `testimonials` ORDER BY
`t_id` DESC") or die(mysql_error());
if(!$query) {echo "Error!";} else { echo "Testimonials";}
echo "<table border='1'>
<tr>
<th>ID</th>
<th>Name,City</th>
<th>Email</th>
<th>Comment</th>
<th>Enable</th>
<th>Edit</th>
</tr>";
while($row = mysql_fetch_array($query))
{
echo "<tr>";
echo "<td>" . $row['t_id'] . "</td>";
echo "<td>" . $row['t_name'] . "</td>";
echo "<td>" . $row['t_email'] . "</td>";
echo "<td>" . $row['t_message'] . "</td>";
echo "<td>" . $row['enable'] . "</td>";
echo "<td>" . $row['edit'] . "</td>";
echo "</tr>";
}
echo "</table>";
Now I want a checkbox to show where $row['enable'] is and an Edit & Save/Update button where $row['edit'] is. When I click 'edit' button I can edit the fields and save them to Database. When I check the checkbox the value in 'enable' field pertaining to those particular comments in database is updated to 'True' so that those can display on the Testimonials page.

item to hide once overdue by 1 day

Ok, I am fairly new to the PHP scene so please correct anything I might have done wrong ect. I am trying to do a calender list, which as soon as it get to the day after the due date it hides it from view. I currently have it getting the information that is submitted however not sure how to continue from here to make the overdue items hide? I have attached my code below for feedback also.
<?php
include 'db-connect.php';
$result = mysqli_query($con,"SELECT * FROM tasks");
echo "<table border='0' align='center'>
<tr>
<th>Task Title:</th>
<th>Task Description:</th>
<th>Due Date:</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['task'] . "</td>";
echo "<td>" . $row['description'] . "</td>";
echo "<td>" . $row['datedue'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
You could do it in your MYSQL query
SELECT * FROM tasks WHERE datedue >= CURDATE()
This would only return the rows that were higher or equal to the current date.

Categories