Jump to content

Displaying The Specific Uploaded Image On Student Home Page


phpNOvice

Recommended Posts

Hello Admins and Php Freaks,

 

I have manage to upload the image using this codes

$student_id = $_POST['student_id'];
	$first_name = $_POST['first_name'];
	$last_name = $_POST['last_name'];
	$gender = $_POST['gender'];
	$date_of_birth = date("Y-m-d",strtotime($_POST['date_of_birth']));
	$contact_no = $_POST['contact_no'];
	$grade	= $_POST['grade'];
	$section = $_POST['section'];
	$LRN = $_POST['LRN'];
	$email1 = $_POST['email1'];
	$email2 = $_POST['email2'];
	$address = $_POST['address'];
	$description = $_POST['description'];
	$LRCardname= "";
	$LRCard2name= "";		
	
	
  	$query = "UPDATE student_information SET first_name='$first_name',last_name='$last_name',";    
    $query .= "gender='$gender',date_of_birth='$date_of_birth',contact_no='$contact_no',grade='$grade',section='$section',";
    $query .= "LRN='$LRN',email1='$email1',email2='$email2',address='$address',description='$description'";  	
	$query .= " WHERE student_id='$student_id'";      
	$result = mysql_query($query, $link_id);
	if(mysql_error() != null){
		die(mysql_error());
	}
		
	/*
			This block shows the start codes for the upload of the Learner Report Card.
	*/
	
	if($result){
			if($_FILES['LRCard']['name'] != ""){
				$filename = $_FILES['LRCard']['name'];
				$ext = strrchr($filename,".");
				$LRCardname = $student_id;
				$LRCardname .="_". $filename; 
				if($ext ==".jpg" || $ext ==".jpeg" || $ext ==".JPG" || $ext ==".JPEG" || $ext ==".gif" || $ext ==".GIF"){
					$size = $_FILES['LRCard']['size'];
					if($size > 0 && $size < 5000000){
						$archive_dir = "LRCards";
						$userfile_tmp_name = $_FILES['LRCard']['tmp_name'];
						if(move_uploaded_file($userfile_tmp_name, "$archive_dir/$LRCardname")){
							/*
								if LRC is successfully uploaded then LRC is stored in database.
							*/
							mysql_query("update student_information set LRCard='$LRCardname' where student_id='$student_id'", $link_id); 
							$flag = "success"; 
							if(mysql_error()!=null){
								die(mysql_error());
							}

						}
						else{
							if(file_exists('LRCard/' . $LRCardname)) {
								unlink('LRCards/' . $LRCardname); 
							}
							rollbackData();
						}
					}
					else{
						if(file_exists('LRCards/' . $LRCardname)) {
							unlink('LRCard/' . $LRCardname); 
						}
						rollbackData();
						die("You can upload LRCard of 5 MB size only. Please, try again.");
					}
				}
				else{
					if(file_exists('LRCards/' . $LRCardname)) {
						unlink('LRCards/' . $LRCardname); 
					}
					rollbackData();
					die("You can upload LRCard of .jpg, .jpeg, .gif extensions only. Please, try again. ");
				}
			} 	
		}
		else{
			$flag="error";
		}
		if($flag == "success"){
			mysql_query(" COMMIT ");
			$flag="success";
			if(mysql_error() != null){
				die(mysql_error());
			}
	    }

Now i want to display this uploaded image with respect to a specific student with auto incremented unique number. You know Admin Ben that i always doing my best to get those codes but this time i think im lost.

I want this image to be displayed on another page. Please help me this time with the codes and ill do the rest.

Please and another please. I always beleive in your expertise in CRUDE.

Link to comment
Share on other sites

What have you done so far?

The process should be pretty simple. On the page where you want the logo to appear, you need to get the image name for the image from the database, adding on whatever path you need in order for the image to link accurately ("path/to/image.jpg"), then, within the page, you add an image using a regular <img> tag, and insert the correct image path retrieved from the database using PHP.

Link to comment
Share on other sites

This is so far what i got Ben and i failed to rename the uploaded file to its $student_id.

