Click here to Skip to main content
15,881,089 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I developed a user profile where he can upload his picture at his first login. His profile picture saves to a folder and image path saves in the database. Now I want to fetch that image using his user_name, which I have make it unique. when user save his profile picture, through a hidden submit button, I save his user_name in the database.
But this fetching image code does not work

What I have tried:

This is my complete welcome. php file where user log change his profile picture and see his profile details.
<?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>      

This is ajax upload file which saves image in the 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);
	}	
?>
Posted
Updated 26-Jul-18 2:05am
v3
Comments
Jochen Arndt 26-Jul-18 6:40am    
Your error message is
"Undefined variable: image_src ..."
but your posted code did not contain this variable.

Check the full error message. It contains the file name and the line number where that undefined variable is used. Then check the source code in the mentioned file.

If you are still stuck, update your question with the full error message and the relevant code.
dush93 26-Jul-18 6:53am    
@Jochen I updated the question
Jochen Arndt 26-Jul-18 7:02am    
See my solution I have posted meanwhile.

In addition to my comment about not showing any code containing the img_src variable.

HTML attribute values should be quoted (browsers accept them usually also without quotes but it is not valid HTML) and must be quoted if they contain spaces or reserved characters:
PHP
<?php echo '<img src="' . $image_path . '" width="500" height="400">'; ?>

[EDIT]
This code seems to be copied and pasted together and won't work as expected:
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

If the above has been fixed to return a path from the database (or print some feedback if not), the path can be used for the img tag provided that the files exists and is a path relative to the web server's document root (beginning with '/') or relative to the current page.
[/EDIT]
 
Share this answer
 
v2
Comments
dush93 26-Jul-18 7:10am    
Thank you for your consideration,But it does not fetch the image.
Jochen Arndt 26-Jul-18 7:19am    
It does not fetch the image but would show it when $image_path is a valid path that can be accessed by the web server.

Your code would not do that even when the path is valid.

However, put all the code into one PHP block (that is put the echo command just after querying from the database).

You should also check if the query was successful.

Anything else requires the full error message (it is still unclear where the error occurs and what the corresponding code is doing).

Also, your code looks not valid at all. I will update my answer.
dush93 26-Jul-18 7:35am    
@jochen Thank you very much for your comments. I updated the code with completed versions.Please have a lookat
Jochen Arndt 26-Jul-18 7:59am    
But you still did not give us a detailed error description (what exact error occured where). "Does not work" does not help us to help to you.

But even if we have a detailed description, we still have to guess because we have no access to your database.

That is what I had done in my updated answer. But you can simply check it step by step:
Is the query successful?
If not, is there an error message?
If yes, what is the value of the 'image path'?
Is that a valid path for the web server?
Does the file exist?

The above process is called debugging. But such can be only done by you. We can only give some tips / hints, and find obviously wrong code.
Thank you very much for your help @Jochen. After considering your comments I made the code as following
I
<?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
        }
        }
        ?> 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900