Im trying to create a dynamic web page in php and mysql. I have the below code on the profile.php page. The issue im having is on the "while" line im not sure how i would go about getting the information from the DB. I want to display details like email, country First name, etc. Any help appreciated. Thanks in advance!
CODE:
$userid = (isset($_GET['id']) ? $_GET['id'] : NULL);
if ($userid) {
$userinfo = $DB->query_first("SELECT * FROM `users` WHERE `id` = '$userid'");
print_r($userinfo);
}
//{
while($row = sql_fetch_assoc($DB)){
echo'<div class="container">
<div class="jumbotron" align="block">';
echo $row['first_name'];
echo $row['last_name'];
echo $row['country'];
echo $row['username'];
echo'</div>';
echo'</div>';
}
Try by using this
<?php
$userid = (isset($_GET['id']) ? $_GET['id'] : NULL);
if ($userid) {
$userinfo = $DB->query_first("SELECT * FROM `users` WHERE `id` = '$userid'");
print_r($userinfo);
?>
<table>
<tr>
<td>First Name</td>
<td><?=$userinfo['first_name']?></td>
</tr>
<tr>
<td>Last Name</td>
<td><?=$userinfo['last_name']?></td>
</tr>
............
............
</table>
<?php } ?>
do not use null use a empty string "" that is what you want sql well do nothing with an empty string it is just white space, enough about sql injection attacks wtf ever topic i enter.
actually make it equal to wither where userid='$userid' or "".
there is only one row so the while statement is redundant just row = mysql_fetch_array.
You can try something like this:
<?php
$userid = (isset($_GET['id']) ? $_GET['id'] : NULL);
if ($userid) {
$userinfo = $DB->query_first("SELECT * FROM `users` WHERE `id` = '$userid'");
print_r($userinfo);
echo'
<div class="container">
<div class="jumbotron" align="block">
'.$userinfo['first_name'].' <br>
'.$userinfo['last_name'].' <br>
'.$userinfo['country'].' <br>
'.$userinfo['username'].' <br>
</div>
</div>';
?>
Related
i'm currently working on an admin which permit to the user who has the rights to delete any account with a button, and all the accounts are displayed in a table and stored in a database (i'm using MySQL). For each row of the , there is a delete button, and i would like that, by pressing the button of the choosed row, it delete the account of the row selected. I dont really know how to do it clearly that's why I ask.
Can someone explain me the approach that i should take and give me an idea.
Have a good day.
[the admin pannel screen][1]
Here is where I am in my code :
<?php
require_once('testadmin.php');
if(!$isAdmin) {
header('Location: index.php');
}
include_once('./includes/head.php');
include_once('./includes/nav.php');
require_once('./lib/db.php');
// users
$stmt = $db->prepare('SELECT * FROM users');
$stmt->execute();
$users = $stmt->fetchAll();
?>
<main>
<table id="usersTable">
<h1 style="text-align: center;margin-top: 25px;">Users</h1>
<tr>
<th>ID</th>
<th>Username</th>
<th>Email</th>
<th>Role</th>
<th> </th>
</tr>
<?php
foreach ($users as $user)
{
echo
'<tr>
<td>'.$user['id'].'</td>
<td>'.$user['username'].'</td>
<td>'.$user['email'].'</td>
<td>'.$user['role'].'</td>
<td><button class="btn-danger" onclick=\'return confirm("Confirm account delete?");\'>
Delete account</button></td></tr>';
}
echo "</table>";
?>
delete.php :
NOTE : this is actually for delete the current connected account.
<?php
require('./lib/db.php');
session_start();
if(empty($_SESSION['user'])){
header('Location: login.php');
}
$user = $_SESSION['user'];
$req = $db->prepare('DELETE FROM users WHERE id=:id');
$req->bindValue(':id', $user["id"], PDO::PARAM_INT);
$req->execute();
unset($_SESSION['user']);
session_destroy();
header('Location: index.php');
Pass the identifier for the record to the delete.php page:
<a href="delete.php?id=' . $user['id'] . '" class="delete" style="color: #fff">
Then in delete.php you can reference $_GET['id'] and use that value in your SQL query to delete that specific record.
Note of course that within delete.php you'll probably want to make sure the current user is authorized to delete that record.
I'm implementing a flag comment system on my blog, currently have this code for it:
<form method="POST">
<?php
$con=mysqli_connect("localhost","root","admin","MyDatabase");
$sql = mysqli_query($con, "SELECT id, username, comment_body, user_id, DATE_FORMAT(date_created, '%M %d %Y') AS date_created FROM news_comments WHERE entry_id = '" . $entry_id. "'");
?>
<?php while ($comment = mysqli_fetch_array($sql)) { ?>
<table class="table">
<thead>
<tr>
...
</tr>
<td>
<?php echo $comment['username']; ?>
</br>
<?php echo $comment['comment_body']; ?>
</br>
<?php echo $comment['date_created'];?>
</td>
...
<td>
<button name = "report">Report</button>
<?php if(isset($_POST['report'])) {
$id = $comment['id'];
$conn=mysqli_connect("localhost","root","admin","MyDatabase");
$sqli = mysqli_query($con, "UPDATE news_comments SET isFlagged = 1 WHERE id = '$id'");
} ?>
</td>
....
</table>
It works, but when the report button is clicked, it flags all comments on the page. How can I change this so it's just the one specific comment?
Thanks
This is happening because you have your flagging logic in the while loop. Whenever a user clicks the Report button every condition where if(isset($_POST['report'])) is triggered - which would be all the comments.
You have to move the condition out of the loop (typically the top of the page), and then send the id of the post you want to flag as it will no longer be part of the loop.
I have a problem with $_GET method. I have retrieved some data about admins of a webpage from database & I added a hyperlink for users to get the information about that them.
Here's the code in my 1st page:
<?php if(($adminlevel)==1){
echo '
<h4 class="widgettitle">List of admins</h4>
<table class="table responsive">
<thead>
<tr>
<th>Admin Level</th>
</tr>
</thead>
'; getAdmins(); echo '
</table>
';
}else{
echo '<h4 class="widgettitle">You dont have permission to see this table</h4>';
}
?>
<div class="divider15"></div>
The function getAdmins() goes like this:
<?php
function getAdmins(){
global $con;
$get_admin = "select * from admins order by id";
$run_admin = mysqli_query($con,$get_admin);
while($row_admin = mysqli_fetch_array($run_admin)){
$id_admin = $row_admin['id'];
echo "
<tbody>
<tr>
<td>Trident</td>
<td class='center'><a href='editlevel.php?id=$id_admin' title='Clik to change admin level' target='_blank'>$adminlevel_admin</a></td>
</tr>
</tbody>
";
}
}
?>
As you see I link the users from my first page to another page which is called editlevel.php by the function getAdmins().
Therefore I made my hyperlink like this:
<a href='editlevel.php?id=$id_admin'>$adminlevel_admin</a>
And Here's the editlevel.php page:
<body>
<?php
if (isset($_GET['id_admin'])){
$result_id = $_GET['id_admin'];
$get_result = "select * from admins where id='$result_id'";
$run_result = mysqli_query($con,$get_result);
while($row_result= mysqli_fetch_array($run_result)){
$id_target = $row_result['id'];
$username_target = $row_result['username'];
$adminlevel_target = $row_result['adminlevel'];
$email_target = $row_result['email'];
echo '
<div class="mainwrapper">
<div class="header">
'; include "php/php_includes/overall/header.inc.php"; echo'
</div>
<div class="leftpanel">
';include "php/php_includes/overall/leftpanel.inc.php"; echo '
</div><!-- leftpanel -->
<div class="rightpanel">
'; include "php/php_includes/gadgets/rightpanel.editlevel.php"; echo '
</div><!--rightpanel-->
</div><!--mainwrapper-->
';
}
}
?>
</body>
So basically I used if (isset($_GET['id_admin'])){ to get the results of the item which user clicked & try to retrieve the data of that item from database via that.. But the problem is nothing appears at my screen. No error message & no result. Please if you know how can I solve it please let me know!
It appears that your link is:
<a href='editlevel.php?id=$id_admin'>$adminlevel_admin</a>
When it should be:
<a href='editlevel.php?id_admin=$id_admin'>$adminlevel_admin</a>
In order for it to work with:
if (isset($_GET['id_admin'])){
$result_id = $_GET['id_admin'];
Edit: It goes without saying, you should never trust user input (such as $_GET). These values should be validated and sanitised before being used in SQL queries.
Im making a page that will display top ten voters in a table and show the number one voter aswell. I got the top ten table to work but I can't figure out how to get the second part to work. What would I do to get it to work where it would show the top voter in a img tag like this example
img src="https://minotar.net/avatar/TOPVOTERHERE"
First part
<div class="col-md-6">
<table class="table table-striped">
<tr>
<td><strong>Username</strong></td>
<td><strong>Votes</strong></td>
</tr>
<?php
$username2 = "exampleuser";
$password2 = "pass";
$hostname = "127.0.0.1";
$dbhandle = mysql_connect($hostname, $username2 , $password2)
or die("Unable to connect to MySQL");
$selected = mysql_select_db("mc711",$dbhandle)
or die("Could not select database");
$result = mysql_query("SELECT * FROM GALTotals ORDER BY votes DESC");
$count = 0;
while ($row = mysql_fetch_assoc($result)) {
$count++;
if($count>10){
}else{
echo "<tr>";
echo "<td>".$row['IGN']."</td>";
echo "<td>".$row['votes']."</td>";
echo "</tr>";
}
}
?>
</table>
</div>
Second Part - This is the part I dont know how to do
<div class="col-md-6">
<center>
<img src="https://minotar.net/avatar/TOPVOTERHERE">
</center>
</div>
The simplest way to do it would be to create a variable to store the top voter data before you start your while loop. Then, in the while loop, you can set this variable if you're in the first iteration.
First part:
$result = mysql_query("SELECT * FROM GALTotals ORDER BY votes DESC");
$count = 0;
$topVoterIGN = NULL;
while ($row = mysql_fetch_assoc($result)) {
$count++;
if($count>10){
}else{
echo "<tr>";
echo "<td>".$row['IGN']."</td>";
echo "<td>".$row['votes']."</td>";
echo "</tr>";
if(is_null($topVoterIGN)) {
$topVoterIGN = $row['IGN'];
}
}
}
Second Part:
<div class="col-md-6">
<center>
<img src="https://minotar.net/avatar/<?php echo $topVoterIGN; ?>">
</center>
</div>
Alternatively, you can store the top 10 voters in an array and then retrieve the first item later.
I'm new to php and mysql so sorry if i'm doing it wrong. i have a page on my site that lists the reviews that members give to other other users.
Basically i have approved and deleted in my database which means that after a user sends the review it has to be reviewed by the user before it gets displayed.
once the user clicks the approved image which is a tick it goes to approved_review.php and in their i have my sql code to update the value from 0 to 1 in my database.
It should work exactly the same for the delete but obviously instead of updating the approved column it will update deleted.
the code i have tried is not working i have been working on this for quite some time and can;t figure it out.
Can someone please tell me where i'm going wrong?
Heres the code:
<?php
$reviews_set = get_pending_reviews();
while ($reviews = mysql_fetch_array($reviews_set)) {
?>
<p> </p>
<div class="pending-review-content">
<?php
$date = $reviews['date_added'];
?>
<div class="prof-content-pend-reviews" id="reviews">
<div class="message_pic"><?php echo "<a href=\"profile.php?id={$reviews['from_user_id']}\">
<img width=\"50px\" height=\"50px\" src=\"data/photos/{$reviews['from_user_id']}/_default.jpg\" /></a>";?>
</div>
<div class="reviews-date"><? echo "$date"; ?></div>
<div class="reviews-from">
<?php echo "<a href=\"profile.php?id={$reviews['from_user_id']}\">{$reviews['display_name']}"; ?>
</a> Wrote:
</div>
<div class="reviews-content">
<?php echo "{$reviews['content']}"; ?>
</div>
</div>
<div class="reviews-approve">
<img src="assets/img/icons/tick.png" width="30" height="25" /></div>
<div class="reviews-delete">
<img src="assets/img/icons/cross.png" width="30" height="25" />
</div>
<? } ?>
approved_review.php function:
<?
$sql = "UPDATE `playtime`.`ptb_reviews` SET `approved` = '1' WHERE `ptb_reviews`.`id` =".$_SESSION['user_id']."";
echo "<div class=\"infobox1\">review approved.</div>";
?>
Your approach seems logical. After you loop through your reviews, you click on the tick or delete pngs to update or delete.
So, in approved_review.php
<?php
//you are missing the connection to your mysql database...
$link = mysql_connect("localhost", "mysql_user", "mysql_password");
mysql_select_db("database", $link);
$sql = "UPDATE `playtime`.`ptb_reviews` SET `approved` = '1' WHERE `ptb_reviews`.`id` =".$_SESSION['user_id']."";
//execute the mysql query
$r = mysql_query($sql);
if (!mysql_error())
{
echo "<div class=\"infobox1\">Review Approved.</div>";
}
?>
a little edit rrrfusco's post
// or die for details if mysql_query won't work correct
$r = mysql_query($sql) or die (mysql_error());