Bagaimana cara mengambil gambar profil pengguna menggunakan nama_pengguna di PHP?

pemrograman

[ad_1]

Saya mengembangkan profil pengguna di mana dia dapat mengunggah fotonya saat login pertama. Gambar profilnya disimpan ke folder dan jalur gambar disimpan di database. Sekarang saya ingin mengambil gambar itu menggunakan nama_penggunanya, yang telah saya buat unik. ketika pengguna menyimpan gambar profilnya, melalui tombol kirim yang tersembunyi, saya menyimpan nama penggunanya di database.
Namun pengambilan kode gambar ini tidak berfungsi

Apa yang saya coba:

Ini adalah sambutan saya sepenuhnya. file php tempat log pengguna mengubah gambar profilnya dan melihat detail profilnya.

<?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>      

Ini adalah file unggah ajax yang menyimpan gambar di ddatabse

<?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);
	}	
?>

Solusi 1

Selain komentar saya tentang tidak menampilkan kode apa pun yang mengandung img_src variabel.

Nilai atribut HTML harus diberi tanda kutip (browser biasanya juga menerimanya tanpa tanda kutip tetapi ini bukan HTML yang valid) dan harus diberi tanda kutip jika mengandung spasi atau karakter yang dicadangkan:

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

[EDIT]

Kode ini sepertinya disalin dan ditempelkan dan tidak berfungsi seperti yang diharapkan:

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

Jika hal di atas telah diperbaiki untuk mengembalikan jalur dari database (atau mencetak umpan balik jika tidak), jalur tersebut dapat digunakan untuk img tag asalkan file tersebut ada dan merupakan jalur yang berhubungan dengan akar dokumen server web (dimulai dengan “https://www.codeproject.com/”) atau relatif terhadap halaman saat ini.
[/EDIT]

Solusi 2

Terima kasih banyak atas bantuan Anda @Jochen. Setelah mempertimbangkan komentar Anda, saya membuat kode sebagai berikut

SAYA

<?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
        }
        }
        ?> 

[ad_2]

コメント

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