How to fetch mysql data when I click side bar menu link - php

I have a sidebar menu with links "approved users, pending users, and rejected users".
I need to implement this: when I click on approved users link in sidebar, the approved_users.php page should load with all users who are approved.
Similarly, I want to load pending users and need to approve or reject them using update query.
<?php
if(isset($_GET['approved'])){ ?>
<table>
<thead>
<tr>
<th>ID</th>
<th>Full Name</th>
<th>Phone</th>
<th>Email</th>
<th>Username</th>
<th>Password</th>
<th>Address</th>
<th>Role</th>
</tr>
</thead>
<?php
$query = "SELECT * FROM users WHERE status = 1 AND role != 'admin' ";
$resul = $db->query($query);
$num_row = $resul->num_rows;
if($num_row == 0){
echo "<tr><td class='info'>No record found.</td></tr>";
}else{
$count = 0;
while($row = $resul->fetch_assoc()){
$id = $row['id'];
$fullname = $row['fullname'];
$username = $row['username'];
$email = $row['email'];
$phone = $row['phone'];
$password = $row['password'];
$address = $row['address'];
$role = $row['row'];
$count++;
?>
<tr>
<td><?php echo $count ?></td>
<td><?php echo $fullname ?></td>
<td><?php echo $phone ?></td>
<td><?php echo $email ?></td>
<td><?php echo $username ?></td>
<td><?php echo $password ?></td>
<td><?php echo $address ?></td>
<td><span><?php echo $role ?></span></td>
</tr>
<?php
} // end of while
} // else end
} // Approved - if
}
?>
</table>

The tbody tag is missing which is causing the problem. Please add the tbody tag.

Related

How to insert a single user from a table of all users into a table in MySQL in PHP

