Trying to update column values by submit button - php

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 = '';?

Related

adding values to a different column after button click

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'";

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>

Select Query incomplete

I have 17 records inserted in my table "book".
<?php
include 'config.php';
session_start();
$sql = "SELECT * from book";
$result = mysqli_query($db, $sql);
$count = mysqli_num_rows($result);
if($count == 0){
$error = "There is no books in the list";
}
?>
<html>
<body>
<table border="2" style="width:80%;" align="center">
<tr>
<th>ISBN</th>
<th>Title of the book</th>
<th>Cost</th>
<th>Copies</th>
<th>Edition</th>
<th>Publisher</th>
<th>Copy Year</th>
<th>Shelf Number</th>
<th>Subject</th>
</tr>
<?php while($row = mysqli_fetch_array($result)){
$ISBN = $row['ISBN'];
$Title = $row['Title'];
$Cost = $row['Cost'];
$Copies = $row['Copies'];
$Edition = $row['Edition'];
$Publisher = $row['Publisher'];
$CopyYr = $row['CopyYr'];
$Shelf = $row['ShelfNo'];
$Subject = $row['SubName'];
?>
<tr>
<td><?php echo $ISBN; ?></td>
<td><?php echo $Title; ?></td>
<td><?php echo $Cost; ?></td>
<td><?php echo $Copies; ?></td>
<td><?php echo $Edition; ?></td>
<td><?php echo $Publisher; ?></td>
<td><?php echo $CopyYr; ?></td>
<td><?php echo $Shelf; ?></td>
<td><?php echo $Subject; ?></td>
</tr>
<?php
}
?>
</table>
<br>
<table align="center" style="width: 30%">
<tr>
<th><form action="AddBook.php" method="post">
<input type="submit" value="Add Book"/>
</form></th>
<th><form action="editbook.php" method="post">
<input type="submit" value="Edit List"/>
</form></th>
<th><form action="deletebook.php" method="post">
<input type="submit" value="Delete Book/s"/>
</form></th>
<th><form action="AdminSummary.php" method="post">
<input type="submit" value="Back"/>
</form></th>
</tr>
</table>
</center>
</body>
</html>
This will display 17 records in the table. But when I add another record to the table, It will display an incomplete record, a "500 server error", or an empty table.
I have been in the same problem for days, searching for a solution, and I can't find any.
I don't know what the problem is, so any help would be appreciated. Thank You :)

trying to get id from table row to perform update

