2016-10-19 8 views
-3

ユーザープロファイルイメージ機能を作成しようとしています。これまでは画像をアップロードして選択して表示することができましたが、ユーザー固有の画像を選択する方法はわかりません。以下のコードを追加するまで(私が変更したものが追加されています)、私のクエリは機能していました(ただし上記の問題があります)。列に一致するクエリを選択します。

私はそれで、ユーザの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> 

enter image description here

+0

したい場合は、 ' – nogad

答えて

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を比較しているので。

+0

(*)ですべてうーん、それはまだ、ユーザーの画像を出力されていない、それを選択しないimg'。 – Becky

+0

結果を '選択する 'から'エコーする'まで、完全なコードを表示する必要があります。他の部分であなたのコードを推測するためにここではマジシャンではありません。 – Irvin

+0

$ destは関数名と同じ行にあるか、それとも重要ではないので、どこかに$ destを置かなければなりませんか? – Becky

関連する問題