[英]I can't add posted data to database sent from javascript where executed in PHP
I have problem with my code. 我的代码有问题。
In this code I am sending some data to another server. 在这段代码中,我将一些数据发送到另一台服务器。
fetch("https://example.com/test.php", {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
"isim": lastPlayersTouched[0].name,
"sure": getTime(scores)
})
});
And here I have database connection: 在这里我有数据库连接:
<?php
try {
$db=new PDO("mysql:localhost;dbname=test1_evet;charset=utf8",'test1_evet','N#rK%*?PkTki');
//echo "ok";
}
catch (PDOException $e) {
echo $e->getMessage();
}
?>
And the final part here I am trying to add posted things to add my database: 最后一部分我在尝试添加发布的内容来添加我的数据库:
<?php
header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Credentials: true");
header('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS');
header('Access-Control-Max-Age: 1000');
header('Access-Control-Allow-Headers: Origin, Content-Type, X-Auth-Token , Authorization');
require_once 'baglan.php';
// if (isset($_POST)) {
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$arr = json_decode(file_get_contents('php://input'), true);
$kaydet=$db->prepare("INSERT into test SET
isim=:isim,
sure=:sure
");
$insert=$kaydet->execute(array(
// 'isim' => $_POST['isim'],
'isim' => $arr['isim'],
// 'sure' => $_POST['sure']
'sure' => $arr['sure']
));
}
// }
?>
But the problem is, I can send data from another server to my current server with 200 OK code. 但问题是,我可以使用200 OK代码将数据从另一台服务器发送到我当前的服务器。 But PHP doesn't add things I've sent to database. 但PHP不会添加我发送给数据库的东西。 I mean not inserting to database. 我的意思是不插入数据库。 Can someone help me on that? 有人可以帮助我吗? I will be very happy if you guys help me. 如果你们帮助我,我会很高兴的。
Any fix? 任何修复?
fetch("https://example.com/test.php", {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
mode: 'cors',
body: JSON.stringify({
'isim': lastPlayersTouched[0].name,
'sure': getTime(scores)
})
});
database connection (host parameter missing) 数据库连接(缺少主机参数)
$db=new PDO("mysql:host=localhost;dbname=test1_evet;charset=utf8",'test1_evet','N#rK%*?PkTki');
Access-Control-Allow-Headers (xhr header missing) Access-Control-Allow-Headers(缺少xhr头)
header('Access-Control-Allow-Headers: Origin, Content-Type, X-Auth-Token , Authorization, X-Requested-With');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.