adding values to a different column after button click - php

I have a page where users can view the tickets they sent, they can cancel it as well, here's what it looks like:
What I want to happen is that when I click on the "Closed: Cancelled" button. It would show "Closed: Cancelled" in the assignee column as well. Currently this is what I only get when I click the button:
Here's my form:
<div class="container">
<div class="page-header">
<h3>My Tickets</h3>
<div class="table-responsive">
<table class="table">
<tr>
<th>Employee Name</th>
<th>Time</th>
<th>Priority</th>
<th>Assignee</th>
<th>Subject</th>
<th>Problem</th>
<th>Status</th>
<th></th>
</tr>
<?php
include ('database.php');
$result = $database->prepare ("SELECT tickets.* FROM tickets INNER JOIN employee ON employee.id = tickets.employee_id WHERE employee.username = '".$_SESSION["VALID_USER_ID"]."'");
$result ->execute();
for ($count=0; $row_message = $result ->fetch(); $count++){
?>
<tr>
<td><?php echo $row_message['firstname']." ".$row_message['lastname']; ?></td>
<td><?php echo $row_message['time']; ?></td>
<td><?php echo $row_message['priority']; ?></td>
<td><?php echo $row_message['assignee']; ?></td>
<td><?php echo $row_message['subject']; ?></td>
<td><?php echo $row_message['problem']; ?></td>
<?php if ($row_message['status']) : ?>
<td><?php echo $row_message['status']."".$row_message['assignee'];?></td>
<?php else : ?>
<td>
<form method="post" action="update-ticket-status-emp.php">
<input type="hidden" name="ticketno" value="<?php echo $row_message['ticketno']; ?>" />
<input type="submit" name="closedcan" value="Closed: Cancelled"></input>
</form>
</td>
<?php endif ; ?>
</tr>
<?php } ?>
</table>
<button type="button" class="btn btn-primary"><i class="glyphicon glyphicon-arrow-left"></i> Back</button>
</div>
</div>
</div>
</div>
</div>
And here's the exec code:
<?php
if(isset($_POST['closedcan']))
{
$msg = "ClosedCan";
$status = $_POST['closedcan'];
$assignee = $_POST['closedcan'];
}
$ticketno=$_POST['ticketno'];
$con = mysqli_connect('localhost', 'root', '');
mysqli_select_db($con, 'companydb');
$sql = "UPDATE tickets SET status = '$status' WHERE ticketno = '$ticketno'";
if(mysqli_query($con, $sql))
header("refresh:1; url=view-tickets-emp.php?msg=$msg");
else
var_dump(mysqli_error($con));
?>
PS: I know mysql is deprecated, I will change it eventually when I figure this out.

Modified the query in the exec code:
$sql = "UPDATE tickets SET status = '$status', assignee = '$assignee' WHERE ticketno = '$ticketno'";

Related

PHP View data from a single record by clicking on the view button

