Reputation: 777
Hi guys so i am trying to create my profile pics for my users to upload. I have got it to work for the users, so you can log in upload etc, and then logout , all works fine, however at the moment it is showing every users profile pic and not the specific one which is logged in at the time
id
user_name
password
profilePic set it as text
MY PHP:
<?php
mysql_connect("localhost","root","") or die ("could not connect");
mysql_select_db("reg") or die ("could not find db");
session_start();
if(!$_SESSION['username']){
header("location: login.php");
}
$favs = array();
$links = array();
$sql = "SELECT * FROM recipe WHERE fav='yes'";
$records = mysql_query($sql);
//while($result =mysql_fetch_assoc($records)){
//$favs[] = $result['recipeName'];
//$links[] = $result['url'];
//}
$user_name = $_SESSION['username'];
if(isset($_POST['submit'])){
move_uploaded_file($_FILES['file']['tmp_name'],
"Image/userImages/".$_FILES['file']['name']);
$con = mysqli_connect("localhost","root","","reg");
$q = mysqli_query($con,"UPDATE users SET profilePic = '".
$_FILES['file']['name']."' WHERE user_name = '".$_SESSION['username']."'");
}
?>
html:
<div class = "pic">
<?php
$con = mysqli_connect("localhost","root","","reg");
$q = mysqli_query($con,"SELECT * FROM users");
while($row = mysqli_fetch_assoc($q)){
if($row['profilePic'] == ""){
echo "<img width='100' height='100'
src='Image/userImages/profile.png' alt='Default Profile Pic'>";
} else {
echo "<img width='100' height='100'
src='Image/userImages/".$row['profilePic']."' alt='Profile Pic'>";
}
echo "<br>";
}
?>
<div class="userNameText">
<?php echo $_SESSION['username'];?>
</div>
</div>
It is something to do with this: $q = mysqli_query($con,"SELECT * FROM users");
I tried changing that to $q = mysqli_query($con,"SELECT * FROM users WHERE user_name = $user_name");
but no luck so went back to something which almost worked. Anyway any help on this matter would be amazing
Upvotes: 0
Views: 39
Reputation: 2180
Change
$q = mysqli_query($con,"SELECT * FROM users");
to
$q = mysqli_query($con,"SELECT * FROM users where user_name = '".$_SESSION['username']."'");
you forgot to add ' '
to where, if that html is in the same file as the php, then you can use $user_name if not, you have to use session
Upvotes: 1