PHP isn't updating values in an SQL row - php

I made a comment page to display comments that hadn't been approved by any Admins yet. On this page there is a button where you click approve to goto a seperate PHP file that will approve the comment you selected. The comment isn't being approved in my DB and the $SearchQueryParameter isn't even being displayed in my toast. It does however give me a success toast.
This is the approval file.
<!-- check DB File-->
<?php require_once("includes/DB.php"); ?>
<!-- check functions/errors File-->
<?php require_once("includes/functions.php"); ?>
<!-- check sessions File-->
<?php require_once("includes/sessions.php"); ?>
<?php
if(isset($_GET["id"])&&!empty($_GET["id"])){
$SearchQueryParameter = $_Get["id"];
global $ConnectingDB;
$Admin = $_SESSION["Username"];
$sql = "UPDATE comments
SET approvedby='$Admin', cmntstatus='ON'
WHERE id='$SearchQueryParameter'";
$Execute = $ConnectingDB->query($sql);
if ($Execute){
$_SESSION["SuccessMessage"]="Comment ".$SearchQueryParameter." Approved Successfully";
Redirect_to("Comments.php");
}else {
$_SESSION["ErrorMessage"]="There was an error!";
Redirect_to("Comments.php");
}
}
?>
Here is the comment file, It seems to be working fine as a whole. There is more but I think it's irrelevant.
<table class="table table-bordered table-hover">
<thead class="table-dark">
<tr>
<th>#</th>
<th>Date&Time</th>
<th>Author</th>
<th>Comments</th>
<th>Approve</th>
<th>Delete</th>
<th>ID</th>
</tr>
</thead>
<?php
global $ConnectingDB;
$sql = "SELECT * FROM comments WHERE cmntstatus='OFF'
ORDER BY id desc";
$stmt = $ConnectingDB->query($sql);
$Sr = 0;
while ($DataRows = $stmt->fetch()){
$CmntId = $DataRows["id"];
$DateTime = $DataRows["datetime"];
$CmntName = $DataRows["name"];
$Cmnt = $DataRows["comment"];
$Sr++;
if(strlen($CmntName)>10) {
$CmntName = substr($CmntName,0,10)."...";
}
if(strlen($DateTime)>10){
$DateTime = substr($DateTime,0,10)."...";
}
?>
<tbody>
<tr>
<td><?php echo htmlentities($Sr);?></td>
<td><?php echo htmlentities($DateTime); ?></td>
<td><?php echo htmlentities($CmntName); ?></td>
<td><?php echo htmlentities($Cmnt); ?></td>
<td>
Approve
</td>
<td>
Delete
</td>
<td>
<?php echo htmlentities($CmntId); ?>
</td>
</tr>
</tbody>
<?php
}?>

I’m not sure but I seem to recall that the superglobals in PHP are case-sensitive. Have you tried using $_GET["id"] instead of $_Get["id"]? –
rickdenhaan
6 mins ago
^ That fixed it ^

Related

how do i select a single user transaction log from the database, it should fetch only the logged in transaction log

how do i select a single user transaction log from the database, it should fetch only the logged in transaction log
i tried with this line of code but it is fetching all the user transaction instead of the current logged in user. what can i do please
<?php
$sql = "SELECT * FROM admin_dept_table";
$query_run = mysqli_query($conn, $sql);
?>
<table class="display table" id="example">
<thead>
<tr class="info">
<th>Amount(USD)</th>
<th>Status</th>
<th>Tnx ID</th>
<th>Date</th>
</tr>
</thead>
<tbody>
<?php
if(mysqli_num_rows($query_run) > 0)
{
while($row = mysqli_fetch_assoc($query_run)){
?>
<tr>
<td> <?php echo $row['amount']; ?></td>
<td> <?php echo $row['status']; ?></td>
<td> <?php echo $row['transactionid']; ?></td>
<td> <?php echo $row['date']; ?></td>
</tr>
<?php
}
}else{
echo "no record found";
}
?>
</tbody>
</table>
Think you can add condition to the SQL query that filtersby the user's ID or username. Example >>> SELECT * FROM table WHERE user_id = '$user_id'

how to add my database on a html table using session variables in php

