i'm designing a Gym Administration System, each member has a unique number stored in a database, each member should type his number when he entered the gym to make sure if his subscription is ended or not, i already design that page, but i need that page to ( add the last 3 visits for each member ) ? how can i do it ?
i need to overwrite the last 3 visits everytime .
ID's 1,2 and 3 are the last visit for member
$con = mysqli_connect('localhost', 'root', '', 'test') or die('error' . mysqli_errno($con));
$query1 = mysqli_query($con, "SELECT * FROM checkin WHERE id = 1");
while ($row = mysqli_fetch_array($query1)){
?>
<div id="checkIn">
<table>
<tr>
<td>Check-In</td>
<td><?php echo $row['timeIn']; ?></td>
<td><?php echo $row['dateIn']; } ?></td>
</tr>
<?php
$query2 = mysqli_query($con, "SELECT * FROM checkin WHERE id = 2");
while ($row = mysqli_fetch_array($query2)){
?>
<tr>
<td>Check-In</td>
<td><?php echo $row['timeIn']; ?></td>
<td><?php echo $row['dateIn']; } ?></td>
</tr>
<?php
$query3 = mysqli_query($con, "SELECT * FROM checkin WHERE id = 3");
while ($row = mysqli_fetch_array($query3)){
?>
<tr>
<td>Check-In</td>
<td><?php echo $row['timeIn']; ?></td>
<td><?php echo $row['dateIn']; } ?></td>
</tr>
</table>
First create a query to fetch all users than loop through them.
$userQuery = $con->prepare("SELECT user_id FROM users");
$userQuery->execute();
$users = mysqli_fetch_array(userQuery);
foreach($users as $user) {
$visitedQuery = $con->prepare("SELECT timeIn, dateIn FROM checkin WHERE user_id = ? ORDER BY id DESC LIMIT 3");
$visitedQuery->execute(array($user['user_id']));
$lastCheckIn = mysqli_fetch_array($visitedQuery);
foreach($lastCheckIn as $lastCheck) {
echo $lastCheckIn['timeIn']." ".$lastCheckIn['dateIn'];
}
}
You mentioned that each user have unique number.
For example:- User A have 123456 unique number.
In Header.php check the condition that user login or not. like:-
if($isset($_SESSION['user_login'])) //you can set your condition here according to your code
{
// first create a new table where user's visit will save.
// Say user database table name is USER_LOGS.which have 3 column.
// ID,User_unique_id,viewpage
$query1 = mysqli_query($con, "SELECT Id,viewpage FROM USER_LOGS WHERE User_unique_id = 12345 ORDER BY Id DESC LIMIT 0"); // that will give you the user's last log.
$total_rows = mysqli_num_rows($query1);
$current_page = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
if($total_rows > 0)
{
$row = mysqli_fetch_array($query1);
if($row['viewpage'] != $current_page)
{
mysqli_query($con,"INSERT INTO USER_LOGS (Id,User_unique_id,viewpage)
VALUES ('','123456','".$current_page."')");
}
}
else
{
mysqli_query($con,"INSERT INTO USER_LOGS (Id,User_unique_id,viewpage)
VALUES ('','123456','".$current_page."')");
}
}
The above code is for inert the user log into the database now you can fetch the last three visit by using this query.
mysqli_query($con, "SELECT Id,viewpage FROM USER_LOGS WHERE User_unique_id = 12345 ORDER BY Id DESC LIMIT 0,3");
Related
I have 2 tables tbl_category and tbl_food.
I sorted the items from tbl_food to tbl_category by adding category_id to tbl_food, where category_id in tbl_food is identical to id in tbl_category.
But how do I take title from tbl_category and display it in a table that represents items from tbl_food?
<?php
//query to get all admin from dtb
$sql = "SELECT * FROM tbl_food";
//Exectue the query
$res = mysqli_query($conn, $sql);
//check if executed
if($res==TRUE)
{
//count rows to check wether we have data
$count = mysqli_num_rows($res); //get all rows in dtb
$sn=1; //create a variable and assign the value
//check numb of rows
if($count>0)
{
//there is data
while($rows=mysqli_fetch_assoc($res))
{
//using while loop to get data from dtb
//get individual data
$id=$rows['id'];
$title=$rows['title'];
$description=$rows['description'];
$price=$rows['price'];
$active=$rows['active'];
$category_id=$rows['category_id'];
//display values in table
?>
<tr>
<td><?php echo $sn++; ?></td>
<td><?php echo $title; ?></td>
<td><?php echo $description; ?></td>
<td><?php echo $price; ?> kn</td>
<td><?php echo $active; ?></td>
<td><?php echo $category_id; ?></td>
</tr>
<?php
}}
else{
?>
<tr colspan="4">
<td class="error">No categories added</td>
</tr>
<?php
}
}
?>
I think you need to edit your query by adding JOIN to join tbl_category to tbl_food via category_id, at first, like this:
$sql = "SELECT food.*, category.title FROM tbl_food AS food INNER JOIN tbl_category AS category ON category.id=food.category_id";
Secondly it seems you have mistake in iterating query results. mysqli_fetch_assoc($res) returns an associated array, so it must be like this:
$rows = mysqli_fetch_assoc($res)
foreach($rows as $row){
$id=$row['id'];
$title=$row['title'];
$description=$row['description'];
$price=$row['price'];
$active=$row['active'];
$category_id=$row['category_id'];
//Doing stuff
}
You have to method for get title from tbl_category
1- JOIN Method: it is already mentioned above
2- Condition Method:
$sql = "SELECT tbl_food.*, tbl_category.title FROM tbl_food,tbl_category WHERE tbl_category.id=tbl_food.category_id";
foreach($sql as $row){
$id=$row['id'];
$title=$row['title'];
$description=$row['description'];
$price=$row['price'];
$active=$row['active'];
$category_id=$row['category_id'];
}
How to display most recent logs for every user?
My database tables are user_log_id, username, login_date, logout_date, user_id. Can you please help me with the query? Thank you
Heres my code:
<?php
$user_query = $conn->query("select * from user_log order by user_log_id DESC ")or die(mysql_error());
while($row = $user_query->fetch()){
$id = $row['user_log_id'];
?>
<tr>
<td><?php echo $row['login_date']; ?></td>
<td><?php echo $row['logout_date']; ?></td>
<td><?php echo $row['username']; ?></td>
</tr>
<?php } ?>
The MySql query you used is correct for getting recent logs. But if you need to get only for specific users or only for logged in users, you have to add a where clause in the query.
select * from user_log where username=".$row['username']." order by user_log_id DESC
This is the query I have written to fetch the records from 3 different tables and display them in one simple table.
$order_id=$_REQUEST['a'];
$query="select DISTINCT product.productId,orders.id,retailerName, total, status, delivery,shopName,ownerName,address,email,contact,notes,order_item.quantity,order_item.date,productName,price,product.detail,product.price,product.expiry from orders,checkout,order_item,product where product.productId=order_item.productId and orders.id ='".$order_id."' and orders.id=order_item.orderId and orders.id=checkout.orderId";
$res=mysqli_query($conn,$query);
$row=mysqli_fetch_array($res);
The below code is written for display of record and detail.
<?PHP
while($row=mysqli_fetch_array($res)){
?>
<tr>
<?php
$subtotal=0;
$subtotal = $row['quantity'] * $row['price'];
$total += $subtotal;
?>
<td><?php echo $row['productName'];?></td>
<td><?php echo $row['quantity'];?></td>
<td><?php echo $row['price'];?></td>
<td><?php echo $subtotal;?></td>
</tr>
<?PHP
}
?>
It displays all of the orders from database whereas I want the orders to be displayed for specific company.
If you have button for show only one result you should link it with some id.
For example:
Delete
Make SQL query show only one result: use WHERE clause.
if(isset($_GET['id'])) {
$id = $_GET['id'];
$q = "DELETE FROM table WHERE id = '$id'";
}
$db->query($q);
I need to run two statements to return the data I need. The below code will return all needed except the count column which can be retrieved from a different table.
How can I run these two statements in the same code to retrieve the count as well?
Here is the code I have:
<?php
$query = "SELECT * from $CLIENTS_DATABASE order by id DESC";
if ($result = mysqli_query($conn, $query)) {
while ($row = mysqli_fetch_assoc($result)) {
?>
<tr>
<td><?php echo $row["name"]; ?> </td>
<td><?php echo $row["url"]; ?> </td>
<td><?php echo $row["secret"]; ?> </td>
<td><?php echo $row["count"]; ?> </td>
</tr>
<?php
}
mysqli_free_result($result);
}
?>
The other statement that the count data I need is this:
$query = "SELECT COUNT(*) as count from visits where id_client='$result[id]'"
Thanks for your time and any help you can provide.
Join the two queries.
$query = "SELECT order.*, IFNULL(c.count, 0) AS count
FROM $CLIENTS_DATABASE AS order
LEFT JOIN (SELECT id_client, COUNT(*) AS count
FROM visits
GROUP BY id_client) AS c
ON c.id_client = order.id
ORDER BY order.id DESC";
I am working on a custom content management system. I was instructed to do some changes, and this is what I need to do. I need to create a user management page which allows the administrator to delete (or disable his status) a user from the database.
This is my User Management Page:
<?php
$query = 'SELECT author_id, author_email as Email, author_name as Name
FROM authors
ORDER BY Name
LIMIT 0, 30';
$result = mysql_query($query);
?>
<table class="listing">
<thead>
<tr>
<td>Author ID</td>
<th>Author E-Mail</th>
<th>Author Name</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
<?php
for ($i = 0; $row = mysql_fetch_array($result); $i++) {
if ($i % 2 == 0) {
echo '<tr class="even">';
} else {
echo '<tr class="odd">';
}
echo "<td>{$row['author_id']}</td>";
echo "<td>{$row['Email']}</td>";
echo "<td>{$row['Name']}</td>";
echo "<td>X</td>";
echo '</tr>';
}
?>
</tbody>
</table>
This is my del-user.php page:
<?php
include('inc/config.php');
$title = 'Delete Individual User';
include('inc/db.php');
include('inc/header.php');
echo '<h2>Delete</h2>';
if (isset($GET['term'])) {
$query = "DELETE FROM authors WHERE author_id = {$GET['term']} LIMIT 1";
mysql_query($query) or die('Failed to delete user');
echo '<p>User Deleted</p>';
echo '<p>Back to <a href="manage-users.php">Manage Users </>.</p>';
} else {
echo '<p>Tried to Delete: "';
echo ($GET['term']);
echo '"</p>';
echo '<p>Nothing to Delete</p>';
}
include('inc/footer.php');
?>
I am new to PHP, but this is not working, the author_id value is not being passed to the other page, and it is being left empty. So I cannot delete anything from the del-users.php page.
I'm guessing that this is the problematic part:
echo "<td>X</td>";
Anybody knows why this is happening?
Several issues:
You send data like this:
del-user.php?term={$row['author_id']}
So that means that actualy $_GET['term'] contains the id.
You catch the value like this:
if (isset($_GET['author_id'])) {
$query = "DELETE FROM authors WHERE author_id = {$_GET['author_id']} LIMIT 1";
And it is not good, since $_GET['term'] contains the id, so you have to fix the lower one to look like this:
if (isset($_GET['term']))
$query = "DELETE FROM authors WHERE author_id = {mysql_real_escape_string($_GET['term'])} LIMIT 1";
Also you need to expand the select query, since you are not actualy fetching the author_id from the db:
$query = 'SELECT author_email as Email, author_name as Name, author_id
FROM authors
ORDER BY Name
LIMIT 0, 30';
Please, escape your variables before you trow them to the database...
http://php.net/manual/en/function.mysql-real-escape-string.php
Cheers
the problem is your query!
$query = 'SELECT author_email as Email, author_name as Name
FROM authors
ORDER BY Name
LIMIT 0, 30';
you are not selecting the author_id
You pass your user id in the url like this :
echo "<td><a href=\"del-user.php?term={$row['author_id']}\"
The you must GET term, not author_id :
$query = "DELETE FROM authors WHERE author_id = {$GET['term']} LIMIT 1";
And by the way, you should read about prepared query and sql injection ;)
use author_id in your query
<?php
$query = 'SELECT author_id, author_email as Email, author_name as Name
FROM authors
ORDER BY Name
LIMIT 0, 30';
$result = mysql_query($query);
?>