将表单数据与图像存储在数据库中

时间:2018-06-08 10:43:55

标签: php json api

它是我的第一个API,在此我能够在我的xyz数据库的abc表中存储id,名称,描述,价格,category_id。我只想将image_id,image_name,image_url也存储在同一个表中。我也想,该图像将存储在我的上传目录中。下面,在我的代码中,我能够在我的表中存储id,名称,描述,价格,category_id。我正在使用POSTMAN。任何人都可以帮助我.....

<?php
// required headers
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: POST");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");

// get database connection
include_once '../config/database.php';

// instantiate product object
include_once '../objects/product.php';

$database = new Database();
$db = $database->getConnection();

$product = new Product($db);

// get posted data
$data = (object) $_POST;

// set product property values
$product->name = $data->name;
$product->price = $data->price;
$product->description = $data->description;
$product->category_id = $data->category_id;
$product->created = date('Y-m-d H:i:s');

// create the product
if($product->create()){
    echo '{';
        echo '"message": "Product was created."';
    echo '}';
}

// if unable to create the product, tell the user
else{
    echo '{';
        echo '"message": "Unable to create product."';
    echo '}';
}
?>

1 个答案:

答案 0 :(得分:1)