I have a website that has multiple users that submit some requests, So each user will have his own Requests table that has been made. my issue is I'm trying to show the requests for every user in a table by using the session variables to get the name of the user and match it with the request table but it dose not work.
$userin= $_SESSION['username'];
$req = "SELECT * FROM request_table where r_status='pending' AND requester = 'max';";
$request_table = $conn->query($req);
it works if I out ( Max ) only but when I put it as a session variable or as $userin it dose not work and I get this error :
Notice: Trying to get property 'num_rows' of non-object in C:\xampp\htdocs\website\b_table.php on line 54.
Below is my code for the table :
<body>
<h1 align="center">Table</h1>
<table border="5" align="center" style="line-height:20px;">
<tr>
<th>Request ID</th>
<th>Requester name</th>
<th>Customer name</th>
<th>Description</th>
<th>Submition Date</th>
<th>Request Status</th>
<th>link</th>
</tr>
<?php
//Fetch Data form database
if($request_table->num_rows > 0){
while($view_request = $request_table->fetch_assoc()){
?>
<tr>
<td><?php echo $view_request['request_id']; ?></td>
<td><?php echo $view_request['requester']; ?></td>
<td><?php echo $view_request['customer_name']; ?></td>
<td><?php echo "Site A is:". $view_request['site_a']. ",<br> Site B is: ".$view_request['site_b']. ",<br>NO. Of links =:".$view_request['no_oflinks']; ?></td>
<td><?php echo $view_request['sub_date']; ?></td>
<td><?php echo $view_request['request_status']; ?></td>
<form action="view_request.php" method="GET"><td><button type="vieww" name="vieww" value= <?php echo $view_request['request_id']; ?>> View </button></td></form>
</tr>
<?php
}
}
else
{
?>
<tr>
<th colspan="7">No Data availabe</th>
</tr>
<?php
}
$conn->close();
?>
</table>
``````````````````````````````````````````````````````````````````````````````````````````
[enter image description here][1]
[1]: https://i.stack.imgur.com/pOHOX.jpg
you can do some debugging and testing.
like check if session variable is set or not like :
if(isset($_SESSION['username'])){
$userin= $_SESSION['username'];
}
else{
die("Session user name wasn't set");
}
and for the query
$req = "SELECT * FROM request_table where r_status='pending' AND requester = '$userin';"
for more debugging and testing , you can use vardump to check if data is present or not like : vardump($request_table);
hope it helps you in some way...
hope it helps.

PHP and Mysql while loop

I have a mysql table with orders. I am trying to loop through the orders table and select individual client orders according to their user id and display the orders on their client accounts. However, my code below just prints the first row and repeats it endlessly jaming my browser every time.
what is wrong with this and how to i solve it
<?php
$records = $conn->prepare('SELECT * FROM orders WHERE user_id= :id');
$records-> bindParam(':id', $_SESSION['user_id']);
$records->execute();
$results=$records->fetch(PDO::FETCH_ASSOC);
$i=0;
while($i<=$results):
$i++;
?>
<h3>Your Orders</h3>
<table >
<tr >
<th>Order Number</th><th>Academic Level</th><th>Order Details</th>Manage Order</th>
</tr>
<tr>
<td>#SJ<?=$results['id']; ?> </td><td><?=$results['academic_level']; ?></td><td ><?=$results['details']; ?></td>
</tr>
</table>
<?php
endwhile;
?>
Remove all $i related code. Just move your fetch statement to while condition, like the following:
while( $results=$records->fetch(PDO::FETCH_ASSOC))
You need to include the html code in the while loop because you want to display all of the orders.
I'm using this method, try this out.
Start while in first php section
<?php
$username = $_SESSION['username'];
$sql = $db->prepare("SELECT * FROM tableName WHERE username = '$username' ");
$sql->execute();
while($results=$sql->fetch(PDO::FETCH_ASSOC)){
?>
After that the html section coming:
<h3>Your Orders</h3>
<table >
<tr >
<th>Order Number</th><th>Academic Level</th><th>Order Details</th>Manage Order</th>
</tr>
<tr>
<td><?php echo $results['id']; ?> </td><td><?php echo $results['academic_level']; ?></td><td ><?php echo $results['details']; ?></td>
</tr>
</table>
and here the ending
<?php
}
?>

No data echoed inside html table

