<?php
require_once($_SERVER['DOCUMENT_ROOT'] . "/session.php");  

header("Content-Type: application/json");  //json을 사용하기 위해 필요한 구문  

isset($_REQUEST["mode"])  ? $mode = $_REQUEST["mode"] : $mode=""; 
isset($_REQUEST["num"])  ? $num = $_REQUEST["num"] : $num=""; 

$name=$_REQUEST["name"];			  
$part=$_REQUEST["part"];			  

$dateofentry=$_REQUEST["dateofentry"];
$referencedate=$_REQUEST["referencedate"];
$availableday=$_REQUEST["availableday"];			  
$comment=$_REQUEST["comment"];			  
$company=$_REQUEST["company"];			  
			  
require_once($_SERVER['DOCUMENT_ROOT'] . "/lib/mydb.php");
$pdo = db_connect();
     
 if ($mode=="modify"){      
     try{
        $sql = "select * from " . $DB . ".almember where num=?";  // get target record
        $stmh = $pdo->prepare($sql); 
        $stmh->bindValue(1,$num,PDO::PARAM_STR); 
        $stmh->execute(); 
        $row = $stmh->fetch(PDO::FETCH_ASSOC);
     } catch (PDOException $Exception) {
        $pdo->rollBack();
        print "오류: ".$Exception->getMessage();
     } 
       			  
     try{
		$pdo->beginTransaction();   
		$sql = "update " . $DB . ".almember set name=?, part=?, dateofentry=?, referencedate=?, availableday=?, comment=?, company=? ";
		$sql .= " where num=?  LIMIT 1";		

		$stmh = $pdo->prepare($sql); 
		$stmh->bindValue(1, $name, PDO::PARAM_STR);  
		$stmh->bindValue(2, $part, PDO::PARAM_STR);  
		$stmh->bindValue(3, $dateofentry, PDO::PARAM_STR);  
		$stmh->bindValue(4, $referencedate, PDO::PARAM_STR);  
		$stmh->bindValue(5, $availableday, PDO::PARAM_STR);  
		$stmh->bindValue(6, $comment, PDO::PARAM_STR);  
		$stmh->bindValue(7, $company, PDO::PARAM_STR);  
		$stmh->bindValue(8, $num, PDO::PARAM_STR);           //고유키값이 같나?의 의미로 ?로 num으로 맞춰야 합니다. where 구문 
	 
		$stmh->execute();
		$pdo->commit(); 
        } catch (PDOException $Exception) {
           $pdo->rollBack();
           print "오류: ".$Exception->getMessage();
       }      
 } 
 
 if ($mode=="insert"){	 	 
   try{
		$pdo->beginTransaction();
		 
		$sql = "insert into " . $DB . ".almember(name, part, dateofentry, referencedate, availableday, comment, company) ";     
		$sql .= " values(?, ?, ?, ?, ?, ?, ?) ";
		   
		$stmh = $pdo->prepare($sql); 
		$stmh->bindValue(1, $name, PDO::PARAM_STR);  
		$stmh->bindValue(2, $part, PDO::PARAM_STR);  
		$stmh->bindValue(3, $dateofentry, PDO::PARAM_STR);  
		$stmh->bindValue(4, $referencedate, PDO::PARAM_STR);  
		$stmh->bindValue(5, $availableday, PDO::PARAM_STR); 
		$stmh->bindValue(6, $comment, PDO::PARAM_STR); 
		$stmh->bindValue(7, $company, PDO::PARAM_STR); 
	 
		$stmh->execute();
		$pdo->commit(); 
     } catch (PDOException $Exception) {
          $pdo->rollBack();
       print "오류: ".$Exception->getMessage();
     }   
}

 if ($mode=="delete"){	 	 
   try{
     $pdo->beginTransaction();
  	 
     $sql = "delete from  " . $DB . ".almember where num = ?";  
     $stmh = $pdo->prepare($sql);
     $stmh->bindValue(1,$num,PDO::PARAM_STR);      
     $stmh->execute();   
     $pdo->commit();	 
     } catch (PDOException $Exception) {
          $pdo->rollBack();
       print "오류: ".$Exception->getMessage();
     }   
}

//각각의 정보를 하나의 배열 변수에 넣어준다.
$data = array(
	"dateofentry" =>  $dateofentry,
	"company" =>  $company,
	"mode" =>  $mode,
);

//json 출력
echo(json_encode($data, JSON_UNESCAPED_UNICODE));   
   
 ?>