forked from ramsatt/A7CRUD_WEB_API
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate.php
36 lines (33 loc) · 939 Bytes
/
create.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
33
34
35
36
<?php
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: PUT, GET, POST");
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
if($_POST){
// include database connection
include 'config/database.php';
try{
// insert query
$query = "INSERT INTO products SET p_name=:name, p_description=:description, p_price=:price";
// prepare query for execution
$stmt = $con->prepare($query);
// posted values
$name = $_POST['name'];
$description = $_POST['description'];
$price = $_POST['price'];
// bind the parameters
$stmt->bindParam(':name', $name);
$stmt->bindParam(':description', $description);
$stmt->bindParam(':price', $price);
// Execute the query
if($stmt->execute()){
echo json_encode(array('result'=>'success'));
}else{
echo json_encode(array('result'=>'fail'));
}
}
// show error
catch(PDOException $exception){
die('ERROR: ' . $exception->getMessage());
}
}
?>