大家好,我正在尝试为我的用户上传创建我的个人资料照片。我已经让它为用户工作,所以你可以登录上传等,然后注销,一切正常,但是目前它显示每个用户个人资料图片而不是当时登录的特定用户< / p>
id
user_name
password
profilePic set it as text
我的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>
这与此有关:$q = mysqli_query($con,"SELECT * FROM users");
我尝试将其更改为$q = mysqli_query($con,"SELECT * FROM users WHERE user_name = $user_name");
,但没有运气,所以回到了几乎有用的东西。无论如何,任何有关此事的帮助都会令人惊讶
答案 0 :(得分:1)
更改
$q = mysqli_query($con,"SELECT * FROM users");
到
$q = mysqli_query($con,"SELECT * FROM users where user_name = '".$_SESSION['username']."'");
你忘了将' '
添加到哪里,如果那个html与php在同一个文件中,那么你可以使用$ user_name,如果没有,你必须使用session