
<?php 
  session_start();
  include"inc/config.php";
   $errors = array(); // initialize variables
  $id = ""; $category="";    $category_name= ""; $image= ""; $price_1= "";
  $id = 0; 
  $update = false;

  if (isset($_GET['edit'])) { 
    $id = $_GET['edit'];
  $record = mysqli_query($db, "SELECT * FROM products WHERE id= $id ");


    if (count($record) == 1 ) {
    $n = mysqli_fetch_array($record);
     $id = $n['id'];  $category = $n['category'];  $name = $n['name'];  $size = $n['size'];  $price = $n['price']; 
      $price_1 = $n['price_1'];  $description = $n['description'];  $quantity = $n['quantity'];   $image = $n['image']; 
    }

  }

if (isset($_POST['update'])) {
$id = $_POST['id']; $category = $_POST['category']; $name = $_POST['name']; $size = $_POST['size']; $price= $_POST['price'];
 $description= $_POST['description'];  $quantity= $_POST['quantity'];

 $image = $_FILES['image']['name'];
  
$target = "../product-images/".basename($image);


   $sql_a = "SELECT * FROM products WHERE image='$image' AND size = '$size'  ";

     
    $res_a = mysqli_query($db, $sql_a);
     

    if (mysqli_num_rows($res_a) > 0) {
      array_push($errors, "Sorry !a product with that image and product size arleady exist.");
    }

    else {
      
if (empty( $image)) { 

$sql_1 = "UPDATE products SET   category= '$category', name='$name', size= '$size', price= '$price',
 quantity  = '$quantity', description = '$description' WHERE id =$id";

}
else{ 

$sql = "UPDATE products SET image = '$image',  category= '$category', name='$name', size= '$size', price= '$price',
 quantity = '$quantity', description= '$description' WHERE id =$id";

 }

 if (( (move_uploaded_file($_FILES['image']['tmp_name'], $target)) AND ($db->query($sql) === TRUE)   OR    ($db->query($sql_1)) )) {
 echo "<script type= 'text/javascript'>alert('$name - Record  updated succesfully.');</script>";
 echo "<script type='text/javascript'>location.href = 'index.php?link=002products';</script>";
}
else{echo "<script type= 'text/javascript'>alert('Error!!! while updating [ $name ] record.');</script>";}

}
}

?>


<!DOCTYPE>
<html lang="en">
<head>
<title></title>
<meta charset="utf-8">
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">

<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
 body { font-family: helvetica, sans-serif; }
* {  box-sizing: border-box;}
body {  margin: 0;}

.top-aa{ padding:15px; text-align: left; width: 100%; font-size: x-small; color: #848482; margin-bottom: 1%;}

.wrapper{ background:whitesmoke;  width:100%; border: 0px solid #a1a1a1;  margin:auto; resize: none;     overflow:hidden; }

.column-a {  float: left;  width:18%;  padding: 0px; height: 90%; background:white ;border-top: 2px solid whitesmoke;  overflow-x: auto; scrollbar-width: thin; }

.column-b { float: left; width: 82%;  padding: 15px; overflow-x: auto; scrollbar-width: thin; height: 90%; }


/* Clear floats after the columns */
.row:after {
  content: "";
  display: table;
  clear: both;
}

@media screen and (max-width:600px) {
  .column-a { width: 100%; height: auto; }
  .column-b {  width: 100%; height:auto; } }

</style>
</head>
<body>


<div class="row">

<div class="wrapper">

  <nav> <?php include "header.php"; ?></nav>


  <div class="column-a"><?php include "menu.php"; ?>  </div>
  
  <div class="column-b">

<div class="top-aa">
<a href="index.php" style="color: black; text-decoration: none; font-weight: bold;"> Home </a>
<a href="index.php?link=002products" style="color: black; text-decoration: none; ">// Manage Products</a>
<a>// Edit product records</a>
</div>

<?php include ("products-edit.php"); ?>

</div>





<?php include ("footer.php"); ?>
</div>
</div>
</body>
</html>
