<?php
session_start();
include 'dbconn.php';
$candName=$_POST["candName"];
$candN=$_POST["candN"];
$candIC=$_POST["candIC"];
$candCourse=$_POST["candCourse"];
$candPost=$_POST["candPost"];
$candMotto=$_POST["motto"];
echo $candMotto;
$q = "select * from tblcandidate WHERE candIC='$candIC'";
$query = mysqli_query($conn,$q);
$rowcount=mysqli_num_rows($query);

if($rowcount == 0){
    
  
    if(isset($_FILES['image'])){
      $errors= array();
      $file_name = $_FILES['image']['name'];
      $file_size =$_FILES['image']['size'];
      $file_tmp =$_FILES['image']['tmp_name'];
      $file_type=$_FILES['image']['type'];
      $file_ext=strtolower(end(explode('.',$_FILES['image']['name'])));
      
      $expensions= array("jpeg","jpg","png","pdf","JPG");
      
      if(in_array($file_ext,$expensions)=== false){
         $errors[]="extension not allowed, please choose a JPEG or PNG file.";
      }
       if($file_size > 20097152){
         $errors[]='File size must be excately 2 MB';
      }
      
       if (file_exists("image/".$file_name)) {
              echo "The file already exists and will be overwritte";
           }
      
      
        if(empty($errors)==true){
        $result=move_uploaded_file($file_tmp,"image/".$file_name);
        $path = "image/".$file_name;
                if($result){
                 $insert ="INSERT into tblcandidate (candName,candN, candIC, candPost, candCourse,image,motto) VALUES ('$candName', '$candN', '$candIC', '$candPost', '$candCourse','$path','$candMotto')";
                 $query1= mysqli_query($conn,$insert);
                  if($query1){
                    echo "<script>
                    alert('Data calon telah berjaya dimasukkan ke database');
                   window.location.replace('https://kvstechub.my/evote/admin.php');
                    </script>";
                            }
                            else{
                                echo "fail to insert the data";
                            }
                }
         
          }
          else{
             print_r($errors);
          }
   }
  
   }

?>
