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
}
?>
Related
I am making a school portal system and right now I am making the page for students to view homework. I want their homework to be highlighted in red, or not shown at all if the current date is past the due date. How can I do this?
My code for the page
<?php
//including the database connection file
include_once("connection.php");
$id= $_GET['id'];
//fetching data in descending order (lastest entry first)
$result = mysqli_query($conn, "SELECT * FROM homework where class_id= '$id'");
?>
<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>
</tr>
<?php
//while($res = mysql_fetch_array($result))
while($res = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>".$res['description']."</td>";
echo "<td>".$res['dateset']."</td>";
echo "<td>".$res['datedue']."</td>";
}
?>
</table>
</body>
Database:
Instead of a query, use a prepared statement with bind_param which is much safer. Then just compare the dates to check if $res['datedue'] passed or not. This should be it:
<?php
include_once("connection.php"); //including the database connection file
date_default_timezone_set('America/Los_Angeles'); //set the default time zone to your time zone (this is just an example)
$result = $conn->prepare("SELECT * FROM homework WHERE class_id=?");
$result->bind_param("i", (int)$_GET['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>
</tr>
<?php
while($res = $result2->fetch_array(MYSQLI_ASSOC)) {
if (date("Y-m-d") > $res['datedue']) {
echo "<tr style=\"color: red;\">";
echo "<td>".$res['description']."</td>";
echo "<td>".$res['dateset']."</td>";
echo "<td>".$res['datedue']."</td>";
echo "</tr>";
} else {
echo "<tr>";
echo "<td>".$res['description']."</td>";
echo "<td>".$res['dateset']."</td>";
echo "<td>".$res['datedue']."</td>";
echo "</tr>";
}
}
?>
</table>
</body>
You could also use $time = new DateTime; and then $time->format("Y-m-d") instead of date("Y-m-d").
More about time zones.
You should use parameterized prepared statements instead of manually building your queries.
I have used the date function to compare the dates if the date is greater then I've put some style while in else there is no style. I have gave you the idea now you can modify accordingly.
<table width='80%' border=0>
<tr bgcolor='#CCCCCC'>
<td>Task</td>
<td>Date Set </td>
<td>Date Due </td>
</tr>
<?php
//while($res = mysql_fetch_array($result))
$current_date=date("Y-m-d");
while($res = mysqli_fetch_array($result)) {
if($current_date > $res['datedue'] ){
?>
<tr style='color:red;'>;
<?php
}else {
<tr>
}
echo "<td>".$res['description']."</td>";
echo "<td>".$res['dateset']."</td>";
echo "<td>".$res['datedue']."</td>";
</tr>
}
?>
</table>
Reference
Don`t use concated raw SQL.
Use variable binding to prevent SQL injections.
<?php
$stmt = $mysqli->prepare("SELECT * FROM homework where class_id= ?");
$stmt->bind_param('i', (int)$_GET['id']); // bind vairable and cast it to integer (it is a good practice when you get data from outside)
$stmt->execute();
$result = $stmt->get_result();
while($res = $result->fetch()){
// Your code here
var_dump($res);
}
I will suggest using PDO instead of mysqli. You can read more about SQL Injections here: How can I prevent SQL injection in PHP?
Don't use select * (wildcard)
SELECT description, dateset, datedue, datedue < NOW() AS is_expired FROM homework where class_id= ?
Check the value of is_expired to see which results you will mark in red
I haven't tried the code but I guess it will work as expected
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>
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."'";
l have created an application using php,html and mysql. The application can store a user's information such as id, name, bio, and date created into the database and display in html table. The id is an auto increment value which increases with every data entered by the user. The insert part of the application works fine but when l try to delete a record nothing happens. An html form is part of the code which l have intentionally decided not to include. Here is a snapshot of my code:
$records = array();
if(!empty($_POST)) {
if(isset($_POST['firstName'],$_POST['lastName'], $_POST['bio'])){
$firstName = trim($_POST['firstName']);
$lastName = trim($_POST['lastName']);
$bio = trim($_POST['bio']);
if(!empty($firstName) && !empty($lastName) && !empty($bio)) {
$insert = $db->prepare("INSERT INTO people (firstName, lastName,
bio, created) VALUES (?, ?,?, NOW())");
$insert->bind_param('sss', $firstName, $lastName, $bio);
if($insert->execute()){
header('Location: addressbook.php');
die();
}
}
}
}
if($results = $db->query("SELECT * FROM people")){
if($results->num_rows){
while($row = $results->fetch_object()){
$records[] = $row;
}
$results->free();
}
}
?>
<!DOCTYPE html>
<html>
<head></head>
<body>
<div class = "container">
<?php
if(!count($records)){
echo 'No records found';
}
else{
?>
<table class="table">
<thead>
<tr>
<th>ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>Bio</th>
<th>Created</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
<?php
foreach ($records as $r) {
?>
<tr>
<td><?php echo escape($r->id);?></td>
<td><?php echo escape($r->firstName); ?></td>
<td><?php echo escape($r->lastName); ?></td>
<td><?php echo escape($r->bio); ?></td>
<td><?php echo escape($r->created); ?></td>
<td>
<a onclick="return confirm('Do you want to delete the
record')" href="addressbook.php?idd=<?php echo $row['id'] ?>"
class="btn btn-
danger">Delete</a></td>
<?php
}
?>
</tr>
//My guess is the problem is with this code down here for deleting
<?php
if(isset($_POST['idd'])){
$idd = $_POST['idd'];
$results = $db->query("DELETE FROM people WHERE id=$idd");
if($results){
header('Location: addressbook.php');
}
}
?>
</tbody>
</table>
<?php
}
?>
you need to use $_GET because by default href tag sends the data with GET method.
your code should be
if(isset($_GET['idd'])){
$idd = $_GET['idd'];
$results = $db->query("DELETE FROM people WHERE id='$idd'");
if($results){
header('Location: addressbook.php');
}
}
NOTE- use prepared statement for avoiding sql injection attack
`
<?php
//database connectivity
$con=mysqli_connect("localhost","root","");
mysqli_select_db($con,"<db_name>");
$idd = $_REQUEST['idd'];
$sql= "DELETE FROM people WHERE id='$idd' ";
$result = mysqli_query($con,$sql) or die(mysql_error());
header("refresh:0.1; addressbook.php");
?>`
if(isset($_GET['idd'])){
$idd = $_GET['idd'];
$results = $db->query("DELETE FROM people WHERE id='{$idd}'");
Try adding a single quote.
If it still doesn't work, please see if the $_POST is actually posting correctly.
Try $results = $db->query("DELETE * FROM people WHERE id=$idd"); instead of $results = $db->query("DELETE FROM people WHERE id=$idd"); in the delete User Function :)
I am outputting newsletter email registrations.
How can I format them into tables and what edits can I do so that I can drop that row also.
My PHP code is :
<?php include "navbar-datacheck.php" ?>
<?php
include "../backend/db.php";
$sql = "SELECT * from newsletter";
$result = $mysqli->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
print "id: " . $row["id"]. " - Email ID: " . $row["email"]."<br>";
}
} else {
echo "0 results";
}
$mysqli->close();
?>
Construct your table comprising of all the newsletter details in the following manner,
// your code
if ($result->num_rows > 0) {
// output data of each row
?>
<table>
<tr>
<td>ID</td>
<td>Email</td>
<td>Action</td>
</tr>
<?php
while($row = $result->fetch_assoc()) {
?>
<tr>
<td><?php echo $row["id"]; ?></td>
<td><?php echo $row["email"]; ?></td>
<td>Delete</td>
</tr>
<?php
}
?>
</table>
<?php
} else {
echo "0 results";
}
// your code
... what edits can I do so that I can drop that row also.
As you can see, I have added a third column Action in your table so that you could delete a row of your choice at any given time. So once the user clicks on Delete link, this is how you should process and delete the corresponding record in delete.php file.
if(isset($_GET['id']) && !empty($_GET['id'])){
$id = $_GET['id'];
// Now delete the corresponding record from the table
}