-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd.php
32 lines (24 loc) · 1.09 KB
/
add.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<?php
require_once "inc/functions.php";
require_once "inc/headers.php";
$input = json_decode(file_get_contents("php://input"));
$description = filter_var($input->description,FILTER_SANITIZE_SPECIAL_CHARS);
$amount = filter_var($input->amount,FILTER_SANITIZE_SPECIAL_CHARS);
try {
$db = new PDO("mysql:host=localhost;dbname=shoppinglist;charset=utf8","root","");
$db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
//$db = new PDO("mysql:host=localhost;dbname=shoppinglist;charset=utf8","root","");
$db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
$query = $db->prepare("insert into item(description, amount) values (:description, :amount)");
$query->bindValue(":description",$description,PDO::PARAM_STR);
$query->bindValue(":amount",$amount,PDO::PARAM_INT);
$query->execute();
header("HTTP/1.1 200 OK");
$data = array('id' => $db->lastInsertId(),'description' => $description,'amount' => $amount);
print json_encode($data);
} catch (PDOException $pdoex) {
header("HTTP/1.1 500 Internal Server Error");
$error = array("error" => $pdoex->getMessage());
print json_encode($error);
}
?>