I have a table of all registered users from my users table from my database. Now I have an accept button, when clicked I want to add that specific user to a new table called accepted_applicants.
But when I try to do this it adds all the users into the database. How can I only add the specific row that I click?
What am I doing wrong?
Here is my code:
//users query
$query = $conn->query("SELECT users.id AS userid, users.status AS status, users.name AS username, users.dob AS dob, users.gender AS g, users.country AS country, users.addedDate AS created_date, users.categoryId AS catID ,
category.name AS awardname, country.id AS countryID, country.country_name AS countryName FROM users LEFT JOIN category ON users.categoryId = category.id LEFT JOIN country ON country.id = users.country;");
<table class="table table-head-fixed text-nowrap">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Gender</th>
<th>Date of Birth</th>
<th>Country</th>
<th>Award Category</th>
<th>Date Created</th>
<th>Application Status</th>
<th>Answers</th>
<th>Accept</th>
<th>Reject</th>
</tr>
</thead>
<tbody>
<?php
while ($row = $query->fetch()) {
$userid = $row['userid'];
$username = $row['username'];
$gender = $row['g'];
$dob = $row['dob'];
$date = $row['created_date'];
$status = $row['status'];
$countryName = $row['countryName'];
$awardCatName = $row['awardname'];
if(isset($_POST['accept'])){
$insertq = $conn->prepare("INSERT INTO accepted_applicants (user_id,name,gender,dob,country,award_cat,status) VALUES (?,?,?,?,?,?,?)");
$insertq->bindValue(1,$userid);
$insertq->bindValue(2,$username);
$insertq->bindValue(3,$gender);
$insertq->bindValue(4,$dob);
$insertq->bindValue(5,$countryName);
$insertq->bindValue(6,$awardCatName);
$insertq->bindValue(7,$status);
$insertq->execute();
}
?>
<tr>
<td><?php echo $row['userid'] ?></td>
<td><?php echo $row['username'] ?></td>
<td><?php echo $row['g'] ?></td>
<td><?php echo $row['dob'] ?></td>
<td><?php echo $countryName ?></td>
<td><?php echo $awardCatName ?></td>
<td><?php echo $row['created_date'] ?></td>
<td><?php if($row['status'] == 1){
echo "Submitted";
}else{
echo "Not Submitted";
} ?></td>
<td><button>Answers</button></td>
<td><input type="submit" name="accept" value="Accept"/></td>
<td><input type="submit" name="reject"/></td>
</tr>
<?php
}
?>
</tbody>
</table>
</form>
i solved it this way:
I added an extra condition to my if statement to check if the userid is equal to the value of the Accept input field.
<form method="post" action="applicant_approval.php" >
<tbody>
<?php
while ($row = $query->fetch()) {
// $userid = $row['userid'];
$username = $row['username'];
$gender = $row['g'];
$dob = $row['dob'];
$date = $row['created_date'];
$status = $row['status'];
$countryName = $row['countryName'];
$awardCatName = $row['awardname'];
if(isset($_POST['accept']) && $_POST['accept']==$row['userid']){
$insertq = $conn->prepare("INSERT INTO accepted_applicants (user_id,name,gender,dob,country,award_cat,status) VALUES (?,?,?,?,?,?,?)" );
$insertq->bindValue(1,$row['userid']);
$insertq->bindValue(2,$username);
$insertq->bindValue(3,$gender);
$insertq->bindValue(4,$dob);
$insertq->bindValue(5,$countryName);
$insertq->bindValue(6,$awardCatName);
$insertq->bindValue(7,$status);
$insertq->execute();
}
?>
<tr>
<td><?php echo $row['userid'] ?></td>
<td><?php echo $row['username'] ?></td>
<td><?php echo $row['g'] ?></td>
<td><?php echo $row['dob'] ?></td>
<td><?php echo $countryName ?></td>
<td><?php echo $awardCatName ?></td>
<td><?php echo $row['created_date'] ?></td>
<td><?php if($row['status'] == 1){
echo "Submitted";
}else{
echo "Not Submitted";
} ?></td>
<td><button>Answers</button></td>
<td><input type="submit" name="accept" value="<?php echo $row['userid']; ?>"/></td>
<td><input type="submit" name="reject"/></td>
</tr>
<?php
}
?>
</tbody>
</table>
</form>

Absolutely confused, how come this particular query is not working?

I do not understand because I can delete comments so far. However, when I attempt to update them, it does not update in the database. What is even more shocking to me is there are no error messages explaining why. Is there a particular reason for this?
<table class="table table-bordered table-hover">
<thead>
<tr>
<td>Id</td>
<td>Post Author</td>
<td>Author Email</td>
<td>Post Content</td>
<td>In Response To</td>
<td>Post Status</td>
<td>Date</td>
<td>Approve</td>
<td>Disapprove</td>
</tr>
</thead>
<tbody>
<?php
if(isset($_GET['delete'])) {
$comId = $_GET['delete'];
$deleteComment = $conn->prepare("
DELETE FROM comments
WHERE comment_id = ?
");
$deleteComment->bind_param("i", $comId);
$deleteComment->execute();
$deleteComment->close();
}
if(isset($_GET['approve'])) {
$comIdApp = $_GET['approve'];
$approveStat = 'approved';
$approveComment = $conn->prepare("
UPDATE comments
SET comment_status = ?
WHERE comment_id = ?
");
$approveComment->bind_param("si", $approveStat, $commIdApp);
$approveComment->execute();
$approveComment->close();
}
?>
<?php
$post_output = $conn->query("SELECT * FROM comments");
while($row = $post_output->fetch_assoc()) {
$commentId = $row['comment_id'];
$postCommentId = $row['post_comment_id'];
$commentAuthor = $row['comment_author'];
$authorEmail = $row['author_email'];
$commentContent = $row['comment_content'];
$commentStatus = $row['comment_status'];
$commentDate = $row['comment_date'];
?>
<tr>
<td><?php echo($commentId); ?></td>
<td><?php echo($commentAuthor); ?></td>
<td><?php echo($authorEmail); ?></td>
<td><?php echo($commentContent); ?></td>
<?php
$postCommentRelate = $conn->query("SELECT * FROM posts WHERE post_id = {$postCommentId}");
while($row = $postCommentRelate->fetch_assoc()) {
$postTitle = $row['post_title'];
?>
<td><?php echo($postTitle); ?></td>
<?php } ?>
<td><?php echo($commentStatus); ?></td>
<td><?php echo($commentDate); ?></td>
<td><a href='admin_comments.php?approve=<?php echo($commentId); ?>'>Approve</a></td>
<td><a href='admin_comments.php?disapprove=<?php echo($commentId); ?>'>Disapprove</a></td>
<td>DELETE</td>
<td><a href=''>EDIT</a></td>
</tr>
<?php } ?>
</tbody>
</table>
Your variables are spelled differently $comIdApp vs $commIdApp
$comIdApp = $_GET['approve'];
$approveStat = 'approved';
$approveComment = $conn->prepare("UPDATE comments SET comment_status = ? WHERE comment_id = ?");
$approveComment->bind_param("si", $approveStat, $commIdApp);

PHP not displaying last column in table

I am trying to display all the info in the table but when I query the information then put it into a PHP table it doesn't show any data in the last table.
Here is my PHP code for the table
<table border='1'>
<tr>
<th>Ticket ID</th>
<th>Username</th>
<th>Message</th>
<th>Date</th>
<th>Error #</th>
<th>Priority</th>
</tr>
<?php
if(!$query){
die('Invalid query: ' .mysql_error());
}
while ($row = mysql_fetch_array($query)) { ?>
<tr>
<td><?php echo $row['TicketID']; ?></td>
<td><?php echo $row['Message']; ?></td>
<td><?php echo $row['Date']; ?></td>
<td><?php echo $row['Error']; ?></td>
<td><?php echo $row['Priority']; ?></td>
</tr>
<?php } ?>
</table>
Here is the PHP code for query
<?php
$server = mysql_connect("localhost", "root", "**password**");
$db = mysql_select_db("minecraft", $server);
$query = mysql_query("SELECT * FROM tickets");
?>
All of my row names are correct but it doesn't want to put the data into that column.
Here is my table structure
You Omitted
<td><?php echo $row['Username']; ?></td>
that should be after
<td><?php echo $row['TicketID']; ?></td>

Query always returns recently added data only

I'm trying to show all my data from the database I made in mysql.
I am using this code:
<table border= "3">
<tr>
<th>ID</th>
<th>Game Name</th>
</tr>
<?php
$query = "SELECT * FROM `test_game_name`";
$result = mysql_query($query);
while($row = mysql_fetch_array($result)) {
$id = $row['game_id'];
$name = $row['game_name'];
}
?>
<tr>
<td><?php echo $id; ?></td>
<td><?php echo $name; ?></td>
</tr>
</table>
My problem is that not all the data show up, only the data I recently added. I believe that SELECT * means selecting all the data.
But I don't know what's the problem why it does not show all the data, anyone would happen to know?
You need to add your td inside your while loop
<?php
$query = "SELECT * FROM `test_game_name`";
$result = mysql_query($query);
?>
<tr>
<?php
while ($row = mysql_fetch_array($result)) {
$id = $row['game_id'];
$name = $row['game_name'];
echo " <td>" . $id . "</td>";
echo " <td>" . $name . "</td>";
}
?>
</tr>
Note:- mysql is deprecated instead use mysqli and PDO
Try using this:
<table border= "3">
<tr>
<th>ID</th>
<th>Game Name</th>
</tr>
<?php
$query = "SELECT * FROM `test_game_name`";
$result = mysql_query($query);
while($row = mysql_fetch_array($result))
{
$id = $row['game_id'];
$name = $row['game_name'];
?>
<tr>
<td><?php echo $id; ?></td>
<td><?php echo $name; ?></td>
</tr>
<?php } ?>
</table>
add this inside while,
<tr>
<td><?php echo $id; ?></td>
<td><?php echo $name; ?></td>
</tr>
Final Code
while($row = mysql_fetch_array($result)) {
$id = $row['game_id'];
$name = $row['game_name']; ?>
<tr>
<td><?php echo $id; ?></td>
<td><?php echo $name; ?></td>
</tr>
<?php } ?>
Switch to mysqli_* or PDO instead of mysql_* which is deprecated.
If you are not connected with database then follow this code it will help you.
<table border= "3">
<tr>
<th>ID</th>
<th>Game Name</th>
</tr>
<?php
$link=mysql_connect("localhost", "root","");
mysql_select_db('dbname', $link);
$query = "SELECT * FROM `test_game_name`";
$result = mysql_query($query);
while($row = mysql_fetch_array($result)) {?>
<tr>
<td><?php echo $row['game_id']; ?></td>
<td><?php echo $row['game_name']; ?></td>
</tr>
<?php }
?>
</table>

Error to view all in data table in php [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
When i run it. i only see 1 records of customer and it says that the error is in " WHILE($cus = mysql_fetch_array($cus)){" Line.. anyone knows how to solve it? ..tnx
<table id="datatables" class="display">
<thead>
<tr>
<th>ID</th>
<th>FullName</th>
<th>Age</th>
<th>Gender</th>
<th>Email</th>
<th>Barangay</th>
<th>CompleteAddress</th>
<th>Username</th>
<th>Password</th>
</tr>
</thead>
<tbody>
<?php $cus = mysql_query("SELECT * FROM customer") or die(mysql_error()); ?>
<?php
WHILE ($cus = mysql_fetch_array($cus)) {
$id = $cus['cus_id'];
$email = $cus['email'];
$control = $cus['cus_id'];
$user = $cus['username'];
$pass = $cus['password'];
$brgy = $cus['barangay'];
$comadd = $cus['com_address'];
$age = $cus['age'];
$gend = $cus['gender'];
$fname = $cus['firstname'] . " " . $cus['middlename'] . " " . $cus['lastname'];
?>
<tr class="gradeA del<?php echo $id; ?>">
<td><?php echo $control; ?></td>
<td><?php echo $fname; ?></td>
<td><?php echo $age; ?></td>
<td><?php echo $gend; ?></td>
<td><?php echo $email; ?></td>
<td><?php echo $brgy; ?></td>
<td><?php echo $comadd; ?></td>
<td><?php echo $user; ?></td>
<td><?php echo $pass; ?></td>
</tr>
<?php
}
?>
</tbody>
</table>
You are missing the closing statement for your while loop. Add this to the bottom
<?php } ?>
Second issue
while($cus = mysql_fetch_array($cus)) { // You are overwriting the recordset
Change the variable name there
while($cusTemp = mysql_fetch_array($cus)) { // or any other name convenient
Please close your missing while loop curly braces.
Complete code:-
<?php
while($rows = mysql_fetch_array($cus)) {
$id = $rows['cus_id'];
$email = $rows['email'];
$control = $rows['cus_id'];
$user = $rows['username'];
$pass = $rows['password'];
$brgy = $rows['barangay'];
$comadd = $rows['com_address'];
$age = $rows['age'];
$gend = $rows['gender'];
$fname = $rows['firstname'] . " " . $rows['middlename']. " " . $rows['lastname'];
?>
<tr class="gradeA del<?php echo $id;?>">
<td><?php echo $control; ?></td>
<td><?php echo $fname; ?></td>
<td><?php echo $age; ?></td>
<td><?php echo $gend; ?></td>
<td><?php echo $email; ?></td>
<td><?php echo $brgy; ?></td>
<td><?php echo $comadd; ?></td>
<td><?php echo $user; ?></td>
<td><?php echo $pass; ?></td>
</tr>
<?php } ?>
you can't use $cus as the result set for the query and the row information. one of these variables needs to be something different. in my comment above, I suggested turning one into row but it's a lot easier to just change the name of the result set, so that is what I did in the code below.
I assume you have the correct connection set up before your pasted code starts, too.
<table id="datatables" class="display">
<thead>
<tr>
<th>ID</th>
<th>FullName</th>
<th>Age</th>
<th>Gender</th>
<th>Email</th>
<th>Barangay</th>
<th>CompleteAddress</th>
<th>Username</th>
<th>Password</th>
</tr>
</thead>
<tbody>
<?php
// this used to be $cus but I renamed it...
$customer_results = mysql_query("SELECT * FROM customer") or die(mysql_error());
// here was the issue... you were using $cus twice.
WHILE ($cus = mysql_fetch_array($customer_results))
{
$id = $cus['cus_id'];
$email = $cus['email'];
$control = $cus['cus_id'];
$user = $cus['username'];
$pass = $cus['password'];
$brgy = $cus['barangay'];
$comadd = $cus['com_address'];
$age = $cus['age'];
$gend = $cus['gender'];
$fname = $cus['firstname'] . " " . $cus['middlename'] . " " . $cus['lastname'];
?>
<tr class="gradeA del<?php echo $id; ?>">
<td><?php echo $control; ?></td>
<td><?php echo $fname; ?></td>
<td><?php echo $age; ?></td>
<td><?php echo $gend; ?></td>
<td><?php echo $email; ?></td>
<td><?php echo $brgy; ?></td>
<td><?php echo $comadd; ?></td>
<td><?php echo $user; ?></td>
<td><?php echo $pass; ?></td>
</tr>
<?php
}
?>
</tbody>
</table>
Keep in mind, mysql functions are old and will be removed from PHP. You should not waste your time learning them. Instead use PDO or MYSQLI. Personally, I think PDO is easier. If you decide to switch over to one of those two new DB classes, be sure to parameterize your queries and bind values to help prevent SQL injection.
You get from $cus, and assign it to $cus as well. This means you will see 1 record, and afterwards it fails on the next fetch since you changed the context:
while($cusdata = mysql_fetch_assoc($cus)){
Would be better :)
What about this code:
<?php while (($row = mysql_fetch_array($cus)) !== false) { ?>
<tr>
<tr class="gradeA del<?php echo $row['cus_id'];?>">
<td><?php echo $row['cus_id']; ?></td>
<td><?php echo $row['firstname'] . " " . $row['middlename']. " " . $row['lastname']; ?></td>
<td><?php echo $row['age']; ?></td>
<td><?php echo $row['gender']; ?></td>
<td><?php echo $row['email']; ?></td>
<td><?php echo $row['barangay']; ?></td>
<td><?php echo $row['com_address']; ?></td>
<td><?php echo $row['username']; ?></td>
<td><?php echo $row['password']; ?></td>
</tr>
<?php } ?>
The code is clean, condition is properly checked against FALSE and no need for useless lines of code...

Categories