<?php
session_start();
require_once('mysqli_con.php');
$errors = array();
?>


<style>
.error {
  width: 92%;
  margin: 0px auto;
  padding: 10px;
  border: 1px solid #a94442;
  color: #a94442;
  background: 5px;
  text-align: left;
}
</style>

<style>
.success {
  width: 92%;
  margin: 0px auto;
  padding: 10px;
  border: 1px solid #a94442;
  color: #a94442;
  background: 5px;
  text-align: left;
}
</style>

<?php
if(isset($_POST['Register'])){
$namef = $_POST["fname"];
$namel = $_POST["lname"];
$mail = $_POST["email"];
$pass = $_POST["pw"];
$add = $_POST["address"];
$fon = $_POST["phone"];
$passw = $_POST["pw2"];
$imgData =addslashes(file_get_contents($_FILES['image']['tmp_name']));
$items_id = $_SESSION['items_id'];


$sqlCheck = "SELECT * FROM buyer WHERE email = '$mail' LIMIT  1";
$result = mysqli_query($conn, $sqlCheck);
$existed = mysqli_fetch_assoc($result);

     if($existed ['email'] === $mail){
       array_push($errors, "Email already exist. Please try another email. ");

?>
       <meta http-equiv="refresh" content="2;URL=register.php" />

<?php
}else if ($pass != $passw) {

        array_push($errors, "The two password do not match. Please try again.");
?>
        <br>
        <meta http-equiv="refresh" content="2;URL=register.php" />
  <?php   }


  if(count($errors) > 0){
  ?>

  <div class = "error">

  <?php
  foreach ($errors as $errors)
  echo $errors; ?>
  </div>

  <meta http-equiv="refresh" content="2;URL=register.php" />
  <?php
}

else if(count($errors) == 0){

$sql = "INSERT INTO buyer (first_name, last_name, email, phone, password, address, image, items_id)
VALUES ('$namef','$namel','$mail','$fon','$pass','$add','$imgData', '$items_id' )";
mysqli_query ($conn,$sql);
?>
<div class = "success">
<?php
echo "Successful Registered ";
?>
</div>
    <meta http-equiv="refresh" content="1; URL='login.php'" >
  <?php } else { ?>
  <?php
    echo "Please Register Again"?>
    <meta http-equiv="refresh" content="1; URL='login.php'" > <?php
  }
}



 ?>
