我正在尝试通过API链接从邮递员发送数据(温度,湿度和时间)以测试我的代码。我将php文件上传到hostinger.com,每次尝试通过邮递员发送数据时,它都可以,没有错误,但数据未显示在phpmyadmin数据库表中!
我的insert.php代码:
<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
//Creating Array for JSON response
$response = array();
// Check if we got the field from the user
if (isset($_GET['temp']) && isset($_GET['hum']) && isset($_GET['time'])) {
$temp = $_GET['temp'];
$hum = $_GET['hum'];
$time = $_GET['time'];
// Include data base connect class
$filepath = realpath (dirname(__FILE__));
require_once($filepath."/db_connect.php");
// Connecting to database
$dbo = new DB_CONNECT();
// Fire SQL query to insert data in weather
$result = "INSERT INTO 'climate'('temp', 'hum', 'time') VALUES('$temp', '$hum', '$time')";
// Check for succesfull execution of query
if ($result) {
// successfully inserted
$response["success"] = 1;
$response["message"] = "climate successfully created.";
// Show JSON response
echo json_encode($response);
} else {
// Failed to insert data in database
$response["success"] = 0;
$response["message"] = "Something has been wrong";
// Show JSON response
echo json_encode($response);
}
} else {
// If required parameter is missing
$response["success"] = 0;
$response["message"] = "Parameter(s) are missing. Please check the
request";
// Show JSON response
echo json_encode($response);
}
?>
和我的db_connect.php:
<?php
class DB_CONNECT {
// Connecting to mysql (phpmyadmin) database
// Constructor
function __construct() {
// Trying to connect to the database
$this->connect();
}
// Destructor
function __destruct() {
// Closing the connection to database
$this->close();
}
// Function to connect to the database
function connect() {
//importing dbconfig.php file which contains database credentials
$filepath = realpath (dirname(__FILE__));
require_once($filepath."/dbconfig.php");
// Connecting to mysql (phpmyadmin) database
$con = mysqli_connect($dbhost_name, $username, $password);
// Selecing database
// returing connection cursor
return $con;
}
// Function to close the database
function close() {
// Closing data base connection
mysqli_close($con);
}
}
?>
这是我正在使用的邮递员中的链接:
http://mydomain in hostinger.com/api/insert.php?temp=25&hum=80&time=2019-05-
09 08:00:00
结果是:
{
"success": 1,
"message": "climate successfully created."
}
我的代码有问题吗?
感谢所有人
答案 0 :(得分:0)
您尚未实际执行查询。 $result
实际上是$query
,应将其传递到数据库连接$dbo
。