【问题标题】:Data handling JS to PHP inserting into SQL database [duplicate]数据处理 JS 到 PHP 插入 SQL 数据库 [重复]
【发布时间】:2019-10-14 13:52:10
【问题描述】:

我已经构建了一个收集数据的工具。数据应分隔为 id、dropzone 和 item。这基本上是一个分类卡片工具。

我已经构建了一个收集数据的函数,最终看起来是这样的。见图1

现在我想把这个发送到 php,而 search1() 是数据收集函数。

function sendData() {
    let target = 'php/data.php';
    let data = searching1();
    JSON.stringify(data);
    let sendData = function () {
        $.post('php/data.php', {
            data: data
        }, function (response) {

        });

    };

    fetch(target, {
        method: "POST",
        mode: "same-origin",
        credentials: "same-origin",
        headers: {
            'Content-Type': 'application/json',
            //'Accept': 'application/json',
        },
        body: JSON.stringify({
            data

        })


    }).then(res => {
        const response = res.json();
        console.log(data);

        return response;
    });

我现在的问题是数据到达 PhP,但我现在需要分开,因为现在一切都是一个大数组。 是否有任何智能功能可以做到这一点? 我试过类似

    $data = $decoded ['data'];
    $dropzone = $decoded['data'];
    $item = $decoded ['data'];
    $id = $decoded ['data'];

但这并不完全有效,因为我收到“数组到字符串转换”的通知。

所以基本上问题是,我可以将这个大数组转换为字符串(对吗?),然后将其插入到 sql 数据库中?


if ($contentType === "application/json") {
    $content = trim(file_get_contents("php://input"));
    $decoded = json_decode($content, true);

    $data = $decoded ['data'];    
    $dropzone = $decoded['data'];
    $item = $decoded ['data'];
    $id = $decoded ['data'];



    $create_table = "CREATE TABLE IF NOT EXISTS test (id VARCHAR(255), dropzone VARCHAR(255),item VARCHAR(255))";
    $table_statement = $pdo->exec($create_table);

    if (is_array($data) || is_object($data)) {
        foreach ($data as $value) {
            $sql = "INSERT INTO test (id, dropzone, item) VALUES ('$id','$dropzone','$item')";
            $stmt = $pdo->exec($sql);
        }

        if (is_array($decoded)) {
            $response = $decoded;
            header('Content-Type: application/json');
            echo json_encode($response);

        } else {
            echo("FEHLER");
        }
    }
}

这基本上是我的 php 代码。我已经完成了数据库配置和数据库连接。

任何帮助我都非常高兴..

一切顺利

【问题讨论】:

  • $content 看起来像什么?试试var_dump($content); var_dump($data); 看看它们都包含什么。

标签: javascript php sql api fetch


【解决方案1】:

这是 var_dump ($data)

  [0]=>
  array(3) {
    ["id"]=>
    string(2) "12"
    ["dropzone"]=>
    int(1)
    ["item"]=>
    string(7) "image29"
  }
  [1]=>
  array(3) {
    ["id"]=>
    string(2) "12"
    ["dropzone"]=>
    int(2)
    ["item"]=>
    string(7) "image30"
  }
}

这是 var_dump ($content)

string(94) "{"data":[{"id":"12","dropzone":1,"item":"image29"},{"id":"12","dropzone":2,"item":"image30"}]}"

如何构建一个引用 item、dropzone 或 id 的变量?比如我如何只访问数组的一部分?

【解决方案2】:

@aynber 很到位。您需要访问数组中的数据,即您需要的数据。

var_dump 应该可以帮你解决这个问题。

如果您想存储一个数组,您可以重新创建 json。

$myJSON = json_encode($myObj);

并且 MySQL 在较新版本中具有数据类型 JSON。

_________________ 已编辑:新信息 _____________

这是 var_dump ($data)

[0]=>
  array(3) {
    ["id"]=>
    string(2) "12"
    ["dropzone"]=>
    int(1)
    ["item"]=>
    string(7) "image29"
  }
  [1]=>
  array(3) {
    ["id"]=>
    string(2) "12"
    ["dropzone"]=>
    int(2)
    ["item"]=>
    string(7) "image30"
  }
}

print $data[0]['id'];       // out 12
print $data[0]['dropzone']; // out 1
print $data[0]['item'];     // out image29

https://www.php.net/manual/en/control-structures.for.php

【讨论】:

  • 我想我的问题是,如何创建访问我的数据的变量。所以我可以稍后将我的变量用于插入到 sql 中的值?现在我无法创建像 $id 这样的变量,它应该引用数据集中的 id...
  • 非常感谢!这有帮助!也许下一步只是解决一个问题,但是您将如何放置一个循环,该代码针对每个放置区和每个放置区的每个项目运行?万事如意
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-08-24
  • 2015-06-26
  • 2018-01-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多