ユーザープロファイルイメージ機能を作成しようとしています。これまでは画像をアップロードして選択して表示することができましたが、ユーザー固有の画像を選択する方法はわかりません。以下のコードを追加するまで(私が変更したものが追加されています)、私のクエリは機能していました(ただし上記の問題があります)。列に一致するクエリを選択します。
私はそれで、ユーザのUSER_IDを送信するために私のINSERTクエリを取得することができた、私はちょうど選択部分を把握することはできません。このため
私のデータベースには、それだけで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>
したい場合は、 ' – nogad