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

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

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>

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

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.

Pagination in while loop with php

I'm newbee about php,trying to learn.
I've search other topic about while loop pagination but not satisfied.
I have 70 user records in my database,wants to list them with html table. I'm showing all records with this code. How can i make simple pagination with these codes? Please help me.
<table class="table">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Surname</th>
<th>Email</th>
<th>Password</th>
<th>Date</th>
<th>Gender</th>
</tr>
</thead>
<tbody>
<?php
$q = "SELECT * FROM users ORDER BY uid ASC";
$r = mysqli_query($dbc,$q);
while($userlist = mysqli_fetch_assoc($r)){ ?>
<tr>
<td><?php echo $userlist['uid']; ?></td>
<td><?php echo $userlist['name']; ?></td>
<td><?php echo $userlist['surname']; ?></td>
<td><?php echo $userlist['email']; ?></td>
<td><?php echo $userlist['password']; ?></td>
<td><?php echo $userlist['date']; ?></td>
<td><?php echo $userlist['gender']; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
The follow provide very simple pagination as a starting point. You will need to supply formatting for the pagination and such.
<table class="table">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Surname</th>
<th>Email</th>
<th>Password</th>
<th>Date</th>
<th>Gender</th>
</tr>
</thead>
<tbody>
<?php
$q = "SELECT count(*) as `numrows` FROM `users` ORDER BY `uid` ASC";
$c = mysqli_query($dbc,$q);
if($c) {
if($t = mysqli_fetch_assoc($c)) {
$numrows = $t['numrows'];
}
}
$numrows = 0;
$rowsperpage = 10;
$currpage = isset($_REQUEST['currpageno']) && $_REQUEST['currpageno'] != 0 ? $_REQUEST['currpageno'] : 1;
$numpages = ceil($numrows / $rowsperpage);
$startrow = ($currpage - 1) * $rowsperpage;
if($startrow > $numrows) {
$startrow = $numrows - $rowsperpage;
}
if($startrow < 0) {
$startrow = 0;
}
$q = "SELECT * FROM `users` ORDER BY `uid` ASC LIMIT ".$startrow.",".$rowsperpage.";";
$r = mysqli_query($dbc,$q);
while($userlist = mysqli_fetch_assoc($r)){
?>
<tr>
<td><?php echo $userlist['uid']; ?></td>
<td><?php echo $userlist['name']; ?></td>
<td><?php echo $userlist['surname']; ?></td>
<td><?php echo $userlist['email']; ?></td>
<td><?php echo $userlist['password']; ?></td>
<td><?php echo $userlist['date']; ?></td>
<td><?php echo $userlist['gender']; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
<div id='pagination'>
<?php
for($pgno = 1;$pgno <= $numpages;$pgno++) {
echo "<a class='' href='?currpageno=".$pgno."'>".$pgno."</a>";
}
?>
</div>

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>

Categories