Var dump result
I am fetching data in MySQL into an HTML table:
<div id="toggleDiv" class="">
<div class="box-body" id="toggleDiv_2">
<div class="row">
<div class="col-md-6">
<?php
$select_patient_info_cash =
"SELECT * FROM patient_info WHERE id_logged = :id_logged".
" AND patient_id = :patient_id AND payment_type = :pt";
$select_patient_info_cash_stmt =
$conn->prepare($select_patient_info_cash);
$select_patient_info_cash_stmt->bindValue(":id_logged", $id_logged);
$select_patient_info_cash_stmt->bindValue(":patient_id", $patient_id);
$select_patient_info_cash_stmt->bindValue(":pt", "cash");
$select_patient_info_cash_stmt->execute();
$select_patient_info_cash_stmt->fetch(PDO::FETCH_ASSOC);
$select_patient_info_cash_stmt_count =
$select_patient_info_cash_stmt->rowCount();
if($select_patient_info_cash_stmt_count > 0){ ?>
<table style="text-align:center"
class="table table-bordered table-striped table-hover">
<thead>
<tr>
<th>Project Description</th>
<th>Project Cost</th>
<th>Date Of Pay</th>
</tr>
</thead>
<?php foreach ($select_patient_info_cash_stmt as $cash) { ?>
<tr>
<td><?php echo $cash['project'] ?></td>
<td><?php echo $cash['project_cost'] ?></td>
<td><?php echo $cash['date_now'] ?></td>
</tr>
<?php } ?>
</table>
<?php } else { ?>
<?php } ?>
</div><!-- /.col -->
Now I test it for a user that have data in patient info where payment_type != cash, and the <thead> didn't show up. But when I test it where payment_type=cash the <thead> shows up but no data are echoed into line.
It should show me 2 new lines after <thead> and I can't figure out why data are not displayed on the page
I think you miss ->fetch() from your prepared statement. According to PHP docs:
<?php
$stmt = $dbh->prepare("SELECT * FROM REGISTRY where name = ?");
if ($stmt->execute(array($_GET['name']))) {
while ($row = $stmt->fetch()) {
print_r($row);
}
}
?>
Therefore you need to modify your code to something like:
<?php while ($cash = $select_patient_info_cash_stmt->fetch()) { ?>
<tr>
<td><?php echo $cash['project'] ?></td>
<td><?php echo $cash['project_cost'] ?></td>
<td><?php echo $cash['date_now'] ?></td>
</tr>
<?php } ?>
I also suggest that you should use a MVC framework or some sort of template engines. Mixing PHP and HTML is a very bad practice.
References: http://php.net/manual/en/pdo.prepared-statements.php

Sending over URL 2 Var (1 selected and 1 logged in user)

I am trying to send over 2 vars to a page - 1 being the logged in user and the other is the ID of a registered user which is having there profile updated by the logged in user.
Im not sure how to change the page i have.
<?php
include("includes/connect.php");
//logged in users ID from logged in page.
$id = $_GET['id'];
$sql="SELECT * FROM guild_apply";
$result=mysql_query($sql);
?>
<blockquote>
<table border='1' width='100%'>
<tr>
<th>ID </th>
<th>Real Name</th>
<th>Character Name</th>
<th>Class</th>
<th>Rank</th>
</tr>
<?php
while($row = mysql_fetch_array($result)){
?>
//info from all users registered with default rank (which is going to be changed)
<tr>
<td><?php echo $row['id']; ?> </td>
<td><?php echo $row['realname']; ?> </td>
<td><?php echo $row['charname']; ?> </td>
<td><?php echo $row['class']; ?> </td>
<td><?php echo $row['rank']; ?> </td>
<td align="center">update</td>
</tr>
<?php
}
?>
</table>
</blockquote>
?>
------update.php-----
<?php
// Connect to server and select database.
include("connect.php");
$id =$_GET['id'];
$rank =$_POST['rank'];
$charname =$_GET ['charname'];
// update data in mysql database
$sql="UPDATE guild_apply SET rank='$rank' WHERE charname='$charname'" or die ("cant find learner");
$result=mysql_query($sql) or die ("this stuffedup");
if ($result){
// if successfully updated.
header ("Location:../change.php?id=$id");
}else{
echo "Update failed";
}
?>
Add an &id= parameter to the URL.
<td align="center">update</td>

Categories