简体   繁体   English

我无法将发布的数据添加到从PHP执行的javascript发送的数据库中

[英]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.

相关问题 我无法使用AngularJs从文本框向数据库添加数据 - I can't add data from textbox to database using AngularJs 如何使用 javascript 文件中的已发布数据打开新的 PHP 页面 - How do I open a new PHP page with posted data from a javascript file 如何在 php 端回显来自 javascript 的发布数据以进行开发? - How do I echo posted data from javascript in the php side for development? 在哪里可以找到一个nodejs / javascript客户端组合示例,其中显示了从服务器在客户端中加载,修改并发送回的数据? - Where can I find a nodejs/javascript client combination example, showing data being loaded in the client from the server, modified, and sent back? PHP无法获取发送的Ajax数据 - PHP can't get ajax data sent 将使用POST从javascript发送的数据插入到使用php的mysql数据库中 - inserting data, sent from javascript using POST, into mysql database using php Javascript:如果稍后重定向到另一个页面,则无法将数据添加到数据库 - Javascript: Can't add data to database if redirecting to another page later 如何将我在 PHP 上上传的数据库中的数据传递给 JavaScript? - How can I pass data from database that I uploaded on PHP to JavaScript? 如何使用 PHP 数据库中的数据向基于 JavaScript 的表添加按钮? - How to add button to a JavaScript based Table with Data from PHP Database? 如何使用javascript获取父网站或发送文件的网站? - How can I get the parent site, or the site where the file was sent from, in javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM