Guest User

PHP Codes

a guest
May 19th, 2017
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.87 KB | None | 0 0
  1. <?php
  2.  
  3.     require_once('config.php');
  4.     if(isset($_GET['what'])){
  5.  
  6.         $what = $_GET['what'];
  7.  
  8.         switch ($what) {
  9.             case 'displayData':
  10.                 $what = displayData();
  11.                 break;
  12.             case 'insertData':
  13.                 $what = insertData();
  14.                 break;
  15.             case 'deleteData':
  16.                 $what = deleteData();
  17.                 break;
  18.             case 'getData':
  19.                 $what = getData();
  20.                 break;
  21.             default:
  22.                 # code...
  23.                 break;
  24.         }
  25.     }
  26.  
  27.  
  28.     function displayData(){
  29.  
  30.         global $mysqli;
  31.         $query = "SELECT * FROM users";
  32.         $rs = $mysqli->query($query);
  33.        
  34.         $data = array();
  35.         while($row = $rs->fetch_assoc()){
  36.             $data[] = $row;
  37.         }
  38.  
  39.         echo json_encode($data);
  40.         $rs->free();
  41.     }
  42.  
  43.     function insertData(){
  44.  
  45.         global $mysqli;
  46.         $data = json_decode(file_get_contents("php://input"));
  47.         $username = $mysqli->real_escape_string($data->username);
  48.         $password = $mysqli->real_escape_string($data->password);
  49.  
  50.         $query = "INSERT INTO tbl_user(username, password)VALUES('$username', '$password')";
  51.  
  52.         if($mysqli->query($query)){
  53.             echo "<p>New Record Successfully Created</p>";
  54.         }
  55.        
  56.     }
  57.  
  58.     function deleteData(){
  59.  
  60.         global $mysqli;
  61.         $data = json_decode(file_get_contents("php://input"));
  62.         $id = $mysqli->real_escape_string($data->id);
  63.  
  64.         $query = "DELETE FROM tbl_user WHERE id = '$id'";
  65.  
  66.         if($mysqli->query($query)){
  67.             echo "<p>Record Deleted</p>";
  68.         }
  69.     }
  70.  
  71.     function getData(){
  72.  
  73.         global $mysqli;
  74.         $data = json_decode(file_get_contents("php://input"));
  75.         $id = $mysqli->real_escape_string($data->id);
  76.  
  77.         $query = "SELECT * FROM tbl_user WHERE id ='$id'";
  78.         $rs = $mysqli->query($query);
  79.         $data = array();
  80.  
  81.         while($row = $rs->fetch_assoc()){
  82.             $data[] = $row;
  83.         }
  84.  
  85.         echo json_encode($data);
  86.         $rs->free();
  87.     }
  88.  
  89.     // function updateData(){
  90.  
  91.     //  global $mysqli;
  92.     //  $data = file_get_contents("php://input");
  93.  
  94.     //  echo $id = $mysqli->real_escape_string($data)
  95.  
  96.     // }
  97.  
  98. ?>
Add Comment
Please, Sign In to add comment