trying to get id from table row to perform update - php

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

Related

date filter not wroking for mysql in php

Table name :- add_user
data is inserting in it, there is a column due_date storing date in (dd-mm-yyyy) format
now i want to create a filter for due_date. But my code is not working. When i'm running the code it is showing all data, it is not filtering it.
here is my code
<form class="form-inline" method="post" action="">
<label> <b>From Date :  </b></label>
<input type="date" name="s_date" class="form-control"> 
<input type="date" name="e_date" class="form-control">
<button type="submit" name="search" class="btn btn-success">Search</button>
</form>
<?php
error_reporting(0);
include_once "config2.php";
if(count($_POST)>0) {
$s_date=date("dd-mm-yyyy", strtotime($_POST['s_date']));
$e_date=date("dd-mm-yyyy", strtotime($_POST['e_date']));
$result = mysqli_query($conn,"SELECT * FROM add_user WHERE due_date BETWEEN '$s_date' AND '$e_date'");
}
?>
<center>
<table class='table table-bordered'>
<thead class="thead-dark">
<tr>
<th>Action</th>
<th>Pet Photo</th>
<th>Owner</th>
<th>Mobile Number</th>
<th>Category</th>
<th>Pet Id</th>
<th>Pet Name</th>
<th>Address</th>
</tr>
<?php
$i=0;
while($row = mysqli_fetch_array($result)) {
?>
<tr>
<td><button class="btn btn-info">View</button></td>
<td><img src="/pet_img/<?php echo $row["signature"]; ?>"width="60px"height="80px"> </td>
<td><img src="/pet_img/<?php echo $row["photo"]; ?>"width="60px"height="80px"> <br><b> <?php echo $row["name"]; ?> </td>
<td><?php echo $row["mobile"]; ?></td>
<td><?php echo $row["type"]; ?></td>
<td><b><font color="green"><?php echo $row["pet_id"]; ?></font></b></td>
<td><?php echo $row["pet_name"]; ?></td>
<td><?php echo $row["add1"]; ?> <?php echo $row["add2"]; ?> <?php echo $row["add3"]; ?> <?php echo $row["pin"]; ?></td>
</tr>
<?php
$i++;
}
?>
</table>
</body>
</html>
Your column data type needs to be either timestamp or date/datetime. You can't compare date strings in this format.

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

Call all posts from Wordpress database with category names

Our Wordpress site are infected and we are deleted all files. we have now only database and want to extract all information from db and afther this copy on other site.
so I am using this php class to connect db and call some information. I am calling now posts with data, but need categories also.
For example:
Post Title, Post Content, Post Category
here is my working code without category names
$posts = $db->get('c3624322676f_wp_posts');
but I have no idea how to call categories names here.
This is my html + foreach
<table class="table table-hover">
<thead class="thead-inverse">
<tr>
<th>#</th>
<th>Title</th>
<th>Type</th>
<th>Content</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php foreach ($posts as $post) : ?>
<tr>
<th scope="row"><?php echo $count++; ?><a href=""></th>
<td><?php echo $post['post_title']; ?></td>
<td><?php echo $post['post_type']; ?></td>
<td><?php echo strip_tags ($post['post_content']); ?></td>
<td>
<form action="" method="post">
<input type="hidden" name="id" value="<?php echo $post['ID']; ?>" />
<input style="cursor:pointer;" type="submit" class="btn btn-primary" value="Delete" />
</form>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
This should work
<table class="table table-hover">
<thead class="thead-inverse">
<tr>
<th>#</th>
<th>Title</th>
<th>Type</th>
<th>Content</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php foreach ($posts as $post) : ?>
<tr>
<th scope="row"><?php echo $count++; ?><a href=""></th>
<td><?php echo $post['post_title']; ?></td>
<td><?php echo $post['post_type']; ?></td>
<td><?php echo strip_tags ($post['post_content']); ?></td>
<?php
$separator = ',';
$output = '';
$categories = get_the_category($post['post_title']);
if ($categories){
foreach($categories as $category) {
$output .= ''.$category->cat_name.''.$separator;
}
echo trim($output, $separator);
}
?>
<td>
<form action="" method="post">
<input type="hidden" name="id" value="<?php echo $post['ID']; ?>" />
<input style="cursor:pointer;" type="submit" class="btn btn-primary" value="Delete" />
</form>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>

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 :)

Categories