if($_FILES['image']['name'] != ""){
				$filename = $_FILES['image']['name'];
				$ext = strrchr($filename,".");
				$imagename = $student_id;
				$imagename .="_". $filename; 
				if($ext ==".jpg" || $ext ==".jpeg" || $ext ==".JPG" || $ext ==".JPEG" || $ext ==".gif" || $ext ==".GIF"){
					$size = $_FILES['image']['size'];
					if($size > 0 && $size < 1000000){
						$archive_dir = "images";
						$userfile_tmp_name = $_FILES['image']['tmp_name'];
						if(move_uploaded_file($userfile_tmp_name, "$archive_dir/$imagename")){

The file has been succesfully uploaded Ben but help me to insert the codes which to rename its file name to its student_id so i can display it to another page smoothly.

Link to comment
Share on other sites

Sorry -- I haven't been around here much recently. I'm just about to move cross country (west coast USA to the east coast), so I've been pretty busy/preoccupied.

 

What does your current code do? Are you getting errors? Are you able to upload successfully? If so, what are the files named once the upload process completes?

Link to comment
Share on other sites

Sorry to bother you Ben, There has been slightly changes since i am also working in solving the scenario. here is now my current codes.

if($_FILES['image']['name'] != ""){
				$filename = $_FILES['image']['name'];
				$ext = strrchr($filename,".");
				$imagename = $student_id;
				$imagename .="_". $filename; 
				if($ext ==".jpg" || $ext ==".jpeg" || $ext ==".JPG" || $ext ==".JPEG" || $ext ==".gif" || $ext ==".GIF"){
					$size = $_FILES['image']['size'];
					if($size > 0 && $size < 1000000){
						$archive_dir = "images";
						$userfile_tmp_name = $_FILES['image']['tmp_name'];
						if(move_uploaded_file($userfile_tmp_name, $archive_dir .'/'. $student_id .'_'. $imagename .'.'. $ext)){

this codes can upload an image.

there is no error.

the image is uploaded succesfully.

Example if the uplaoded file has a file name pic.jpg,,after it has been succesfully upload it is rename to

"_pic.jpg"

without the quotes just underscore and the file name itself.

What i wanted is to rename it to its unique student_id number so i can display it smoothly to another page.

Link to comment
Share on other sites

This is the line that sets the final file name, which is currently the student id, and underscore, and the image name.

if(move_uploaded_file($userfile_tmp_name, $archive_dir .'/'. $student_id .'_'. $imagename .'.'. $ext)){

If you want to name the image just the student id, you would need to remove the 

'_'. $imagename .

portion. If the filename isn't being saved with the student id currently, you need to make sure that the $student_id value is set. Who is actually doing the uploading? The student themselves? An admin user? If it's the student themselves, do you set a session variable that holds their student ID when they log in? If so, you can set $student_id to the value of that session variable.

Link to comment
Share on other sites

Yes Ben,

 

I understand your codes in fact that is what i am using. Actually, this is still on the process when the student register themselves-Registration_handler.php'. I suppose in this instance the student_id is not yet created.

 

This is the registration page where the student filled-up information including the uploading of their image. 

Link to comment
Share on other sites

OK. So if the user's ID isn't created yet, that's going to be the first step. You'll need to:

 

-- make sure that the user's information is completely valid

-- make sure the user's image is a valid type/size (assuming that the registration form includes both the user's information and an image upload

-- insert the user's information into the database

-- with the info inserted, use http://php.net/manual/en/mysqli.insert-id.php to get the last used id (the student's id)

-- upload the image

 

Personally, my suggestion would be to keep the registration process as short and as simple as possible, and save any non-essential information, such as a profile photo, for a "Edit Account" or "Edit Profile" page once they have logged in. 

Link to comment
Share on other sites

ok got it ben,,

 

i think i would accept your suggestion to keep registration process simple, that is always what im thinking all night long, thank you for confirming it. i'll work the codes now and put the adding of image on the Update profile.

 

Please stand by Ben, i may need your help after this ;-)

Link to comment
Share on other sites

Hi Ben, 

 

i got new codes now that uploads the image of registered student with unique student_id. it is called Student_Edit_handler. This was in response to the idea you suggested.

$student_id = $_POST['student_id'];
    $first_name = $_POST['first_name'];
    $last_name = $_POST['last_name'];
    $gender = $_POST['gender'];
    $date_of_birth = date("Y-m-d",strtotime($_POST['date_of_birth']));
    $contact_no = $_POST['contact_no'];
    $grade = $_POST['grade'];
    $section = $_POST['section'];
    $LRN = $_POST['LRN'];
    $email1 = $_POST['email1'];
    $email2 = $_POST['email2'];
    $address = $_POST['address'];
    $description = $_POST['description'];
    $imagename = "";
    $flag= "";
        mysql_query("SET AUTOCOMMIT = 0 ");
        if(mysql_error() != null){
            die(mysql_error());
        }
    $query = "UPDATE student_information SET first_name='$first_name',last_name='$last_name',";    
    $query .= "gender='$gender',date_of_birth='$date_of_birth',contact_no='$contact_no',grade='$grade',section='$section',";
    $query .= "LRN='$LRN',email1='$email1',email2='$email2',address='$address',description='$description'";     
    $query .= " WHERE student_id='{$_SESSION['user_id']}'";      
    $result = mysql_query($query, $link_id);
    if(mysql_error() != null){
        die(mysql_error());
        }
        /*
            This block shows the start codes for the upload of the Learner's image
        */
 if($_FILES['image']['name'] != ""){
                $filename = $_FILES['image']['name'];
                $ext = strrchr($filename,".");
                $imagename = $student_id;
                $imagename .="_". $filename; 
                if($ext ==".jpg" || $ext ==".jpeg" || $ext ==".JPG" || $ext ==".JPEG" || $ext ==".gif" || $ext ==".GIF"){
                    $size = $_FILES['image']['size'];
                    if($size > 0 && $size < 1000000){
                        $archive_dir = "images";
                        $userfile_tmp_name = $_FILES['image']['tmp_name'];
                        if(move_uploaded_file($userfile_tmp_name, $archive_dir .'/'. $student_id .'_'. $imagename .'.'. $ext)){
                            /*
                                if image is successfully uploaded then imagename is stored in database.
                            */
                            mysql_query("update student_information set image='$imagename' where student_id='{$_SESSION['user_id']}'", $link_id); 
                            $flag = "success"; 
                            if(mysql_error()!=null){
                                die(mysql_error());
                            }
                        }else{
                            if(file_exists('images/' . $imagename)) {
                                unlink('images/' . $imagename); 
                            }
                            rollbackData();
                        }
                    }else{
                        if(file_exists('images/' . $imagename)) {
                            unlink('images/' . $imagename); 
                        }
                        rollbackData();
                        die("You can upload image of 1 MB size only. Please, try again.");
                    }
                }else{
                    if(file_exists('images/' . $imagename)) {
                        unlink('images/' . $imagename); 
                    }
                    rollbackData();
                    die("You can upload images of .jpg, .jpeg, .gif extensions only. Please, try again. ");
                }
                }else{
            $flag="error";
    }
        if($flag == "success"){
            mysql_query(" COMMIT ");
            $flag="success";
            if(mysql_error() != null){
                die(mysql_error());
            }
            }
        header("location:Student_Edit.php?flag=$flag");
        die();      
?>

the problem left now is the image wont upload and it flags the error.

i know this has been a lot but i need you to help me modify these codes.

Link to comment
Share on other sites

i have writen flags on theStudent_Edit.php.

<?php  if(!empty($_GET['flag']) && $_GET['flag'] == "success") { ?>   
		<span class="stylered style1"><span class="style5">Learner Information updated successfully.</span></span>  
        <?php } else if(!empty($_GET['flag']) && $_GET['flag'] == "error") { ?>
		<span class="stylered style3 style5"><span class="style1">Error while updating Learner Information. Please try again</span></span> 
        <?php }  ?>	</td>

the specific error im getting is the flag=error which is 

"Error while updating Learner Information. Please try again."

The reason im pasting almost the whole codes.

Link to comment
Share on other sites

When I look through your code, it looks like the only reason you are getting this error is if this returns false:

if($_FILES['image']['name'] != "")

So the next step would be to figure out if the $_FILES array is completely empty, and if so, resolve the error. This is a pretty good overview of the essentials regarding what is necessary in order to upload a file: http://www.w3schools.com/php/php_file_upload.asp

 

Are you sure you are using the proper 

enctype="multipart/form-data"

on your form? That is required to upload a file. Is your input for your file named correctly? For example, your input has to be named "image" in order for this to work properly.

Link to comment
Share on other sites

These are my codes in the html

<tr class="stylesmall">
		  <td>Update Learner Image: </td>
		  <td colspan="3"><input type="file" name="image">
		           <span class="style3"> <span class="stylesmall">jpg or gif file only</span></span></td>
		</tr>
        <tr>
          <td colspan="4"><span class="stylered style6"><span class="style3">*</span> <em>fields are compulsory</em></span></td>
        </tr>
		
        <tr>
          <td colspan="4" align="center"><input name="update" type="submit" id="update" value="Update">

i read the link you gave me and tried to modify some codes but then it end up same error.

Link to comment
Share on other sites

  • 3 weeks later...

Hello Ben, i have solved the flag part but still the problem of uploading image is there even i have followed your advise in enctype="multipart/form-data" 

which i have missed also last time. I got brand new codes now and best thing is i got WARNING displayed. Kindly help me this out.

<?php
error_reporting(E_ALL | E_NOTICE);
ini_set('display_errors', '1');
	session_start();
	$session_id = $_SESSION['user_id'];
	if($session_id == null){
	   header("location:Student_Edit.php");
	   die();
	}
	include 'Connect.php';
	$flag = "";
	$student_id = $_POST['student_id'];
	$first_name = $_POST['first_name'];
	$last_name = $_POST['last_name'];
	$gender = $_POST['gender'];
	$date_of_birth = date("Y-m-d",strtotime($_POST['date_of_birth']));
	$contact_no = $_POST['contact_no'];
	$grade	= $_POST['grade'];
	$section = $_POST['section'];
	$LRN = $_POST['LRN'];
	$email1 = $_POST['email1'];
	$email2 = $_POST['email2'];
	$address = $_POST['address'];
	$description = $_POST['description'];
	$imagename = "";
	
  	$query = "UPDATE student_information SET first_name='$first_name',last_name='$last_name',";    
    $query .= "gender='$gender',date_of_birth='$date_of_birth',contact_no='$contact_no',grade='$grade',section='$section',";
    $query .= "LRN='$LRN',email1='$email1',email2='$email2',address='$address',description='$description'";  	
	$query .= " WHERE student_id='{$_SESSION['user_id']}'";      
	$result = mysql_query($query, $link_id);
		if(mysql_error() != null){
		die(mysql_error());
	}
	if($_FILES['image']['name'] != ""){
                $filename = $_FILES['image']['name'];
                $ext = strrchr($filename,".");
                $imagename = $student_id;
                $imagename .="_". $filename; 
                if($ext ==".jpg" || $ext ==".jpeg" || $ext ==".JPG" || $ext ==".JPEG" || $ext ==".gif" || $ext ==".GIF"){
                    $size = $_FILES['image']['size'];
                    if($size > 0 && $size < 1000000){
                        $archive_dir = "images/".$student_id;
                        $userfile_tmp_name = $_FILES['image']['tmp_name'];
                        if(move_uploaded_file($userfile_tmp_name, $archive_dir)){
       mysql_query("update student_information set image='$imagename' where student_id='{$_SESSION['user_id']}'", $link_id); 
                            $flag = "success"; 
                            if(mysql_error()!=null){
                                die(mysql_error());
                            }
                        }
                        else{
                            if(file_exists('images/' . $imagename)) {
                                unlink('images/' . $imagename); 
                            }
                            rollbackData();
                        }
                    }
                    else{
                        if(file_exists('images/' . $imagename)) {
                            unlink('images/' . $imagename); 
                        }
                        rollbackData();
                        die("You can upload image of 1 MB size only. Please, try again.");
                    }
                }           
                else{
                    if(file_exists('images/' . $imagename)) {
                        unlink('images/' . $imagename); 
                    }
                    rollbackData();
                    die("You can upload images of .jpg, .jpeg, .gif extensions only. Please, try again. ");
                }
              }
			  if($result){ 
		$flag = "success";
	}
	else{
		 $flag = "error"; 
  	}
	header("location:Student_Edit.php?flag=$flag&student_id='{$_SESSION['user_id']}'");	  
?>     

and these are the 3 warnings displayed except for being deprecated maybe you can figure this out.

 

Warning: move_uploaded_file(images/<br /> <b>Notice</b>: Undefined variable: student_id in <b>C:\xampp\htdocs\a\Student_Edit.php</b> on line <b>112</b><br /> ;): failed to open stream: Invalid argument inC:\xampp\htdocs\a\Student_Edit_Handler.php on line 45

Warning: move_uploaded_file(): Unable to move 'C:\xampp\tmp\php87E3.tmp' to 'images/<br /> <b>Notice</b>: Undefined variable: student_id in <b>C:\xampp\htdocs\a\Student_Edit.php</b> on line <b>112</b><br /> ;' in C:\xampp\htdocs\a\Student_Edit_Handler.php on line 45

Fatal error: Call to undefined function rollbackData() in C:\xampp\htdocs\a\Student_Edit_Handler.php on line 56

 

Please advise

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...