<?php  //require_once("../includes/initialize.php");
$connect = mysqli_connect("localhost", "oscotech_oscopt", "oscopt", "oscotech_portal");
if(isset($_POST["submit"]))
{
 if($_FILES['file']['name'])
 {
  $filename = explode(".", $_FILES['file']['name']);
  if($filename[1] == 'csv')
  {
   $handle = fopen($_FILES['file']['tmp_name'], "r");
   while($data = fgetcsv($handle))
   {
      $item1 = mysqli_real_escape_string($connect, $data[0]);  
      $item2 = mysqli_real_escape_string($connect, $data[1]);
      $item3 = mysqli_real_escape_string($connect, $data[2]);  
      $item4 = mysqli_real_escape_string($connect, $data[3]);
      $item5 = mysqli_real_escape_string($connect, $data[4]);  
      $item6 = mysqli_real_escape_string($connect, $data[5]);
      $item7 = mysqli_real_escape_string($connect, $data[6]);  
      $item8 = mysqli_real_escape_string($connect, $data[7]);
    //   $item9 = mysqli_real_escape_string($connect, $data[8]);
        $item9 = date('Y-m-d');

      $query = "INSERT into admitted_student(formno,surname,othername,email,department,sex,level,session,admitted_date) values('$item1','$item2','$item3','$item4','$item5','$item6','$item7','$item8','$item9')";
      mysqli_query($connect, $query);
      
      
   }
   fclose($handle);
   echo "<script>alert('Import done');</script>";
  }
 }
}
// SELECT formno, COUNT(formno) FROM `admission_status` GROUP BY formno HAVING COUNT(formno) > 1
// DELETE t1 FROM admission_status t1 INNER JOIN admission_status t2 WHERE t1.id > t2.id AND t1.formno = t2.formno
?>  
<!DOCTYPE html>  
<html>  
 <head>  
  <title>Admin Upload</title>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>  
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
 </head>  
 <body>  
  <h3 align="center">Upload CSV File Only</h3><br />
  <form method="post" enctype="multipart/form-data">
   <div align="center">  
    <label>Select CSV File:</label>
    <input type="file" name="file" />
    <br />
    <div class="table-responsive">
        <div class="col-md-4">
            <table id="fo_table" class="table table-striped table-bordered">
                    <thead>
                          <tr>
                              <th>S/N</th>  
                              <th>FORM NO</th>
                              <th>SURNAME</th>     
                              <th>OTHER NAMES</th>
                              <th>DEPARTMENT</th>
                              <th>SEX</th>
                              <th>LEVEL</th>
                              <th>SESSION</th>
                              <th>DATE/TIME</th>                         
                          </tr>
              </thead>
                    <?php  
                //   echo fetch_data();  
                    ?>  
            </table>
        </div>
    </div>
    <input type="submit" name="submit" value="Import" class="btn btn-info" />
   </div>
  </form>
 </body>  
</html>