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>
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
}
?>
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 :)
My idea is to click 'Delete' link and it will pass the id to another PHP page (deleteSession.php), and then execute the query in deleteSession.php. but I couldn't seems to get the id from manageSession.php
In manageSession.php,
<table align='center' border='1' cellpadding='5' cellspacing='0'>
<tr>
<th>Session Id</th>
<th>Type</th>
<th>Date & Time</th>
<th>Venue</th>
<th>Pax</th>
<th>Delete</th>
<th>Edit</th>
</tr>
<?php
$sql = "SELECT booking_id, booking_types, dates_sessions, venue_available, room_count FROM bookings_available ORDER BY dates_sessions asc";
$result = mysqli_query($link, $sql) or die(mysqli_error($link));
//mysqli_close($link);
while ($row = mysqli_fetch_array($result)) {
?>
<tr>
<td><?php echo $row['booking_id']; ?></td>
<td><?php echo $row['booking_types']; ?></td>
<td><?php echo $row['dates_sessions']; ?></td>
<td><?php echo $row['venue_available']; ?></td>
<td><?php echo $row['room_count']; ?></td>
<td><input type="button" value="Delete"/></td>
<td><input type="button" value="Edit"/></td>
</tr>
<?php } ?>
</table>
In deleteSession.php,
<?php
include "dbFunctions.php";
include "manageSession.php";
//$sql = "SELECT booking_id, booking_types, dates_sessions, venue_available, room_count FROM bookings_available";
//$result = mysqli_query($link, $sql) or die(mysqli_error($link));
$bookingId = filter_input(INPUT_GET, 'booking_id');
$deleteQuery = "DELETE FROM bookings_available WHERE booking_id = '$bookingId'";
?>
I think in deleteSession.php file code should be as follows.
$bookingId = filter_input(INPUT_GET, 'id');
OR
$bookingId = $_GET['id'];
Because you are passing get parameter as follows.
deleteSession.php?id=
And also keep anchor as follows.
Delete
In the deleteSession.php you can try and replace:
$bookingId = filter_input(INPUT_GET, 'booking_id');
with the below code:
$bookingId = $_REQUEST['id'];
Finally at the last line you have to execute the query which is stored in $deleteQuery variable, which is not executed yet by using below code:
$qry = mysql_query("DELETE FROM bookings_available WHERE booking_id = '$bookingId'");
//will show you error if not able to delete
if(!$qry)
die("Error: ".mysql_error());
Added this at line 3 and it works:
mysqli_select_db($link ,$DB);
Because in the code I have not selected the mysql database and also the query was not executing as the first parameter $link was missing.
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!