<?php

$stock_id = $_GET['stock_id']; 
$ext = $_POST['ext'];
$int = $_POST['int'];

$mysqli = new mysqli("localhost","root","","inspection");

if ($mysqli -> connect_errno) {
  echo "Failed to connect to MySQL: " . $mysqli -> connect_error;
  exit();
}else{

  if(isset($ext) &&isset($int) ){
    $sqlExt="INSERT INTO `exterior_auction_grade`(`id`, `grade`, `stock_id`) VALUES (DEFAULT,'$ext',$stock_id)";
    $sqlInt="INSERT INTO `interior_auction_grade`(`id`, `grade`, `stock_id`) VALUES (DEFAULT,'$int',$stock_id)";
    $resultExt = $mysqli->query($sqlExt);
    $resultInt = $mysqli->query($sqlInt);
    if( $resultExt === TRUE AND $resultInt === TRUE){

     
      $string = str_replace('data:image/png;base64,', '', $_POST['img']);
      $data = $_POST['img'];
      list($type, $data) = explode(';', $data);
      list(, $data)      = explode(',', $data);
      $data = base64_decode($data); 
      $time = time();
      if (!file_exists('images')) {
        mkdir('images', 0777, true);
      }
      file_put_contents('images/'.$stock_id.'.png',$data);
      $file ='images/'.$stock_id.'.png';
      $sqlExt="INSERT INTO `inspection_pictures`(`id`, `stock`, `img`) VALUES (DEFAULT,'$stock_id','$file')";
      
      $resultExt = $mysqli->query($sqlExt);
      echo "Record updated successfully";

    }else{
         echo "Error updating record: " . $mysqli->error;
    }
  }else{
    echo "Invalid paramters";
  }



}


?>
