<?php
$conn = mysqli_connect("localhost", "root", "", "p2");

if (isset($_POST["import"])) {
    
    $fileName = $_FILES["file"]["tmp_name"];
    
    if ($_FILES["file"]["size"] > 0) {
        
        

        echo "hello";
        $file = fopen($fileName, "r");
        
        while (($column = fgetcsv($file, 10000, ",")) !== FALSE) {

            $sqlInsert = "INSERT into records(emp_id,lname,fname,gender,contact,email,dob,jdate,address,city,zip,country,emp_type,division,basic_pay,username,password)
                   values ('" . $column[0] . "','" . $column[1] . " ','" . $column[2] . "','" . $column[3] . "','" . $column[4] . "','" . $column[5] . "','" . $column[6] . "','" . $column[7] . "','" . $column[8] . "','" . $column[9] . "','" . $column[10] . "','" . $column[11] . "','" . $column[12] . "','" . $column[13] . "','" . $column[14] . "','" . $column[15] . "','" . $column[16] . "')";

            $result = mysqli_query($conn, $sqlInsert);
            
            if (! empty($result)) {
                $type = "success";
                $message = "CSV Data Imported into the Database";

            } else {
                $type = "error";
                $message = "Problem in Importing CSV Data";
            }

        }
    }

    echo "hello";
}

?>

<body>
    <h2>WEBSTER TO ORANGE IMPORT</h2>
    
    <div id="response" class="<?php if(!empty($type)) { echo $type . " display-block"; } ?>"><?php if(!empty($message)) { echo $message; } ?></div>
    <div class="outer-scontainer">
        <div class="row">

            <form class="form-horizontal" action="" method="post"
                name="frmCSVImport" id="frmCSVImport" enctype="multipart/form-data">
                <div class="input-row">
                    <label class="col-md-4 control-label">Choose CSV
                        File</label> <input type="file" name="file"
                        id="file" accept=".csv">
                    <button type="submit" id="submit" name="import"
                        class="btn-submit">Import</button>
                    <br />
                </div>

            </form>

        </div>
               <?php
            $sqlSelect = "SELECT * FROM records";
            $result = mysqli_query($conn, $sqlSelect);
            
            if (mysqli_num_rows($result) > 0) {
                ?>
            <table id='userTable'>
            <thead>
                <tr>
                    <th>User ID</th>
                    <th>User Name</th>
                    <th>First Name</th>
                    <th>Last Name</th>
                    <th>Last Name</th>
                    <th>Last Name</th>
                    <th>Last Name</th>
                    <th>Last Name</th>
                    <th>Last Name</th>
                    <th>Last Name</th>
                    <th>Last Name</th>
                     <th>Last Name</th>
                      <th>Last Name</th>
                       <th>Last Name</th>
                        <th>Last Name</th>
                         <th>Last Name</th>
                          <th>Last Name</th>



                </tr>
            </thead>
<?php
                
                while ($row = mysqli_fetch_array($result)) {
                    ?>
                    
                <tbody>
                <tr>
                    <td><?php  echo $row['emp_id']; ?></td>
                    <td><?php  echo $row['lname']; ?></td>
                    <td><?php  echo $row['fname']; ?></td>
                    <td><?php  echo $row['gender']; ?></td>
                    z<td><?php  echo $row['contact']; ?></td>
                    <td><?php  echo $row['email']; ?></td>
                    <td><?php  echo $row['dob']; ?></td>
                <td><?php  echo $row['jdate']; ?></td>
                 <td><?php  echo $row['address']; ?></td>
                  <td><?php  echo $row['city']; ?></td>

 <td><?php  echo $row['zip']; ?></td>
 <td><?php  echo $row['country']; ?></td>
 <td><?php  echo $row['emp_type']; ?></td>
 <td><?php  echo $row['division']; ?></td>  
 <td><?php  echo $row['basic_pay']; ?></td>
  <td><?php  echo $row['username']; ?></td>  
   <td><?php  echo $row['password']; ?></td> 
  


                </tr>
                    <?php
                }
                ?>
                </tbody>
        </table>
        <?php } ?>
    </div>

</body>

</html>