Click here to Skip to main content
15,887,371 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm trying to update employee details along with a picture. If I edit only the details, it updated and redirects to employee.php but when I update the image, it redirects me to code.php (where all my function codes are) with a blank page. im guessing it's something wrong with the code for the image update but i dont know which is it. What are the possibilities for this happening? Any suggestion?

What I have tried:

Here is my code: employee_edit.php
<div class="modal-body">
<?php

if(isset($_POST['emp_edit_btn'])) {
    $emp_edit_id= $_POST['emp_edit_id'];
    $query="SELECT * FROM employee WHERE eid='$emp_edit_id'";
    $query_run=mysqli_query($conn,$query);

    foreach($query_run as $rowEdit) {
?>
          
          
    <form action="code.php" method="POST" enctype="multipart/form-data">
        <input type="hidden" name="emp_update_id" value="<?php echo $rowEdit['eid']?>">

        <hr style="border-top: 1px solid #ccc; background: transparent;">
        <h5>Personal Details</h5>
        <hr style="border-top: 1px solid #ccc; background: transparent;">
        <div class="form-group">
            <label>Name</label>
            <input type="text" name="emp_edit_name" value="<?php echo $rowEdit['emp_name']?>" class="form-control">
        </div>
        <div class="form-group">
            <label>Date of Birth</label>
            <input type="date" name="emp_edit_dob" value="<?php echo $rowEdit['emp_dob']?>" class="form-control" >
        </div>


cont. employee_edit.php
<div class="form-group">
        Employee Image
                        
        
         

        
    </div> 
                      
    
    " height = 90px width = 90px >
    <br>
    <div> 
        Update
        <a href="employee.php" class="btn btn-danger">Cancel</a>
    </div> 




code.php:
if(isset($_POST['emp_updatebtn'])){
    $emp_update_id=$_POST['emp_update_id'];
    $emp_name=$_POST['emp_edit_name'];
    $emp_dob=$_POST['emp_edit_dob'];
    $emp_gender=$_POST['emp_edit_gender'];
    $emp_national=$_POST['emp_edit_national'];
    $emp_phone=$_POST['emp_edit_phone'];
    $emp_email=$_POST['emp_edit_email'];
    $emp_curr_add=$_POST['emp_edit_currAdd'];
    $emp_perm_add=$_POST['emp_edit_permAdd'];
    $s_id=$_POST['s_id_edit'];
    $c_id=$_POST['c_id_edit'];
    $emp_start=$_POST['emp_edit_start'];
    $emp_as=$_POST['emp_edit_empAs'];
    $emp_job_id=$_POST['emp_edit_jobID'];
    $emp_end=$_POST['emp_edit_end'];
    $emp_status=$_POST['emp_edit_status'];

    $emp_edit_conPerson=$_POST['emp_edit_conPerson'];
    $emp_edit_rlt=$_POST['emp_edit_rlt'];
    $emp_edit_emerPhone=$_POST['emp_edit_emerPhone'];
    $emp_edit_emerEmail=$_POST['emp_edit_emerEmail'];
    $emp_edit_bankName=$_POST['emp_edit_bankName'];
    $emp_edit_accName=$_POST['emp_edit_accName'];
    $emp_edit_accNum=$_POST['emp_edit_accNum'];

    $emp_img_old=$_POST['emp_img_old'];
    $emp_img_new=$_FILES['emp_img']['name'];

    if($emp_img_new!=''){
        $update_img= $_FILES['emp_img']['name'];
    } else {
        $update_img= $emp_img_old; 
    }

    if ($_FILES['emp_img']['name']!='') {
        if (file_exists("emp_upload/" . $_FILES['emp_img']['name'])) {
            $img=$_FILES['emp_img']['name'];
            $_SESSION['status']="img exists".$img;
            header("Location:employee.php");
        }
    } else {

        $query="UPDATE employee 
                    SET emp_name='$emp_name', 
                    emp_dob='$emp_dob', 
                    emp_gender='$emp_gender',
                    emp_national='$emp_national',
                    emp_phone='$emp_phone',
                    emp_email='$emp_email',
                    emp_curr_add='$emp_curr_add',
                    emp_perm_add='$emp_perm_add',
                    s_id='$s_id',
                    c_id='$c_id',
                    emp_start='$emp_start',
                    emp_as='$emp_as',
                    emp_job_id='$emp_job_id',
                    emp_end='$emp_end',
                    emp_status='$emp_status',
                    contact_person='$emp_edit_conPerson',
                    rlt='$emp_edit_rlt',
                    emer_phone='$emp_edit_emerPhone',
                    emer_email='$emp_edit_emerEmail',
                    bank_name='$emp_edit_bankName',
                    acc_name='$emp_edit_accName',
                    acc_num='$emp_edit_accNum',
                    emp_img='$update_img'  
                WHERE eid='$emp_update_id'";

        $query_run=mysqli_query($conn,$query);

        if($query_run){
            if($_FILES['emp_img']['name']!= '') {
                move_uploaded_file($_FILES['emp_img']['tmp_name'], "emp_upload/".$_FILES['emp_img']['name']);
                unlink ('emp_upload/'.$img_name_old );
            }

            $_SESSION['success']="Your data is Updated";
            header("Location:employee.php");
        } else{
            $_SESSION['status']="Your data is NOT Updated";
            header("Location:employee.php");

        }   
    }
}</div>
Posted
Updated 17-Dec-20 5:25am
v2

1 solution

PHP
$query="UPDATE employee
            SET emp_name='$emp_name',
            emp_dob='$emp_dob',
            emp_gender='$emp_gender',
            emp_national='$emp_national',
            emp_phone='$emp_phone',
            emp_email='$emp_email',
            emp_curr_add='$emp_curr_add',
            emp_perm_add='$emp_perm_add',
            s_id='$s_id',
            c_id='$c_id',
            emp_start='$emp_start',
            emp_as='$emp_as',
            emp_job_id='$emp_job_id',
            emp_end='$emp_end',
            emp_status='$emp_status',
            contact_person='$emp_edit_conPerson',
            rlt='$emp_edit_rlt',
            emer_phone='$emp_edit_emerPhone',
            emer_email='$emp_edit_emerEmail',
            bank_name='$emp_edit_bankName',
            acc_name='$emp_edit_accName',
            acc_num='$emp_edit_accNum',
            emp_img='$update_img'
        WHERE eid='$emp_update_id'";

Not necessary a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
How can I explain SQL injection without technical jargon? - Information Security Stack Exchange[^]
 
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