Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- require_once('config.php');
- if(isset($_GET['what'])){
- $what = $_GET['what'];
- switch ($what) {
- case 'displayData':
- $what = displayData();
- break;
- case 'insertData':
- $what = insertData();
- break;
- case 'deleteData':
- $what = deleteData();
- break;
- case 'getData':
- $what = getData();
- break;
- default:
- # code...
- break;
- }
- }
- function displayData(){
- global $mysqli;
- $query = "SELECT * FROM users";
- $rs = $mysqli->query($query);
- $data = array();
- while($row = $rs->fetch_assoc()){
- $data[] = $row;
- }
- echo json_encode($data);
- $rs->free();
- }
- function insertData(){
- global $mysqli;
- $data = json_decode(file_get_contents("php://input"));
- $username = $mysqli->real_escape_string($data->username);
- $password = $mysqli->real_escape_string($data->password);
- $query = "INSERT INTO tbl_user(username, password)VALUES('$username', '$password')";
- if($mysqli->query($query)){
- echo "<p>New Record Successfully Created</p>";
- }
- }
- function deleteData(){
- global $mysqli;
- $data = json_decode(file_get_contents("php://input"));
- $id = $mysqli->real_escape_string($data->id);
- $query = "DELETE FROM tbl_user WHERE id = '$id'";
- if($mysqli->query($query)){
- echo "<p>Record Deleted</p>";
- }
- }
- function getData(){
- global $mysqli;
- $data = json_decode(file_get_contents("php://input"));
- $id = $mysqli->real_escape_string($data->id);
- $query = "SELECT * FROM tbl_user WHERE id ='$id'";
- $rs = $mysqli->query($query);
- $data = array();
- while($row = $rs->fetch_assoc()){
- $data[] = $row;
- }
- echo json_encode($data);
- $rs->free();
- }
- // function updateData(){
- // global $mysqli;
- // $data = file_get_contents("php://input");
- // echo $id = $mysqli->real_escape_string($data)
- // }
- ?>
Add Comment
Please, Sign In to add comment