Hello there so I am having a little trouble here what I am trying to do is use my code below to show all the users friend requests on the same page, but what is happening is the page is showing only one request at a time then the page has to be refreshed or reloaded for the remaining requests to show up one by one instead off all of them listed down the page any help would be great as I'm trying to learn thank you
<?php
include ('views/header.php');
require_once ('config/config.php');
include ('config/connection.php');
{
global $user_name,$page_owner,$username;
$user_name = trim(strip_tags($_SESSION["user_name"])); //This is the user who logged into the system or logged in session
$page_owner = trim(strip_tags($_SESSION["user_name"])); // This is the owner of the page viewed
$username = mysql_query("select * from request where friend ='".$user_name."'");
$user_id = mysql_query("select user_id from users where user_id = 'user_id'");
//This is the page that checks for Friend Request
$check_request = mysql_query("select * from request where friend = '".$user_name."'"); //First Request receive, first to respond to
if(intval(mysql_num_rows($check_request))==0); //If there is a friend request for the logged in user then show it to the user otherwise do nothing
$get_request_details = mysql_fetch_array($check_request);
//Check friend who sent the request full info from the users table
$check_request_info = mysql_query("select * from `users` where `user_name` = '".mysql_real_escape_string($get_request_details["username"])."'");
//Get friend who sent the request full info from the users table
$get_request_info = mysql_fetch_array($check_request_info);
//Check logged in user full info from the users table
$check_logged_in_user_info = mysql_query("select * from `users` where `user_name` = '".$_SESSION['user_name']."'");
//Get logged in user full info from the users table
$get_logged_in_user_info = mysql_fetch_array($check_logged_in_user_info);
?>
new requests(<?php echo intval(mysql_num_rows($check_request)); ?>)
<div>Hello <?php echo strip_tags($get_logged_in_user_info["user_name"]);?><div>
<div style="font-family:Verdana, Geneva, sans-serif; font-size:11px; line-height:18px;" align="left">Here are your friend requests.</div>
<font style="color:blue;font-family:Verdana, Geneva, sans-serif; font-size:14px;"><?php echo strip_tags($get_request_info["user_name"]); ?> wants to be friends</font><div>
<div>
<div>
<a href="af.php?username=<?php echo $get_request_info["user_name"];
?>"class="square">Accept</a>
Decline
<?php
}
{
//Unknown page realized
}
?>
It's because you only print out one. You never loop through the array containing all friend requests - you echo the first one in the array. If you decline or accept that one, it'll show the next one, and so forth.
See Populate PHP Array from While Loop
The function mysql_fetch_array() only fetches one result row at a time. You want to wrap this in a while loop like so:
while ($get_request_details = mysql_fetch_array($check_request))
{
// do repetitive processing
}
$user_name = mysql_real_escape_string($user_name);
$query ="SELECT user_id FROM users JOIN request ON users.user_id = request.user_id WHERE users.user_id = {$user_name}";
$request = mysql_query($query);
while ( $row = mysql_fetch_assoc($request)){
$results[] = $row;
}
//debug
echo '<pre>';
print_r($results);
and it's a good practice to use LIMIT in your SQL
Related
I have a page called profiles.php that displays the users own information when logged in. Recently I made it so the user can click on the name of someone else and it will take them to profiles.php. I want it to display the user's they clicked on profile/information, but it only shows your own information.
The way I have the page now is, it uses a session varaible based on if your logged in and from there it puts your data out in its designated places if that makes sense.
//if theuser is logged in then it turns their database id into a variable for later use
if(isset($_SESSION['userUid'])){
$current = $_SESSION['Id'];
}
//This is the link a person clicks on thats supposed to take them to that user's page. $post is a varaible from a foreach. I tried to make it so once they click it takes them to profiles.php users id (href = "profiles.php/id"'.$post["idUser"].'")
<a href = "profiles.php/id"'.$post["idUser"].'" ><h1>'.$post["UserID"].'</h1></a>
I expect the output to be taking the the current user to the desired user's profile page, but the actual output is taking the current user to their own profile page.
From what I understand you would like to show specific users profile information.
// define your connection
require_once('connection.php');
$users = mysql_query($conn, "Select id, name from users);
$row = mysql_fetch_array($users)
?>
<ul>
<?php
while($row){
echo <? <a href='profile_display.php?id=<?php $row['id'] ?> > <li> <?php $row['name'] ?> </li></a> ;
}
</ul>
in profile display.php
<?php
require_once'connection.php' ;
$id = $_GET['id'];
if(isset($id)) {
$result = mysql_query($conn, 'select * from users where id=$id);
if(!empty($result)) {
// display all value here
} else {
echo "No user profile information was found!"
}
?>
This code is not tested by me. But I would say this is the strategy needed to be used here.
Pseudocode:
List all users
User click specific other user, grab their id
make a query on db for the user information
display if available else display error information
Hope this helps!
currently I'm stuck with how I should identify/differentiate a particular account from many accounts that exists in my database. Let's say I am in home page and I click an account and I want the information to be displayed of that particular account that I viewed, but I couldn't display it as I don't know how to identify that particular account that I try to view.
Here's my php code to display all the accounts.
$username = $_SESSION['username'];
$sql1 = "SELECT username, address, country, zipcode, photo, bio FROM user WHERE NOT username = '$username'";
$result = $conn->query($sql1);
if($result->num_rows > 0)
{
while($row = $result->fetch_assoc())
{
echo "<div id = 'account'>";
echo "<span id = 'image'><a id = 'accounts' href = 'accounts.php'><img src = '".$row['photo']."' alt = 'profile photo' width = '100px' height = '100px' style = 'margin-right:40px; margin-top: 5px;'></span>";
echo "<div id = 'username'><big><b style='text-transform:lowercase;'>".$row['username']."</big></a></b><br><small><i><q>".$row['bio']."</q></i></small><br>Lives in <b style='text-transform: capitalize;'>".$row['address']."</b><br>From <b>".$row['country']."</b></div><button id = 'addFriend' type = 'button' onclick = 'addFriends()'>Add friend</button>";
echo "</div>";
}
}
From the above code I can easily identify the user(person who logged in) account by storing his/her username in session from the input text while signing up. But now as theres no textfield or anything from where I can get the username to store in session, I am stuck on how to identify a user. And to retrieve it from database too, I think I pretty much need to identify the user first. Hope I am clear.
Thank You in advance.
From what I understood you have an home page and when you click an account you want to display a page specific to that account.
Supposing that this is what you are looking for, you can identify the account using their id or user name.
What I usually do I use GET parameters to get the information and then I run a query to get the information. So what you need is a specific link https://example.com/user?id=3 like this.
As you can see after the domain and the page (https://example.com/user) we have a question mark and a name id in this case (can be whatever you want) and a value =3
this means that I have a value in the superglobal array $_GET which is accessible using id as key, so if I do this echo $_GET["id"]; //3 the output will be 3.
By doing so you can use this value to query the DB and get a specific account.
REMEMBER TO SANITIZE INPUT!!! other wise user can hack into you DB ( use this link for more info)
after you can run a query to get the information.
Detailed example:
-Home.php
/**/
Visit This Account!!
/**/
--User.php
/**/
$id = $_GET["id"]; //SANITIZE INPUT!!!!!!
$sql1 = "SELECT username, address, country, zipcode, photo, bio FROM user WHERE id = '$id'";
$result = $conn->query($sql1);
if($result->num_rows > 0)
{
while($row = $result->fetch_assoc())
{
echo "<div id = 'account'>";
echo "<span id = 'image'><a id = 'accounts' href = 'accounts.php'><img src = '".$row['photo']."' alt = 'profile photo' width = '100px' height = '100px' style = 'margin-right:40px; margin-top: 5px;'></span>";
echo "<div id = 'username'><big><b style='text-transform:lowercase;'>".$row['username']."</big></a></b><br><small><i><q>".$row['bio']."</q></i></small><br>Lives in <b style='text-transform: capitalize;'>".$row['address']."</b><br>From <b>".$row['country']."</b></div><button id = 'addFriend' type = 'button' onclick = 'addFriends()'>Add friend</button>";
echo "</div>";
}
}
/**/
P.S.
You should use PDO and prepered statement to prevent from attacks.
Being a huge PHP newbie I find myself stuck here.
I have an HTML table for a videogame store filled with elements taken from my database.
The point is, I want to be able to add a link to the game title. Moreover I want the link to direct to some "gamePage.php", a php page used for every videogame but of course showing different infos for each game (title, console etc).
The fact is that not only I can't add the hyperlink, but I have no clue on how to carry the videogame infos when I click on a link (even managing to add the link, all I would manage to do would be redirecting the user to a blank gamePage.php with no title).
This is the code I use to fill the table (the restore function fills my table):
<html>
<body>
<div>
<table width = "550px" height = "300px" border="2" >
<tr bgcolor="#5f9ea0">
<td>Title</td>
<td>Console</td>
<td>Genre</td>
<td>Price</td>
</tr>
<?php
$conn = #pg_connect('dbname=project user=memyself password=project');
function search(){
<!-- Work in progress -->
}
function restore(){
$query = "SELECT v.Title , c.Consolename , g.Genrename , v.Price
FROM vg_shop.videogame v, vg_shop.console c, vg_shop.genre g
WHERE v.Console=c.IDConsole AND v.Genre=g.IDGenre";
$result = pg_query($query);
if (!$result) {
echo "Problem with query " . $query . "<br/>";
echo pg_last_error();
exit();
}
while($myrow = pg_fetch_assoc($result)) {
printf ("<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>",
$myrow['title'], $myrow['consolename'], $myrow['genrename'], $myrow['price']);
}
}
<!-- some code -->
</body>
</html>
At first i tried to do this
while($myrow = pg_fetch_assoc($result)) {
printf ("<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>",
$myrow['title'], $myrow['consolename'], $myrow['genrename'], $myrow['price']);
But all I get is a white page, there's some syntax error I don't get.
And, even if it worked, I still can't carry at least the videogame PID through the gamePage link
so, you're managing to go to gamepage.php somehow.
So you need to add some sort of identifier to your link you that you could do some query on the gamepage.php by using that identifier to get info for that particular game.
while($myrow = pg_fetch_assoc($result)) {
printf ("<tr><td><a href='gamePage.php?id=%s'>%s</a></td><td>%s</td><td>%s</td><td>%s</td></tr>", $myrow['id'], $myrow['title'], $myrow['consolename'], $myrow['genrename'], $myrow['price']);
Note: I assume that you're picking $myrow['id'] from database as well.
now on your gamepage.php do following.
$id = $_GET['id'];
$sql = "SELECT * FROM vg_shop.videogame WHERE `id` = $id";
$result = pg_query($sql);
if($result){
$result = pg_fetch_assoc($result):
//...
// echo "Name: ".$result['Title'];
// other fields
}
$result will have all info about that particular game that was clicked, you can display all as you want.
Cheers :)
I made a login system and it works but I also have the user's session in the page after login.
How can I get user data from my database by referencing the user's session, and how can I update it when the user put what they going to change
<?php
session_start();
$query=mysql_query("SELECT username, email, password FROM user WHERE username = $_SESSION['username']");
while($row = mysql_fetch_array( $query )) {
echo .row['username'];
echo .row['email'];
echo .row ['password'];
?>
In new codes you should be using mysqli if your PHP version is high enough (if not... update...)
But here you go:
<?php
session_start();
$query = mysql_query("SELECT * FROM user WHERE username = {$_SESSION['username']}");
$row = mysql_fetch_assoc($query);
echo $row['username'];
echo $row['email'];
echo $row ['password'];
?>
Oh and the update:
<?php
$query = mysql_query("UPDATE user SET username={$_POST['username']}, email={$_POST['email']}, password={$_POST['password']} WHERE username={$_SESSION['username']}");
?>
And you should make a form for that..
The answer of Cris is almost perfect and well explained. But it is missing the quotes for the string in the statement.
As the col username will most likely be a varchar of any length.
$query = mysql_query("SELECT * FROM user WHERE username = '{$_SESSION['username']}'");
I am having some difficulty using PHP with jQTouch. I am fairly
confident with JavaScript however my PHP skills are little to none.
I am creating an application for my final year project at University
what displays football rumours posted by different users. My problem
is as follows:
I have one screen that displays each individual rumour, using a while
loop in PHP I am able to get each rumour from the database and display
them correctly. However I want to be able to click on one rumour which
then displays this rumour in a different screen, along with options to
reply/share etc. However I do not know how to tell which rumour has
been clicked on.
Snippets of my code:
All rumours page:
<?php
$q1 = "SELECT * FROM tblrumours;";
$r1 = mysql_query($q1);
while( $row1 = mysql_fetch_assoc($r1) ){
?>
<a class="rumourTag submit" id="<?php echo $row1['rumourID']; ?>">
<div class='oneRumour'>
<div class='standardBubble'>
<p>
<?php
$userID = $row1['userID'];
$q2 = "SELECT * FROM tblusers WHERE userID = $userID;";
$r2 = mysql_query($q2);
while( $row2 = mysql_fetch_array($r2) ){
$username = $row2['username'];
$teamID = $row2['teamID'];
}
$q5 = "SELECT * FROM tblteams WHERE teamID = $teamID;";
$r5 = mysql_query($q5);
while( $row5 = mysql_fetch_array($r5) ){
echo "<img src='img/".$row5['teamPicture']."' alt=''
class='teamImg' />";
}
?>
<span class='username'>
<?php
echo $username;
?>
</span>
<br/>
<span class='rumourMsg'><?php echo $row1['rumourText']; ?></
span>
</p>
</div>
</a>
SINGLE RUMOURS PAGE:
<?php
$q1 = "SELECT * FROM tblrumours WHERE rumourID = 1;"; /* NEED
TO SELECT WHERE RUMOUR ID IS THE ONE THAT IS CLICKED */
$r1 = mysql_query($q1);
while( $row1 = mysql_fetch_array($r1) ){
?>..........
I have tried using Session variables, storing the ID's in an array,
creating a separate php file for the single rumour page, and all to no
avail. I am guessing I have to use AJAX in some way, but I have no
idea where to even begin. Any help is greatly appreciated!
Thanks!
If you need to click on a rumour to see more details about it, you could always output in the HTML a unique value used to reference that rumour in the DB.
e.g. have <span class='rumourMsg' id='rumourName'> where rumourName is a unique value stored in your database to reference that rumour. Then when a user clicks to see more details, you can make a request to the PHP page with that value and return the content.
e.g. rumourDetails?rumourName=uniqueRumourName
(make sure to escape all your data properly to avoid SQL injection vulnerabilities.)