I have two pages students_list.php and students_profile.php
On students_list.php i have a table showing all records of all students and the VIEW button which when clicked it should open the students_profile.php page and get all the data from the single record selected based on the std_id
UPDATE: So now i get the id http://localhost/sms/student_profiles.php?std_id=3 when i click on the VIEW button but i am still not retrieving the data from this record selected
This is the code for students_list.php
<?php
require_once 'include/database/connect.php';
try {
$pdo = new PDO("mysql:host=$host_db;dbname=$name_db", $user_db, $pass_db);
$sql = 'SELECT * FROM students';
$q = $pdo->query($sql);
$q->setFetchMode(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
die("Could not connect to the database $name_db :" . $e->getMessage());
}
?>
<table id="data-table-default" class="table">
<thead>
<tr>
<th width="1%">#</th>
<th width="1%" data-orderable="false"></th>
<th>Name & Surname</th>
<th>Gender</th>
<th>Faculty</th>
<th>Semester</th>
<th>Mobile</th>
<th></th>
</tr>
</thead>
<tbody>
<?php while ($row = $q->fetch()): ?>
<tr class="odd gradeX">
<td width="1%" class="f-s-600 text-inverse"><?php echo ($row['std_id']) ?></td>
<td width="1%" class="with-img"><?php echo ($row['std_status']) ?></td>
<td><?php echo ($row['std_name']) ?></td>
<td><?php echo ($row['std_gender']) ?></td>
<td><?php echo ($row['std_faculty']) ?></td>
<td><?php echo ($row['std_semester']) ?></td>
<td><?php echo ($row['std_mobile']) ?></td>
<td align="right">
<i class="fas fa-eye"></i> View
</td>
</tr>
<?php endwhile; ?>
</tbody>
</table>
and below this is the student_profile.php
<?php
require_once('include/database/sharepoint.php');
if(isset($_GET) & !empty($_GET)){
$std_id = $_GET['std_id'];
$sql = "SELECT * FROM `students` WHERE std_id=$std_id";
$res = mysqli_query($connection, $sql);
}
?>
<h4><?php echo ($row['std_name']) ?></h4>
<div class="d-flex">
<div class="pr-4"><strong>NATIONAL#</strong> <?php echo ($row['std_number']) ?></div>
<div class="pr-4"><strong>DOB</strong> <?php echo ($row['std_dob']) ?></div>
<div class="pr-4"><strong>GENDER</strong> <?php echo ($row['std_gender']) ?></div>
<div class="pr-4"><strong>MOBILE</strong> <?php echo ($row['std_mobile']) ?></div>
</div>
<div class="pt-1 font-weight-bold">Master's Degree (MBA)</div>
<div>STD# <strong><?php echo ($row['std_id']) ?></strong></div>
<div>email#example.com</div>
You don't fecth the data from the query. So $row will never contain the array you expect:
$row = mysqli_fetch_assoc($res); //this will populate $row
in your code:
<?php
require_once('include/database/sharepoint.php');
if(isset($_GET) & !empty($_GET)){
$std_id = $_GET['std_id'];
$sql = "SELECT * FROM `students` WHERE std_id=$std_id";
$res = mysqli_query($connection, $sql);
$row = mysqli_fetch_assoc($res);
}
?>

Cannot generate the report

I want to generate the report that click and choose from the category drop down list, then it will show that the order which product in the category that you select and how many of them sold. But it cannot come out, I have been find many times the errors, and I also tried the query in SQL as it showed correct. I don't know why. Please help me.
<?php
session_start();
include_once 'header_admin.php';
?>
<div class="report">
<h2>Sales Report</h2>
<div class="sales_report">
<form action="" method="POST">
<select name="category" class="category">
<option>Category</option>
<?php
include_once 'includes/dbh.php';
$query1 = "SELECT * from category";
$result1 = mysqli_query($conn, $query1);
while ($row1 = mysqli_fetch_assoc($result1)) {
$CategoryID = $row1['CategoryID'];
$rowData1 = $row1['CategoryName'];
?>
<option value="<?php echo $CategoryID; ?>"><?php echo $rowData1; ?></option>
<?php
}
?>
</select>
<button class="generate" type="submit" name="generate">Generate</button>
<table class="genreport">
<tr>
<th>Furniture ID</th>
<th>Furniture Name</th>
<th>Category Name</th>
<th>Order Date</th>
<th>Quantity</th>
<th>Total Amount</th>
</tr>
<?php
if (isset($_POST['generate'])) {
$catName = mysqli_real_escape_string($conn, $_POST['category']);
$query2 = "SELECT D.FurCode, F.FurName, C.CategoryName, O.OrderDate, D.quantity, COUNT(D.quantity * D.PriceEach)
FROM orderdetail D, furniture F, category C, ordertab O
WHERE C.CategoryName = '$catName' GROUP BY C.$catName ORDER BY C.$catName ";
$result2 = mysqli_query($conn, $query2);
if ($result2) {
while ($row2 = mysqli_fetch_array($result2)) {
$furCode = $row2['FurCode'];
$furName = $row2['FurName'];
$orderDate = $row2['OrderDate'];
$quantity = $row2['quantity'];
$priceEach = $row2['PriceEach'];
$total = $quantity * $priceEach;
?>
<tr>
<td><?php echo $furCode; ?></td>
<td><?php echo $furName; ?></td>
<td><?php echo $catName; ?></td>
<td><?php echo $orderDate; ?></td>
<td><?php echo $quantity; ?></td>
<td><?php echo $total; ?></td>
</tr>
<?php
}
}
} else {
echo '<tr><td>No Result!</td><td></td><td></td><td></td><td></td><td></td></tr>';
}
?>
</table>
</form>
</div>
</div>
</div>
</section>
</body>
</html>

Remove button after being clicked

I have a page where the users can view their leave applications, I also have a Cancel button beside it which looks like this:
View Page
What I wanted to happen is that when I click on the cancel button, the status would change and the button would disappear.
I added a script that would make the button disappear and it worked, but it won't update the status.
So here's my form:
<div class="container">
<div class="page-header">
<h3>My Leaves</h3>
<div class="table-responsive">
<table class="table">
<tr>
<th>Employee Name</th>
<th>Phone</th>
<th>Email</th>
<th>From</th>
<th>To</th>
<th>Reason</th>
<th>Type</th>
<th>Status</th>
</tr>
<?php
include ('database.php');
$result = $database->prepare ("SELECT leaves.* FROM leaves INNER JOIN employee ON employee.id = leaves.user_id WHERE employee.username = '".$_SESSION["VALID_USER_ID"]."'");
$result ->execute();
for ($count=0; $row_message = $result ->fetch(); $count++){
?>
<tr>
<td><?php echo $row_message['firstname']." " .$row_message['lastname']; ?></td>
<td><?php echo $row_message['phone']; ?></td>
<td><?php echo $row_message['email']; ?></td>
<td><?php echo $row_message['fromdate']; ?></td>
<td><?php echo $row_message['todate']; ?></td>
<td><?php echo $row_message['reason']; ?></td>
<td><?php echo $row_message['type']; ?></td>
<td><?php echo $row_message['status']; ?></td>
<td>
<form method="post" action="update-leave-status-emp.php">
<input type="hidden" name="leaveno" value="<?php echo $row_message['leaveno']; ?>" />
<input type="submit" value="Cancelled" class="removebtn" name="cancelled"></input>
</form>
</td>
</tr>
<?php } ?>
</table>
<button type="button" class="btn btn-primary"><i class="glyphicon glyphicon-arrow-left"></i> Back</button>
</div>
</div>
</div>
</div>
</div>
And here's the script below:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$(".removebtn").click(function(){
$(this).remove();
});
});
</script>
PHP Code:
<?php
if(isset($_POST['cancelled']))
{
$msg = "Cancelled";
$status=$_POST['cancelled'];
}
$leaveno=$_POST['leaveno'];
$con = mysqli_connect('localhost', 'root', '');
mysqli_select_db($con, 'companydb');
$sql = "UPDATE leaves SET status = '$status' WHERE leaveno = '$leaveno'";
if(mysqli_query($con, $sql))
header("refresh:1; url=view-leave-emp.php?msg=$msg");
else
var_dump(mysqli_error($con));
?>

