Hello Admins and Php Friends,

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 Daniweb that i always doing my best to get those codes but this time i think im lost.Please give me the codes to write on another page like Student_Home.php to make this uplaoded images display.
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.

An advise.. is better u rename the uploaded img with the student_id that way you can always refer to the img using the student_id
1. change the uploaded img name to student_id

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/".$student_id; //append student_id as filename
$userfile_tmp_name = $_FILES['LRCard']['tmp_name'];
if(move_uploaded_file($userfile_tmp_name, $archive_dir)){
/*
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());
}
}
  1. to display
    <?php print '<img src="LRCards/'.$student_id.$ext.'" />' ?>

How many images are allowed per student?

More than one?
you need to create an image database table

+-----+----------+---------+
+ id  + owner_id + img_url +
+-----+----------+---------+

one only?
follow the recommendation above.

Hello Friends,

I have modified slightly my codes according to your recommendation

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)){
                            /*
                                if image is successfully uploaded then imagename is stored in database.
                            */
        mysql_query("update student_information set image='$imagename' where student_id='$student_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:Admin_Home.php?flag=$flag");
        die();      
?>

The image can't upload now and it notifies me this

Parse error: syntax error, unexpected 'update' (T_STRING) in C:\xampp\htdocs\a\Admin_Add_Student_Handler.php on line 75.

this refers to line

mysql_query("update student_information set image='$imagename' where student_id='$student_id'", $link_id);

i change the LRCard to image and the destination folder LRCards to images.
Maybe i got wrong in modifying codes or something we missed.

Please advise.

line 11, should read

if(move_uploaded_file($userfile_tmp_name, $archive_dir)){

Hello Daniweb and veedeoo,

Thanks for that email me that your missing me in this site. :-)

I was working with this codes lately to upload the image successfully on the folder 'images'. And im back now cause the problem of uploading is solved but i cant still rename the file uploaded to student_id.

change this

if(move_uploaded_file($userfile_tmp_name, "$archive_dir))

to this

if(move_uploaded_file($userfile_tmp_name, $archive_dir ."/". $LRCardname.$ext))

another alternative is to do it like this.

if(move_uploaded_file($_FILES['image']['tmp_name'], $archive_dir ."/". $LRCardname.$ext))

still out of luck :-(
all the suggestions give same result.

copy, paste and save as inisettings.php

<?php

echo 'Maximum Execution Time : '. ini_get('max_execution_time').'<br/>';
echo 'Maximum Input Time : '. ini_get('max_input_time').'<br/>';
echo 'Upload Max File size : '. ini_get('upload_max_filesize'). '<br/>';
echo 'post_max_size : '. ini_get('post_max_size') .'<br/>';  

direct your browser to this file post the result on your response.

This is so far what i got veedeo 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 veedeo 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.

try this

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)){

@veedeoo,

sorry for the delayed response. this is what i got in inisettings.php

Maximum Execution Time : 30
Maximum Input Time : 60
Upload Max File size : 2M
post_max_size : 8M

@veedeoo,

in the codes you suggested above. the uploaded filename is change
to

._

Just the two characters,underscore and dot.

Upload is not working because your php.ini settings for the upload related directives are pretty low.

Can you copy, past, save as info.php

<?php

    phpinfo();

direct your browser to this page. On this page, look for the Server API directives or value.

is it fast CGI?
or is it Apache 2.0 Handler?

Also, look for the value of this

**Loaded Configuration File **

Give me those values and I will tell you what to do next.

@veedeoo,

the upload is working but the file is not rename to it unique student_id.

Server API Apache 2.0 Handler
Loaded Configuration File C:\xampp\php\php.ini

Thanks for the focus veedeoo, i'm learning a lot from you.
Those are the infos you need.

hello veedeoo,

i come to realize that this is still on registration page (the page where students filled-in their information and upload their picture) i suppose the $student_id is not yet created. I think the student_id will be created after the student is registered. Just a thought.

i have come to think to make the registration page as simple as possible so i decided to add the uploading image feature on the student_edit.php and not in the regstration.php.

Please stand by veedeoo, i may need your help later after il write the codes in student_edit.php.

thank you very much

hello veedeeo,

i got new codes now that uploads the image of registered student with unique student_id. it is called from Student_Edit_handler.php

    $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 i am facing now is the image wont upload and it flags the error.
i know this has been a lot but i need your help to modify my codes.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.