I am trying to get the id from each row in the table so that i'll be able to update each record in a form on a different page. I've been trying to, however, i'm only getting the first record from the table. Could someone provide some assistance please.
<div class="personnel_title">Personnel<br><hr>
<div class="addcontain">
<form method="post" action="">
<input type="button" name="addpersonnel" value="Add">
</form>
<div class="searchcontain">
<form method="post" action="HR_core_personnel.php">
<input type="text" id="search" name="search" placeholder="Search" /><br>
</form>
<div id="output">
</div>
</div>
</div>
<hr>
<?php
$query = "SELECT `fighterID`, `firstName`, `middleName`, `lastName`, `rank`.`rank`, `status`, `telephone1`, `telephone2`, `stationlocation`.`exactlocation`, `hireDate`, `workShift`
FROM `firefighterinfo`
JOIN `rank` ON `firefighterinfo`.`Rank_rankID` = `rank`.`rankID`
JOIN `stationlocation` ON `firefighterinfo`.`StationLocation_locationID` = `stationlocation`.`locationID`";
$result = mysql_query($query);
$num = mysql_num_rows($result);
mysql_close();
?>
<div class="results_table">
<table>
<thead>
<tr>
<th>Firefighter</th>
<th>Rank</th>
<th>Status</th>
<th>Home No.</th>
<th>Cell No.</th>
<th>Station</th>
<th>Hire Date</th>
<th>Shift</th>
<th></th>
</tr>
</thead>
<?php
$i = 0;
while($i < $num){
$f1 = mysql_result($result, $i, 'firstName');
$f2 = mysql_result($result, $i, 'middleName');
$f3 = mysql_result($result, $i, 'lastName');
$f4 = mysql_result($result, $i, 'rank');
$f5 = mysql_result($result, $i, 'status');
$f6 = mysql_result($result, $i, 'telephone1');
$f7 = mysql_result($result, $i, 'telephone2');
$f8 = mysql_result($result, $i, 'exactlocation');
$f9 = mysql_result($result, $i, 'hireDate');
$f10 = mysql_result($result, $i, 'workShift');
?>
<tbody id="table_body">
<tr>
<td>
<?php echo $f1; ?>
<?php echo $f2; ?>
<?php echo $f3; ?>
</td>
<td>
<?php echo $f4; ?>
</td>
<td>
<?php echo $f5; ?>
</td>
<td>
<?php echo $f6; ?>
</td>
<td>
<?php echo $f7; ?>
</td>
<td>
<?php echo $f8; ?>
</td>
<td>
<?php echo $f9; ?>
</td>
<td>
<?php echo $f10; ?>
</td>
<td>
<img src="images/Awicons-Vista-Artistic-Edit.ico" width="15" height="20" alt="Edit">
</td>
</tr>
</tbody>
<?php
$i++;
}
?>
</table>
</div>
</div>
#Farman left you a good answer, here's mine with some additions
reformated for my eyes
changed those ugly calls to mysql_result to something with better performance
assumes fighterID as the rightfull key
this is your script
<div class="personnel_title">Personnel<br><hr>
<div class="addcontain">
<form method="post" action="">
<input type="button" name="addpersonnel" value="Add">
</form>
<div class="searchcontain">
<form method="post" action="HR_core_personnel.php">
<input type="text" id="search" name="search" placeholder="Search" /><br>
</form>
<div id="output"></div>
</div>
</div>
<hr>
<?php
$query = "SELECT `fighterID`, `firstName`, `middleName`, `lastName`,
`rank`.`rank`, `status`, `telephone1`, `telephone2`,
`stationlocation`.`exactlocation`, `hireDate`, `workShift`
FROM `firefighterinfo`
JOIN `rank` ON `firefighterinfo`.`Rank_rankID` = `rank`.`rankID`
JOIN `stationlocation` ON `firefighterinfo`.`StationLocation_locationID` = `stationlocation`.`locationID`";
$result = mysql_query($query);
$num = mysql_num_rows($result);
mysql_close();
?>
<div class="results_table">
<table>
<thead>
<tr>
<th>Firefighter</th>
<th>Rank</th>
<th>Status</th>
<th>Home No.</th>
<th>Cell No.</th>
<th>Station</th>
<th>Hire Date</th>
<th>Shift</th>
<th> </th>
</tr>
</thead>
<?php
$c = array('fighterID','firstName','middleName','lastName','rank','status','telephone1','telephone2','exactlocation','hireDate','workShift');
while($r = mysql_fetch_assoc($result)) { ?>
<tbody id="table_body">
<tr>
<td><?php echo $r[$c[1]].' '.$r[$c[2]].' '.$r[$c[3]]; ?></td>
<td><?php echo $r[$c[4]]; ?></td>
<td><?php echo $r[$c[5]]; ?></td>
<td><?php echo $r[$c[6]]; ?></td>
<td><?php echo $r[$c[7]]; ?></td>
<td><?php echo $r[$c[8]]; ?></td>
<td><?php echo $r[$c[9]]; ?></td>
<td><?php echo $r[$c[10]]; ?></td>
<td>
<!-- the change made by #Farman Ullah is here -->
<a href="HR_core_updatepersonnel.php?fighterID=<?php echo $r[$c[0]]; ?>">
<img src="images/Awicons-Vista-Artistic-Edit.ico" width="15" height="20" alt="Edit">
</a>
</td>
</tr>
</tbody>
<?php } ?>
</table>
</div>
</div>
Feel free to ask any questions or add feedback

Categories