Removing button after updating database values

I have a page where users can view the leave applications they submitted and they can also cancel the application, here's what it looks like:
I wanted to get rid of the cancel button after it was cancelled or after it was clicked. So I added a remove button script in the execution code. Apparently, it was wrong. Everything worked fine until I added the script in the execution code.
Here's my form:
HTML+PHP
<div class="container">
<div class="page-header">
<h3>My Leaves</h3>
<div class="table-responsive">
<table class="table">
<tr>
<th>Employee Name</th>
<th>Phone</th>
<th>Email</th>
<th>From</th>
<th>To</th>
<th>Reason</th>
<th>Type</th>
<th>Status</th>
</tr>
<?php
include ('database.php');
$result = $database->prepare ("SELECT leaves.* FROM leaves INNER JOIN employee ON employee.id = leaves.user_id WHERE employee.username = '".$_SESSION["VALID_USER_ID"]."'");
$result ->execute();
for ($count=0; $row_message = $result ->fetch(); $count++){
?>
<tr>
<td><?php echo $row_message['firstname']." " .$row_message['lastname']; ?></td>
<td><?php echo $row_message['phone']; ?></td>
<td><?php echo $row_message['email']; ?></td>
<td><?php echo $row_message['fromdate']; ?></td>
<td><?php echo $row_message['todate']; ?></td>
<td><?php echo $row_message['reason']; ?></td>
<td><?php echo $row_message['type']; ?></td>
<td><?php echo $row_message['status']; ?></td>
<td>
<form method="post" action="update-leave-status-emp.php">
<input type="hidden" name="leaveno" value="<?php echo $row_message['leaveno']; ?>" />
<input type="submit" value="Cancelled" class="removebtn" name="cancelled"></input>
</form>
</td>
</tr>
<?php }?>
</table>
<a href="employee_panel.php">
<button type="button" class="btn btn-primary"><i class="glyphicon glyphicon-arrow-left"></i> Back</button>
</a>
</div>
</div>
</div>
Execution Code:
<?php
if(isset($_POST['cancelled'])){
$msg = "Cancelled";
$status=$_POST['cancelled'];
echo "<script>
$(document).ready(function(){
$(".removebtn").click(function(){
$(this).remove();
});
});
</script>";
}
$leaveno=$_POST['leaveno'];
$con = mysqli_connect('localhost', 'root', '');
mysqli_select_db($con, 'companydb');
$sql = "UPDATE leaves SET status = '$status' WHERE leaveno = '$leaveno'";
if(mysqli_query($con, $sql))header("refresh:1; url=view-leave-emp.php?msg=$msg");
else var_dump(mysqli_error($con));
?>
PS: I know my codes are vulnerable to injections, I will change it eventually, for now, I have to figure this out.
Enclosed cancelled button in an if statement that if the record is marked cancelled the button won't be shown anymore.
You can try this updated code:
<div class="container">
<div class="page-header">
<h3>My Leaves</h3>
<div class="table-responsive">
<table class="table">
<tr>
<th>Employee Name</th>
<th>Phone</th>
<th>Email</th>
<th>From</th>
<th>To</th>
<th>Reason</th>
<th>Type</th>
<th>Status</th>
</tr>
<?php
include ('database.php');
$result = $database->prepare ("SELECT leaves.* FROM leaves INNER JOIN employee ON employee.id = leaves.user_id WHERE employee.username = '".$_SESSION["VALID_USER_ID"]."'");
$result ->execute();
for ($count=0; $row_message = $result ->fetch(); $count++){
?>
<tr>
<td><?php echo $row_message['firstname']." " .$row_message['lastname']; ?></td>
<td><?php echo $row_message['phone']; ?></td>
<td><?php echo $row_message['email']; ?></td>
<td><?php echo $row_message['fromdate']; ?></td>
<td><?php echo $row_message['todate']; ?></td>
<td><?php echo $row_message['reason']; ?></td>
<td><?php echo $row_message['type']; ?></td>
<td><?php echo $row_message['status']; ?></td>
<td>
<form method="post" action="update-leave-status-emp.php">
<input type="hidden" name="leaveno" value="<?php echo $row_message['leaveno']; ?>" />
<?php if( $row_message['status'] != "Cancelled" ) {
<input type="submit" value="Cancelled" class="removebtn" name="cancelled"></input>
<?php } ?>
</form>
</td>
</tr>
<?php }?>
</table>
<a href="employee_panel.php">
<button type="button" class="btn btn-primary"><i class="glyphicon glyphicon-arrow-left"></i> Back</button>
</a>
</div>
</div>
</div>

