So I have a table named realtimeusage it contains ID, KWH, UnitValue, AccessTIME I want to fetch usage only for the Current user by his "id" any suggestion for my code
<?php
session_start();
require_once('connect.php');
$_SESSION['id'] = $id;
// For display Current user realtimeusage
$displayquery = "SELECT * ";
$displayquery .= "FROM realtimeusage WHERE `id` = '".$_SESSION['id']."'";
$displayresult = mysqli_query($connection, $displayquery);
if (!$displayresult){
die("database query failed");
}
?>
the table to fetch data:
<table>
<thead>
<tr>
<th> AccountID</th>
<th> KWH</th>
<th>UnitValue</th>
<th>AccessTIME</th>
</tr>
</thead>
<tbody>
<?php
while ($rows= mysqli_fetch_assoc($displayresult)) {
?>
<!--id-->
<td><?php echo $rows["ID"]; ?></td>
<!--User name-->
<td><?php echo $rows["KWH"]; ?></td>
<!--Full name-->
<td><?php echo $rows["UnitValue"]; ?></td>
<!-- Roles-->
<td><?php echo $rows["AccessTIME"]; ?></td>
</tbody>
<?php } ?>
</table>
when I run this code it shows all usage in the table
<?php
session_start();
require_once('connect.php');
$username = $_SESSION['username'];
$roles = $_SESSION['roles'];
// For display realtimeusage
$displayquery = "SELECT * ";
$displayquery .= "FROM realtimeusage";
$displayresult = mysqli_query($connection, $displayquery);
if (!$displayresult){
die("database query failed");
}
?>
From where are you getting the value of id? I guess id is in $_SESSION['id'], if user is logged in, and to use that id you need to change the assignment statement as
$id=$_SESSION['id'];
And use $id in query
$displayquery .= "FROM `realtimeusage` WHERE `id` = '".$id."'";
Related
right now I have a page which displays homework set by teachers from a database. The students must be able to see all their homework on this page, with the due date and set date. As of now, it's working and after the due date, the task turns red, which is fine. However, I need to now add a small box or button which can be clicked by the student once they have completed the task. Once this is done, It would delete it ONLY for the student which has clicked it.
<?php
include_once("connection.php"); //including the database connection file
$id= $_GET['id'];
$result = $conn->prepare("SELECT * FROM homework WHERE class_id=? ORDER BY datedue DESC");
$result->bind_param("i", $id);
$result->execute();
$result2 = $result->get_result();?>
<html>
<head>
<title>View IS</title>
</head>
<body>
<table width='80%' border=0>
<tr bgcolor='#CCCCCC'>
<td>Task</td>
<td>Date Set </td>
<td>Date Due </td>
<td><button type="button">Click Me!</button></td>
</tr>
<?php
while($res = mysqli_fetch_array($result2)) {
if (strtotime(date("d-m-Y")) > strtotime($res['datedue'])) {
echo "<tr style=\"color: red;\">";
echo "<td>".$res['description']."</td>";
echo "<td>".$res['dateset']."</td>";
echo "<td>".$res['datedue']."</td>";
echo "<td>".<button type=button>Click Me!</button>."</td>";
echo "</tr>";
} else {
echo "<tr>";
echo "<td>".$res['description']."</td>";
echo "<td>".$res['dateset']."</td>";
echo "<td>".$res['datedue']."</td>";
echo "<td>".<button type=button>Click Me!</button>."</td>";
echo "</tr>";
}
}
?>
</table>
</body>
How can I do this? Thank you
I couldn't test this, can you give this a try and let me know if error occurs
create new field name 'stud_completed' in homework table
homework.php page
<?php
include_once("connection.php"); //including the database connection file
$id = $_GET['id'];
$result = $conn->prepare("SELECT * FROM homework WHERE class_id=? ORDER BY datedue DESC");
$result->bind_param("i", $id);
$result->execute();
$result2 = $result->get_result();
$todayDate = strtotime(date("d-m-Y"));
$Log_student = $_SESSION['studentID'];
?>
<html>
<head>
<title>View IS</title>
</head>
<body>
<table width='80%' border=0>
<tr bgcolor='#CCCCCC'>
<td>Task</td>
<td>Date Set </td>
<td>Date Due </td>
<td>Action</td>
</tr>
<?php
while($res = mysqli_fetch_array($result2)) {
$redDueTask = null; // each loop $redDueTask will be set to NULL
$homeworkID = $res['id']; // Get the DueDate of each task
$dueDate = strtotime($res['datedue']); // Get the DueDate of each task
if ($todayDate > $dueDate) { $redDueTask = 'style="color: red;"'; } // Set $redDueTask if task has past duedate
$student_completed = explode(',',$res['stud_completed']); // get the coma seperated completed student list and convert it to array
if (!in_array($Log_student, $student_completed)) { // chk if logged in student ID is in array and if not in the list show task
?>
<tr <?php echo $redDueTask?>>
<td><?php echo $res['description']?></td>
<td><?php echo $res['dateset']?></td>
<td><?php echo $res['datedue']?></td>
<td>
<?php if (isset($redDueTask)) { // $redDueTask will bset if the task duedate has passed, so no need compelete button ?>
Time UP!
<?php } else { // $redDueTask is not set then show compelete button ?>
<button type='button'>Have Complete</button>
<?php } ?>
</td>
</tr>
<?php
}
}
?>
</table>
</body>
taskdone.php page
<?php
include_once("connection.php"); //including the database connection file
$tid = $_GET['tid']; // Get Homework Task ID from URL
$Log_student = $_SESSION['studentID']; // Get Loggedin Student ID from Session
// Get ROW Statment
$result = $conn->prepare("SELECT * FROM homework WHERE id=?");
$result->bind_param('i', $tid);
$result->execute();
$result2 = $result->get_result();
$res = mysqli_fetch_array($result2);
$stud_completed = $res['stud_completed']; // Get the current List of completed student
if ($stud_completed == "") { // If stud_completed is null or blank
$stud_completed = $Log_student; // add the current student ID with out coma
} else {
$stud_completed .= "," . $Log_student; // Inculde the current logged in student ID with coma
}
// Update ROW Statement
$sql = "UPDATE homework SET stud_completed=? WHERE id=?";
$stmt = $conn->prepare($sql);
$stmt->bind_param('i', $stud_completed, $tid);
if ($stmt->execute()) {
header("homework.php"); // if GOT updated go to home work task list page
}
?>
I am working on some code for a php assignment, I get the correct id from the URL, the table displays all the correct records that correspond to that person, my delete button does not however work right, I either delete records in the table pertaining to the person or I get errors.
My PHP Portion above the head
<?php require "config/config.php"; ?>
<?php
if(isset($_GET['upd'])){
$id = $_GET['upd'];
$query = "SELECT * FROM persons WHERE id=$id";
$fire = mysqli_query($con,$query) or die("Can not fetch the data.".mysqli_error($con));
$user = mysqli_fetch_assoc($fire);
}
?>
My delete Portion above the head
<?php
if(isset($_GET['delweight'])){
$weightid = ($_GET['weightid']);
$query = "DELETE FROM personweight WHERE weightid = $weightid";
$fire = mysqli_query($con,$query) or die("Can not delete the data from database.". mysqli_error($con));
if($fire) echo "Data deleted from database";
}
?>
My Table with the delete record
<table class="table table-striped table-dark" id="weightTable">
<thead>
<tr><th>weightid</th><th>Weight</th><th>Date</th><th>Delete</th></tr>
</thead>
<tbody>
<?php
$query = "SELECT * FROM personweight WHERE id=$id";
$fire = mysqli_query($con,$query) or die("can not fetch data from datase ".mysqli_error($con));
if(mysqli_num_rows($fire)>0){
while($user = mysqli_fetch_assoc($fire)){ ?>
</tr>
<td><?php echo $user['weightid'] ?></td>
<td><?php echo $user['weight'] ?></td>
<td><?php echo $user['added'] ?></td>
<td>
Delete
</td>
</tr>
<?php }} ?>
</tbody>
</table>
I want to arrange the table head with my table body, so the data fill have a same type, how can i doing it??
The black square is the correct one
This is my code
<table>
$syntax = 'SELECT customer,type, count(type) as num FROM view_detail GROUP BY type ORDER BY customer';
$rows = $config->prepare($syntax);
$rows->execute();
$result = $rows->fetchAll(PDO::FETCH_OBJ);
?>
</tr>
<td>No</td>
<td>Customer</td>
<?php for($x=0;$x<count($result);$x++){ ?>
<td><?php echo $result[$x]->type; ?></td>
<?php } ?>
<td>Total</td>
<?php $no = 1;
$query = 'SELECT customer,type, count(type) as tipe FROM view_detail GROUP BY customer ORDER BY customer';
$baris = $config->prepare($query);
$baris->execute();
$hasil1 = $baris->fetchAll(PDO::FETCH_OBJ);
for($i=0;$i<count($hasil1);$i++){
$sql = 'SELECT * FROM view_detail WHERE customer = "'.$hasil1[$i]->customer.'" ORDER BY customer';
$row = $config->prepare($sql);
$row->execute();
$hasil = $row->fetchAll(PDO::FETCH_OBJ);
?>
<tr>
<td><?php echo $no; $no++;?></td>
<td><?php echo $hasil[$i]->customer; ?></td>
<?php
for($y=0;$y<count($result);$y++){
if($y < count($hasil)){
?>
<td><?php echo $hasil[$y]->type; ?></td>
<?php }else{ ?>
<td>0</td>
<?php } } }?>
</tr>
</table>
Thanks
I want to make it like this
Edit:
Dynatrade -> MFN200, N50Z, and N70
Super Bloom -> N70, MFN100, MFN120, and MFN150
I am trying to set up a PHP MySQL search script which will let me print the content of my membership MySQL database table and then filter the results using different criteria.
The following fields are used in my MySQL table:
committee_id
rank
last_name
first_name
sex
address
email
phone_number
active_status
I want 2 ways to filter the data:
1) using using a drop down with all the available rank i can filter the results by rank of member.
2) using a drop down with all the available active_status you can filter the results by active status only.
I have set up my search form successfully, and printed all MySQL Table contents, Only the search by position part is filtering my result, but not the active_status, the filter part is a challenge. here is my html table and search script:
<?php include("./includes/connnect.php");?>
<!DOCTYPE HTML>
<html>
<body>
<table>
<thead>
<tr>
<td>ID</td>
<td>position</td>
<td>Last Name</td>
<td>First Name</td>
<td>Sex</td>
<td>Address</td>
<td><strong>Email</td>
<td><strong>Phone Number</td>
<td>Status</td>
</tr>
</thead>
<tbody>
<?php
if ($_REQUEST["position"]<>'') {
$search_position = " AND position='".mysql_real_escape_string($_REQUEST["position"])."'";
}
if ($_REQUEST["status"]<>'') {
$search_status = " AND status='".mysql_real_escape_string($_REQUEST["status"])."'";
}
else {
$sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE committee_id>0".$search_position.$search_status;
}
$sql_result = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql);
if (mysql_num_rows($sql_result)>0) {
while ($row = mysql_fetch_assoc($sql_result)) {
?>
<tr>
<td><?php echo $row["committee_id"]; ?></td>
<td><?php echo $row["rank"]; ?></td>
<td><?php echo $row["last_name"]; ?></td>
<td><?php echo $row["first_name"]; ?></td>
<td><?php echo $row["sex"]; ?></td>
<td><?php echo $row["address"]; ?></td>
<td><?php echo $row["email"]; ?></td>
<td><?php echo $row["phone_number"]; ?></td>
<td><?php echo $row["active_status"]; ?></td>
</tr>
<?php
}
} else {
?>
<tr><td colspan="5">No results found.</td>
<?php
}
?>
</tbody>
</table>
</body>
</html>
I would appreciate any suggestions to get this going.
Thanks.
That is because when there is $_REQUEST["status"] the variable SQL is not setted as you put it on the else statement
if ($_REQUEST["status"]<>'') {
$search_status = " AND status='" .
mysql_real_escape_string($_REQUEST["status"])."'";
}
else { /// this is your problem
$sql = "SELECT * FROM ".$SETTINGS["data_table"]."
WHERE committee_id>0".$search_position . $search_status;
}
Put the $sql out of the else and take the else out.
Thank you Jorge your solution worked. But I got the same result with these elseif statements:
if ($_REQUEST["position"]<>'') {
$search_position = " AND position='".mysql_real_escape_string($_REQUEST["position"])."'";
}
if ($_REQUEST["status"]<>'') {
$search_status = " AND status='".mysql_real_escape_string($_REQUEST["status"])."'";
}
if ($_REQUEST["position"]<>'' and $_REQUEST["status"]<>'') {
$sql = "SELECT * FROM ".$SETTINGS["data_table"]."
WHERE position = '".mysql_real_escape_string($_REQUEST["position"])."'
AND status = '".mysql_real_escape_string($_REQUEST["status"])."'";
}else if ($_REQUEST["position"]<>'') {
$sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE position = '".mysql_real_escape_string($_REQUEST["position"])."'".$search_city;
}else if ($_REQUEST["status"]<>'') {
$sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE status = '".mysql_real_escape_string($_REQUEST["status"])."'".$search_position;
}else {
$sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE committee_id>0".$search_position.$search_status;
}
I shall have to convert these to mysqli or pdo as advised.
I've recently made a PHP, that should; if click a link delete a certain row within one of my MYSQL tables.
The script below has everything but the link [href=delete_ac.php?id etc...] leads to the page but when the page activates it echo ERROR instead of deleting the row.
<h1>Members</h1>
<table>
<tr>
<th>ID</th>
<th>Username</th>
<th>E-Mail Address</th>
<th></th>
</tr>
<?php foreach($rows as $row): ?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo htmlentities($row['username'], ENT_QUOTES, 'UTF-8'); ?></td>
<td><?php echo htmlentities($row['email'], ENT_QUOTES, 'UTF-8'); ?></td>
<td>delete</td>
</tr>
<?php endforeach; ?>
</table>
delete_ac.php
The script below is what should delete it but it isn't
<?php
require("../php/bp-connectionAdmin.php");
$id=$_GET['id'];
$query = "DELETE FROM `users` WHERE `id` = $id";
$result = mysql_query($query);
if ($result) {
echo "Successful";
} else {
echo "ERROR";
}
?>
Is the ID numeric only? Would the addition of quote marks around $id not help?
$query = "DELETE FROM `users` WHERE `id`='$id'";
mysql_query($query);
Not sure...but give it a go!
Put on the line after $query = "DELETE ..
An
echo "DELETE FROM `users` WHERE `id` = $id";
die;
Then you will see what goes wrong.
Personally i would remove the ', assuming that the id=integer, and you will have:
$query = "DELETE FROM users WHERE id=$id";
If not, try that echood query directly in your Database window and you will see what is wrong.
Most probably you should change your line into
$id=intval($_GET['id']);
which is also much more secure!