1.插入函数文件dbop.php
<?php
function add_niki_book_info($conn, $row)
{
$query = "insert into NIKI_BOOK_INFO(`cnum`,`isbn`,`date`,`status`) values ('".$row["cnum"]."','".$row["isbn"]."','".$row["date"]."','".$row["status"]."');";
$result = $conn->query($query);
if($result)
{
echo "ok";
$conn->commit();
}
else
{
echo "error";
$conn->rollback();
}
}
?>
2.api封装5.php文件
<?php
require "dbop.php";
$recv = json_decode(file_get_contents('php://input'));
$row['cnum'] = $recv->{cnum};
$row['isbn'] = $recv->{isbn};
$row['date'] = $recv->{date};
$row['status'] = $recv->{status};
$conn = new mysqli("localhost", "niki", "niki", "niki") or dir("不能连接到mysql");
$conn->query("set names utf8");
add_niki_book_info($conn, $row);
$conn->close();
?>
3.测试脚本book_info_add.sh
#!/bin/bash
curl -H "Content-Type:application/json" -X POST --data '{
"cnum": "TN0000002",
"isbn": "502",
"date": "2019-06-02",
"status": "1"
}' http://10.192.89.12/local/2/5.php
4.运行测试脚本
$ ./book_info_add.sh