Trying to update column values by submit button

I'm trying to update status on my leaves table by pressing the approve/reject button. Here's my table schema:
So far this is what i've done:
<?php
if(isset($_POST['approved']))
{
echo "Approved";
$status=$_POST['approved'];
}
if(isset($_POST['rejected']))
{
echo "Rejected";
$status=$_POST['rejected'];
}
?>
<!-- Begin page content -->
<div class="container">
<div class="page-header">
<h3>Employee Leaves</h3>
<div class="table-responsive">
<table class="table">
<tr>
<th>Employee Name</th>
<th>Phone</th>
<th>Email</th>
<th>From</th>
<th>To</th>
<th>Reason</th>
<th>Status</th>
<th>---</th>
</tr>
<?php
include ('database.php');
$result = $database->prepare ("SELECT * FROM leaves order by id DESC");
$result ->execute();
for ($count=0; $row_message = $result ->fetch(); $count++){
?>
<tr>
<td><?php echo $row_message['full_name']; ?></td>
<td><?php echo $row_message['phone']; ?></td>
<td><?php echo $row_message['email']; ?></td>
<td><?php echo $row_message['fromdate']; ?></td>
<td><?php echo $row_message['todate']; ?></td>
<td><?php echo $row_message['reason']; ?></td>
<td><?php echo $row_message['status']; ?></td>
<td>
<form method="post" action="update.php">
<input type="hidden" value="<?php echo $row_message['id']; ?>" />
<input type="submit" value="Approved" name="approved"></input>
<input type="submit" value="Rejected" name="rejected"></input>
</form>
</td>
</tr>
<?php } ?>
</table>
<button type="button" class="btn btn-primary"><i class="glyphicon glyphicon-arrow-left"></i> Back</button>
</div>
</div>
</div>
</div>
</div>
and here's my update.php
<?php
$con = mysqli_connect('localhost', 'root', '');
mysqli_select_db($con, 'leaves');
$sql = "UPDATE leaves SET status = :status WHERE id = :id";
if(mysqli_query($con, $sql))
header("refresh:1; url=view-leave.php");
else
echo "Error";
?>
I'm getting the "error" when I click on approve/reject. I think the problem is with the update.php. I'm not sure what it is tho or where.
Try this : update.php
<?php
if(isset($_POST['approved']))
{
$msg = "Approved";
$status=$_POST['approved'];
}
if(isset($_POST['rejected']))
{
$msg = "Rejected";
$status=$_POST['rejected'];
}
$id=$_POST['id'];
$con = mysqli_connect('localhost', 'root', '');
mysqli_select_db($con, 'leaves');
$sql = "UPDATE newusers SET username = '$status' WHERE id = '$id'";
if(mysqli_query($con, $sql))
header("refresh:1; url=index.php?msg=$msg");
else
var_dump(mysqli_error($con));
?>
and view-leave.php
<?php
if(isset($_GET['msg']))
{
echo $_GET['msg'];
}
?>
<!-- Begin page content -->
<div class="container">
<div class="page-header">
<h3>Employee Leaves</h3>
<div class="table-responsive">
<table class="table">
<tr>
<th>Employee Name</th>
<th>Phone</th>
<th>Email</th>
<th>From</th>
<th>To</th>
<th>Reason</th>
<th>Status</th>
<th>---</th>
</tr>
<?php
include ('database.php');
$result = $database->prepare ("SELECT * FROM leaves order by id DESC");
$result ->execute();
for ($count=0; $row_message = $result ->fetch(); $count++){
?>
<tr>
<td><?php echo $row_message['full_name']; ?></td>
<td><?php echo $row_message['phone']; ?></td>
<td><?php echo $row_message['email']; ?></td>
<td><?php echo $row_message['fromdate']; ?></td>
<td><?php echo $row_message['todate']; ?></td>
<td><?php echo $row_message['reason']; ?></td>
<td><?php echo $row_message['status']; ?></td>
<td>
<form method="post" action="update.php">
<input type="hidden" name="id" value="<?php echo $row_message['id']; ?>" />
<input type="submit" value="Approved" name="approved"></input>
<input type="submit" value="Rejected" name="rejected"></input>
</form>
</td>
</tr>
<?php } ?>
</table>
<button type="button" class="btn btn-primary"><i class="glyphicon glyphicon-arrow-left"></i> Back</button>
</div>
</div>
</div>
</div>
</div>
You have to write database name here:
mysqli_select_db($con, 'dbname');
Did you try to declare a default value for the $msg=''; and $status = '';?

Categories