  <?php 
     require('top.php');
     $categories='';
     $msg='';
     $name='';
     $image='';
     $image_required='required';
     if (isset($_GET['id']) && $_GET['id']!='') {
      $image_required='';
      $id=mysqli_real_escape_string($con,$_POST['id']);
     $res=mysqli_query("select * from product where id='$id'");
     $check=mysqli_num_rows($res);
     if($check>0){
     $row=mysqli_fetch_assoc($res);
     $categories=$row['categories'];
     $name=$row['name'];
          } else{
               header('location:product.php');
        die();
      }
               }
  
     if(isset($_POST['submit'])){

            $categories=mysqli_real_escape_string($con,$_POST['categories']);
           $name=mysqli_real_escape_string($con,$_POST['name']);
           

             $res=mysqli_query($con,"select * from product where name='$name'");
             $check=mysqli_num_rows($res);
             if($check>0){
                  if (isset($_GET['id']) && $_GET['id']!='') {
                 $getData=mysqli_fetch_assoc($res); 
                 if($id==$getData['id']){

                 }else{
                  $msg="Product already exist.";
                 }                   

                         $msg="Product already exist.";
             }
           }
            

             if ($msg=='') {
                if (isset($_GET['id']) && $_GET['id']!=''){
                     if($_FILES['image']['name']!=''){
                        $image=rand(1111111,9999999).'_'.$_FILES['image']['name'];
               move_uploaded_file($_FILES['image']['tmp_name'],'../admin/images/'.$image);
                $update_sql="update product set categories='$categories',name='$name',image='$image' where id='$id'";
                     }
                     else{
                      $update_sql="update product set categories='$categories',name='$name', where id='$id'";
                     }
                  mysqli_query($con,$update_sql);
            }
            else{
             $image=rand(1111111,9999999).'_'.$_FILES['image']['name'];
               move_uploaded_file($_FILES['image']['tmp_name'],'../admin/images/'.$image);
                 mysqli_query($con,"insert into product(categories,name,status,image) values('$categories','$name','1','$image')");
              }
        echo " <script> window.location='product.php'</script>";
        die();
             }

}
   
    ?>
     <div class="content pb-0">
<div class="animated fadeIn">
 
                        <div class="card">
                            <div class="card-header"><strong>Product</strong><small> Form</small></div>
                            <form method="POST" enctype="multipart/form-data">
                            <div class="card-body card-block">
                                <div class="form-group"><label for="categories" class=" form-control-label">Categories</label>
                                  <select class="form-control" name="categories">
                                    <option>Select Category</option>
                                    <?php 
                                   $res=mysqli_query($con,"select id,categories from categories order by categories asc");
                                   while ($row=mysqli_fetch_assoc($res)) {
                                           if($row['id']==$categories){

                                           echo "<option selected value=".$row['id'].">" .$row['categories'].
                                     "</option>";
                                           }else{
                                           echo "<option value=".$row['id'].">" .$row['categories'].
                                     "</option>";
                                           }
                                   }
                                    ?>

                                  </select>
                                   <div class="form-group"><label for="categories" class=" form-control-label">Product Name</label>
                                  <input type="text"  name="name" placeholder="Enter Product  name" class="form-control" required value="<?php echo $name ?>" ></div>
                                   <div class="form-group"><label for="categories" class=" form-control-label">Image</label>
                                  <input type="file"  name="image" class="form-control" <?php echo $image_required  ?> ></div>
                               
                                <button id="payment-button" type="submit" name="submit" class="btn-btn-lg btn-info btn-block">
                                <span id="payment-button-amount">Submit</span>
                              </button>
                              <div class="field_error">  <?php echo $msg ?> </div>
                            </div>
                            </form>
                        </div>
                    </div>
 <?php 
     require('footer.php');
     
  ?>
                     