【解決方法】PHPでuser_nameを使用してユーザーのプロフィール写真を取得するには?


最初のログイン時に写真をアップロードできるユーザー プロファイルを作成しました。 彼のプロフィール写真はフォルダーに保存され、画像パスはデータベースに保存されます。 今、私はそれを一意にした彼のuser_nameを使用してその画像を取得したいと考えています。 ユーザーが隠し送信ボタンを使用してプロフィール写真を保存すると、彼のuser_nameがデータベースに保存されます。
しかし、この画像コードの取得は機能しません

私が試したこと:

これは私の完全な歓迎です。 ユーザーログがプロファイル写真を変更し、プロファイルの詳細を表示するphpファイル。

<?php include('config.php');?>
<?php
    // index.php
    session_start();
    if(!isset($_SESSION['username']))

     {
        header("Location: login.php");
        exit();
    }
    $username = $_SESSION['username'];
    $sql = "SELECT *  FROM services WHERE user_name = '".$_SESSION['username']."'";
    $result = $con->query($sql);

    if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        ?>
 <!-- Save username and profile picture-->   

<link rel="stylesheet" type="text/css" href="css/welcome_php.css">
<?php include('header.php');?>

<div class="container">
    <div class="row">
        <div class="col-md-7 ">
            <div class="panel panel-default">
                <div class="panel-heading">
                  <a style="float: right" href="logout.php">class="fa fa-sign-out">  Logout</a>
                 <center> <h4 >User Profile</h4></center>

               </div>
                    <div class="panel-body">

                        <div class="box box-info">

                            <div class="box-body">
                                <div class="col-sm-6">
                                    
                                        <form method="post" action="ajaxupload.php" enctype="multipart/form-data">
                                          <h5 style="color:#a66a6a;">Set your profile picture</h5>
                                          <input type="file" name="Filename"> 
                                          <input type="hidden" name="Description"  value="<?php echo $row ['user_name']; ?>" />
                                          <br/>
                                          <input TYPE="submit" name="upload" value="Submit" class="btn1" />
                                        </form>

   
                                    

              
                                </div>
                                

            <div class="col-sm-6">
            <h4 style="color:#a66a6a;"><?php echo $row ['name']; ?></h4></span>
              <span><p><?php echo $row ['service']; ?></p></span>            
            </div>
            <div class="clearfix"></div>
            <hr style="margin:5px 0 5px 0;">
  
              
<div class="col-sm-5 col-xs-6 tital " >Name</div><div class="col-sm-7 col-xs-6 "><?php echo $row ['name']; ?></div>
     <div class="clearfix"></div>
<div class="bot-border"></div>

<div class="col-sm-5 col-xs-6 tital " >User Name</div><div class="col-sm-7"> <?php echo $row ['user_name']; ?></div>
  <div class="clearfix"></div>
<div class="bot-border"></div>

<div class="col-sm-5 col-xs-6 tital " >Password</div><div class="col-sm-7"> <?php echo $row ['password']; ?></div>
  <div class="clearfix"></div>
<div class="bot-border"></div>

<div class="col-sm-5 col-xs-6 tital " >email</div><div class="col-sm-7"><?php echo $row ['email']; ?></div>

  <div class="clearfix"></div>
<div class="bot-border"></div>

<div class="col-sm-5 col-xs-6 tital " >District</div><div class="col-sm-7"><?php echo $row ['district']; ?></div>

  <div class="clearfix"></div>
<div class="bot-border"></div>

<div class="col-sm-5 col-xs-6 tital " >City</div><div class="col-sm-7"><?php echo $row ['city']; ?></div>

 <div class="clearfix"></div>
<div class="bot-border"></div>

<div class="col-sm-5 col-xs-6 tital " >Address</div><div class="col-sm-7"><?php echo $row ['address']; ?></div>

<div class="clearfix"></div>
<div class="bot-border"></div>

<div class="col-sm-5 col-xs-6 tital " >Service</div><div class="col-sm-7"><?php echo $row ['service']; ?></div>
<div class="clearfix"></div>
<div class="bot-border"></div>

<div class="col-sm-5 col-xs-6 tital " >Years</div><div class="col-sm-7"><?php echo $row ['years']; ?></div>
<div class="clearfix"></div>
<div class="bot-border"></div>

