php http请求非法偏移量
我正在尝试制作一个php脚本,它允许http请求将某些数据插入数据库。我对此有3个文件: Database.phpphp http请求非法偏移量,php,mysql,Php,Mysql,我正在尝试制作一个php脚本,它允许http请求将某些数据插入数据库。我对此有3个文件: Database.php Record.php 和Test.php 我试图发送以下请求: 这给我带来了以下错误消息: 警告:中的字符串偏移量“Naam”非法 /mnt/web106/b1/74/58274074/htdocs/school/fontyskart/php/test.php on 第25行警告:中的非法字符串偏移量“Eindtijd” /mnt/web106/b1/74/58274
Record.php
和Test.php
我试图发送以下请求:
这给我带来了以下错误消息:
警告:中的字符串偏移量“Naam”非法
/mnt/web106/b1/74/58274074/htdocs/school/fontyskart/php/test.php on
第25行警告:中的非法字符串偏移量“Eindtijd”
/mnt/web106/b1/74/58274074/htdocs/school/fontyskart/php/test.php on
第26行警告:中的非法字符串偏移量“Tijdstip”
/mnt/web106/b1/74/58274074/htdocs/school/fontyskart/php/test.php on
第27行{“消息”:“产品已创建”}
如您所见:它确实在数据库中创建了一个产品,但它们只是空记录
因为我是PHP新手,我真的不知道我做错了什么。任何能帮我解决这个问题的人?file\u get\u contents都无法获取发布的数据。阅读该功能的文档。我不知道你从哪里得到这样的想法,它会做你所期待的。你在网上找不到它的任何例子,教程,文档中没有任何能给你这种印象的东西。是什么让你决定试试这个??它将发出一个新的HTTP请求,并以文本而不是数组的形式从调用该URL返回响应。要获取发布到脚本中的数据,您要做的是从$\u POST数组中读取数据。世界上的每一个PHP教程都会向您展示这一点。
<?php
class Database{
private $host = "******";
private $db_name = "*********";
private $username = "**********";
private $password = "**********";
public $conn;
public function getConnection(){
$this->conn = null;
try{
$this->conn = new PDO("mysql:host=" . $this->host . ";dbname=" . $this->db_name, $this->username, $this->password);
$this->conn->exec("set names utf8");
}catch(PDOException $exception){
echo "Connection error: " . $exception->getMessage();
}
return $this->conn;
}
}
?>
<?php
class record{
// database connection and table name
private $conn;
private $table_name = "data";
// object properties
public $id;
public $Naam;
public $Eindtijd;
public $Tijdstip;
// constructor with $db as database connection
public function __construct($db){
$this->conn = $db;
}
function create(){
// query to insert record
$query = "INSERT INTO
" . $this->table_name . "
SET
Naam=:Naam, Eindtijd=:Eindtijd, Tijdstip=:Tijdstip";
// prepare query
$stmt = $this->conn->prepare($query);
// sanitize
$this->Naam=htmlspecialchars(strip_tags($this->Naam));
$this->Eindtijd=htmlspecialchars(strip_tags($this->Eindtijd));
$this->Tijdstip=htmlspecialchars(strip_tags($this->Tijdstip));
// bind values
$stmt->bindParam(":Naam", $this->Naam);
$stmt->bindParam(":Eindtijd", $this->Eindtijd);
$stmt->bindParam(":Tijdstip", $this->Tijdstip);
// execute query
if($stmt->execute()){
return true;
}else{
return false;
}
}
}
?>
<?php
// required headers
header("Access-Control-Allow-Methods: GET");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
error_reporting(E_ALL);
ini_set('display_errors', 'On');
// get database connection
include_once 'database.php';
// instantiate product object
include_once 'record.php';
$database = new Database();
$db = $database->getConnection();
$product = new record($db);
// get posted data
$data = file_get_contents("https://www.kevinit.nl/school/fontyskart/php/test.php");
// set product property values
$product->Naam= $data['Naam'];
$product->Eindtijd = $data['Eindtijd'];
$product->Tijdstip = $data['Tijdstip'];
// create the product
if($product->create()){
echo '{';
echo json_encode(['message' => 'Product was created']);
echo '}';
}
// if unable to create the product, tell the user
else{
echo '{';
echo json_encode(['message' => 'Unable to create product']);
echo '}';
}
?>