아래 쿼리에서 사용자의 프로필 사진을 선택하여 출력하려고합니다. 내 삽입 쿼리 및 선택 쿼리가 작동합니다 (저는 믿습니다). 지금은 이미지를 업로드 할 수 있으며 사용자의 user_id는 내 데이터베이스의 이미지와 연결되어 있습니다. 그런 다음 선택한 쿼리에서 해당 사용자와 관련된 이미지를 선택하려고합니다.select query issue 출력
내 데이터베이스는 간단하며 그 안에는 다음이 있습니다. id, user_id, img
.
나는 문제가 여기, 내 출력 존재할 수있다 생각 : 난 그냥 내가 잘못하고있는 중이 야 모르는
<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"; ?>" />
. 내가 잘못 출력했는지 누가 알 수 있습니까? 지금은 기본 이미지가 페이지로드시로드되지만 사용자에게 이미지를 업로드하면 이미지가 표시되지만 페이지를 다시로드하면 이미지가 기본값으로 돌아갑니다.
function getPhoto($con,$dest)
{
$user_id = (isset($_SESSION['user']) ? $_SESSION['user'] : ""); //added
$result = mysqli_query($con,"SELECT * FROM `profile_img` where `user_id` = '$user_id' ORDER BY `id` DESC LIMIT 1");
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>
변경하여'getPhoto의 반환 값()' 이미지 열 대신에 전체 열을 반환합니다 –
또한 $ profPic은 데이터가 데이터베이스에 삽입되었는지 확인합니다. 데이터를 새로 고치면 데이터베이스에 삽입되지 않으므로 기본값이 표시됩니다. 그래서 단지 데이터베이스에서 데이터를 꺼내는'$ profPic'에 별도의 무조건 서술문을 할당하십시오. –
이렇게하면 업로드 된 사진이 표시 (선택)되는 것을 멈 춥니 다. – Becky