<div class="col-sm-5 col-xs-6 tital " >Details</div><div class="col-sm-7"><?php echo $row ['details']; ?></div>

<center><a href="update_userinfo.php" class="btn" role="button">Update</a></center>


<?php
        }
        }
        ?>

<?php

 $sql="SELECT image_path FROM images where user_name = '".$_SESSION['username']."'";
 
      $result = $con->query($sql);

      $file_name=$row['file_name'];
      $image_path=$row['image_path'];//here you get path where you store image for user1

    ?>    
          
  <img src=<?php echo"$image_path";?> width=500 height=400> <br>      

これは、ddatabse に画像を保存する ajax アップロード ファイルです。

<?php
	$fileExistsFlag = 0; 
	$fileName = $_FILES['Filename']['name'];
	$link = mysqli_connect("localhost","root","","construction") or die("Error ".mysqli_error($link));
	/* 
	*	Checking whether the file already exists in the destination folder 
	*/
	$query = "SELECT file_name FROM images WHERE file_name='$fileName'";	
	$result = $link->query($query) or die("Error : ".mysqli_error($link));
	while($row = mysqli_fetch_array($result)) {
		if($row['filename'] == $fileName) {
			$fileExistsFlag = 1;
		}		
	}
	/*
	* 	If file is not present in the destination folder
	*/
	if($fileExistsFlag == 0) { 
		$target = "uploads/";		
		$fileTarget = $target.$fileName;	
		$tempFileName = $_FILES["Filename"]["tmp_name"];
		$fileDescription = $_POST['Description'];	
		$result = move_uploaded_file($tempFileName,$fileTarget);
		/*
		*	If file was successfully uploaded in the destination folder
		*/
		if($result) { 
			echo "Your file <html>".$fileName."</html> has been successfully uploaded";		
			$query = "INSERT INTO images(image_path,file_name,user_name) VALUES ('$fileTarget','$fileName','$fileDescription')";
			$link->query($query) or die("Error : ".mysqli_error($link));			
		}
		else {			
			echo "Sorry !!! There was an error in uploading your file";			
		}
		mysqli_close($link);
	}
	/*
	* 	If file is already present in the destination folder
	*/
	else {
		echo "File <html>".$fileName."</html> already exists in your folder. Please rename the file and try again.";
		mysqli_close($link);
	}	
?>

解決策 1

を含むコードを表示しないという私のコメントに加えて、 img_src 変数。

HTML 属性値は引用符で囲む必要があり (ブラウザーは通常、引用符なしでも受け入れますが、有効な HTML ではありません)、スペースまたは予約文字が含まれている場合は引用符で囲む必要があります。

PHP
<?php echo '<img src="' . $image_path . '" width="500" height="400">'; ?>

[EDIT]

このコードはコピー アンド ペーストされているようで、期待どおりに動作しません。

PHP
$sql="SELECT image_path FROM images where user_name = '".$_SESSION['username']."'";

// Executing the query and assigning to $result
// OK so far but it misses checking for errors
$result = $con->query($sql);

// What is $row?
// The query result has been stored in $result!
// There is probably something missing like
//$row = $result->fetch_row(); 
// Finally, the above SQL query returns only one field: image_path
// So 'file_name' will never be present
$file_name=$row['file_name'];
$image_path=$row['image_path'];//here you get path where you store image for user1

上記がデータベースからパスを返すように修正されている場合 (そうでない場合はフィードバックを出力する場合)、そのパスを img タグは、ファイルが存在し、Web サーバーのドキュメント ルート (「https://www.codeproject.com/」で始まる) からの相対パス、または現在のページからの相対パスである場合に提供されます。
[/EDIT]

解決策 2

@Jochen さん、ご協力ありがとうございます。 あなたのコメントを検討した後、私は次のようにコードを作成しました

<?php

 $sql="SELECT * FROM images where user_name = '".$_SESSION['username']."'";
 $result = $con->query($sql);

    if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        ?>
      
      <?php echo '<img src="' . $row['image_path']. '" width="500" height="400">'; ?>

    
   <?php
        }
        }
        ?> 

コメント

タイトルとURLをコピーしました