Created
April 24, 2015 21:48
-
-
Save ziedrebhi/181cf6cb07c26b7403e6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Following code will create a new product row | |
* All product details are read from HTTP Post Request | |
*/ | |
// array for JSON response | |
$response = array(); | |
// check for required fields | |
if (isset($_POST['name_prod']) && isset($_POST['price_prod']) && isset($_POST['desc_prod'])&& isset($_POST['image_prod'])&& isset($_POST['id_cat'])) { | |
$name_prod = $_POST['name_prod']; | |
$price_prod = $_POST['price_prod']; | |
$desc_prod = $_POST['desc_prod']; | |
$image_prod = $_POST['image_prod']; | |
$id_cat = $_POST['id_cat']; | |
// include db connect class | |
require_once __DIR__ . '/db_connect.php'; | |
// connecting to db | |
$con = new DB_CONNECT(); | |
$con->connect(); | |
// mysql inserting a new row | |
$result = mysqli_query($con->myconn,"INSERT INTO products(name_prod, price_prod, desc_prod,id_cat,image_prod) VALUES('$name_prod', '$price_prod', '$desc_prod', '$id_cat', '$image_prod')"); | |
// check if row inserted or not | |
if ($result) { | |
// successfully inserted into database | |
$response["success"] = 1; | |
$response["message"] = "Product successfully created."; | |
// echoing JSON response | |
echo json_encode($response); | |
} else { | |
// failed to insert row | |
$response["success"] = 0; | |
$response["message"] = "Oops! An error occurred."; | |
// echoing JSON response | |
echo json_encode($response); | |
} | |
} else { | |
// required field is missing | |
$response["success"] = 0; | |
$response["message"] = "Required field(s) is missing"; | |
// echoing JSON response | |
echo json_encode($response); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment