hendroprwk08

PHP Supplier

Oct 24th, 2020
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.17 KB | None | 0 0
  1. <?php
  2. header('Access-Control-Allow-Origin: *');
  3. header('Content-Type: application/json; charset=UTF-8');
  4. header('Access-Control-Allow-Methods: POST, GET, DELETE, PUT');
  5. header('Access-Control-Max-Age: 3600');
  6. header('Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With');
  7.  
  8. #BERI NAMA FILE DENGAN supplier-000.php
  9.  
  10. include 'database.php';
  11. $db = new Database();
  12.  
  13. switch ($_SERVER['REQUEST_METHOD']) { //jenis method
  14.     case 'POST':
  15.        
  16.         $db->open();
  17.        
  18.         switch ($_REQUEST['action']){
  19.             case 'simpan':
  20.                 $sql = 'insert into supplier values("'. $_REQUEST['id'] .'", ';
  21.                 $sql .= '"'. $_REQUEST['nama'] .'", "'. $_REQUEST['alamat'] .'", ';
  22.                 $sql .= '"'. $_REQUEST['telp'] .'",  "'. $_REQUEST['email'] .'", ';
  23.                 $sql .= '"'. $_REQUEST['pic'] .'")';
  24.                 break;
  25.             case 'ubah':
  26.                 $sql = 'update supplier set namasupplier = "'. $_REQUEST['nama'] .'", ';
  27.                 $sql .= 'alamatsupplier = "'. $_REQUEST['alamat'] .'", ';
  28.                 $sql .= 'telpsupplier = "'. $_REQUEST['telp'] .'", ';
  29.                 $sql .= 'emailsupplier = "'. $_REQUEST['email'] .'", ';
  30.                 $sql .= 'picsupplier = "'. $_REQUEST['pic'] .'" ';
  31.                 $sql .= 'where idsupplier = "'. $_REQUEST['id'] .'"';
  32.                 break;
  33.             case 'hapus':
  34.                 $sql = 'delete from supplier where idsupplier ="'. $_REQUEST['id'] .'"';
  35.                 break;
  36.         }
  37.        
  38.         $result = [
  39.             'result' => $db->execute($sql)
  40.         ];    
  41.        
  42.         $db->close();
  43.         print json_encode( $result );
  44.         break;
  45.        
  46.     case 'GET':
  47.        
  48.         $db->open();
  49.  
  50.         $result = [
  51.             'result' => $db->get('select * from supplier')
  52.         ];    
  53.  
  54.         $db->close();
  55.         print json_encode( $result );
  56.         break;
  57.    
  58.     default:
  59.        
  60.         http_response_code(400); //kode bad request
  61.  
  62.         $result = [
  63.             'result' => null
  64.         ];    
  65.    
  66.         print json_encode( $result );
  67.         break;
  68. }
  69. ?>
Add Comment
Please, Sign In to add comment