Advertisement
Guest User

Untitled

a guest
Apr 20th, 2018
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.41 KB | None | 0 0
  1. <?php
  2. header("Access-Control-Allow-Origin: *");
  3. header("Content-Type: application/json; charset=UTF-8");
  4.  
  5. class Database{
  6.  
  7. // specify your own database credentials
  8. private $host = 'itsuite.it.brighton.ac.uk';
  9. private $db_name = "al605_andrea";
  10. private $username = "al605";
  11. private $password = "al605";
  12. public $conn;
  13.  
  14. // get the database connection
  15. public function getConnection()
  16. {
  17. $this->conn = null;
  18.  
  19. try {
  20. $this->conn = new PDO("mysql:host=" . $this->host . ";dbname=" . $this->db_name, $this->username, $this->password);
  21. $this->conn->exec("set names utf8");
  22. }
  23. catch (PDOException $exception) {
  24. echo "Connection error: " . $exception->getMessage();
  25.  
  26. }
  27.  
  28. return $this->conn;
  29. }
  30.  
  31. }
  32.  
  33.  
  34. if ($_SERVER['REQUEST_METHOD'] === 'GET') {
  35. // instantiate database and product object
  36. $database = new Database();
  37. $db = $database->getConnection();
  38.  
  39. // initialize object
  40. $product = new Item($db);
  41.  
  42. // query products
  43. $stmt = $product->read();
  44. $num = $stmt->rowCount();
  45.  
  46. // check if more than 0 record found
  47. if ($num > 0) {
  48.  
  49. // products array
  50.  
  51. $products_arr = array();
  52.  
  53.  
  54.  
  55. while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
  56. // extract row
  57. // this will make $row['name'] to
  58. // just $name only
  59. extract($row);
  60.  
  61. $product_item = array(
  62. "name" => $name,
  63. "notes" => $notes
  64.  
  65. );
  66.  
  67. array_push($products_arr, $product_item);
  68. }
  69. http_response_code(200);
  70. header('content-type: application/json');
  71. echo json_encode($products_arr);
  72.  
  73. }
  74.  
  75. else {
  76. http_response_code(204);
  77. echo json_encode(array(
  78. "message" => "No products found."
  79. ));
  80. }
  81.  
  82. }
  83.  
  84. if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  85.  
  86. $database = new Database();
  87. $db = $database->getConnection();
  88.  
  89. $product = new Item($db);
  90.  
  91. // get posted data
  92. $data = json_decode(file_get_contents("php://input"));
  93.  
  94.  
  95.  
  96. $product->oid = $_POST['oid'];
  97. $product->name = $_POST['name'];
  98. $product->notes = $_POST['notes'];
  99.  
  100.  
  101. // create the product
  102. if ($product->create()) {
  103. echo '{';
  104. echo '"message": "Product was created."';
  105. echo '}';
  106. }
  107.  
  108. // if unable to create the product, tell the user
  109. else {
  110. echo '{';
  111. echo '"message": "Unable to create product."';
  112. echo '}';
  113. }
  114.  
  115. }
  116.  
  117.  
  118. class Item
  119. {
  120.  
  121. // database connection and table name
  122. private $conn;
  123. private $table_name = "assesment_2";
  124.  
  125. // object properties
  126. public $oid;
  127. public $name;
  128. public $notes;
  129.  
  130.  
  131. // constructor with $db as database connection
  132. public function __construct($db)
  133. {
  134. $this->conn = $db;
  135. }
  136.  
  137. // read products
  138. function read()
  139. {
  140.  
  141. $query = "SELECT
  142. oid , name, notes
  143. FROM
  144. " . $this->table_name . " p
  145. WHERE
  146. oid='" . $_GET['oid'] . "'";
  147.  
  148. // prepare query statement
  149. $stmt = $this->conn->prepare($query);
  150.  
  151. // execute query
  152. $stmt->execute();
  153.  
  154. return $stmt;
  155. }
  156.  
  157. // create product
  158. function create(){
  159.  
  160.  
  161. // query to insert record
  162. $query = "INSERT INTO
  163. " . $this->table_name . " (oid,name,notes)
  164. VALUES ('" . $this->oid . "','" . $this->name . "','" . $this->notes . "')";
  165.  
  166.  
  167. echo $query;
  168. // prepare query
  169. $stmt = $this->conn->prepare($query);
  170. // sanitize
  171. $this->name = htmlspecialchars(strip_tags($this->name));
  172. $this->oid = htmlspecialchars(strip_tags($this->oid));
  173. $this->notes = htmlspecialchars(strip_tags($this->notes));
  174.  
  175.  
  176.  
  177.  
  178. // execute query
  179. if ($stmt->execute()) {
  180. return true;
  181. }
  182.  
  183. return false;
  184. }
  185. }
  186. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement