我正在尝试创建用户个人资料图片功能。到目前为止,我能够上传和选择图像并显示它,但我无法弄清楚如何选择特定于用户的图像。我的查询工作(虽然有上述问题),直到我添加下面的代码(//添加是我更改的)。
我能够得到我的插入查询来发送用户的user_id,我只是无法弄清楚选择部分。
我的数据库很小,只有id, user_id, img
。
我想选择数据库中包含用户user_id的img
。我带有一个会话的user_id,这就是$user_id
。
任何人都会在我的选择查询中看到我做错了什么?
function getPhoto($con,$dest)
{
$user_id = ( isset( $_SESSION['user'] ) ? $_SESSION['user'] : "" ); //added
// $result = mysqli_query($con,"SELECT * FROM `profile_img` where `img` = '$dest'");
$result = mysqli_query($con,"SELECT * FROM `profile_img` where `img` = '$user_id'"); //added
if($row = mysqli_fetch_array($result))
return $row;
return 0;
}
编辑:更多代码。
function getPhoto($con,$dest)
{
$user_id = ( isset( $_SESSION['user'] ) ? $_SESSION['user'] : "" ); //added
// $result = mysqli_query($con,"SELECT * FROM `profile_img` where `img` = '$dest'");
$result = mysqli_query($con,"SELECT * FROM `profile_img` where `user_id` = '$user_id'"); //added
if($row = mysqli_fetch_array($result))
return $row;
return 0;
}
// Make sure all functions above are include here
// Get the database connection
$con = Connection();
// Check for post
if(isset($_POST['create'])) {
// Try uploading
$upload = UploadFile($_FILES);
// If upload fails
if(!$upload['success'])
echo '<h3>Sorry, an error occurred</h3>';
else {
// You could add error handling here based on the results of
// each function's success or failure below.
// Try to save it
$saveToDb = SaveToDb($con,$upload['file']['dest']);
// Get the profile from image name
$profPic = ($saveToDb)? getPhoto($con,$upload['file']['dest']) : false; ?>
<?php
}
}
?>
<img id="profile-pic" src="<?php echo (!empty($profPic) && $profPic != 0)? $profPic['img'] : "profile_images/default.jpg"; ?>" alt="<?php echo (!empty($profPic) && $profPic != 0)? "Profile Picture" : "No Picture"; ?>" />
<form action="" method="POST" enctype="multipart/form-data">
<input type="file" name="file" class="inputbarfile" onchange="readURL(this);">
<img width="400px" height="300px" id="file" src="#" alt="your image">
<input type="submit" name="create" id="signinButton" value="Upload">
</form>
答案 0 :(得分:0)
此:
$result = mysqli_query($con,"SELECT * FROM `profile_img` where `img` = '$user_id'"); //added
应该是:
$result = mysqli_query($con,"SELECT * FROM `profile_img` where `user_id` = '$user_id'"); //added
由于img
列包含image path
而您正在比较user id
。