0

I have built a tool which collects data. The data should be separated to id, dropzone and item. Which is basically a sorting cards tool.

I have built a function that collects data, which looks like this in the end. See picture:

Now I want to send this to PHP, while searching1() is the data collecting function.

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;
    });

My problem now is that the data is arriving in PHP, but I need separate now I guess from each other, cause now everything is one big array. Is there any smart function to do this? I've tried something like:

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

But this doesn't quite work, because I get the "Array to string conversion" notice.

So basically the question is, can I transform this one big array into strings (right?), to insert it into an SQL database?

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");
        }
    }
}

This is basically my PHP code. I have the database config and database connect already done.

I'm super happy for any help..

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • 1
    What does `$content` look like? Try `var_dump($content); var_dump($data);` to see what they both contain. – aynber Oct 14 '19 at 13:58

2 Answers2

0

@aynber is spot on. You need to access the data in your array, the data you need.

var_dump should help you sort that out.

Should you want to store an array you could just re-create the json.

$myJSON = json_encode($myObj);

and MySQL has a data type JSON in newer versions.

_________________ Edited: New Information _____________

This is the 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

  • I think my question it, how do I create variables that access to my data. So I can later use my variables for values that are inserted in the sql? Right now i cant create a variable like $id that should refer to the id in the dataset... – Marc J. Jerschov Oct 14 '19 at 14:48
  • https://www.php.net/manual/en/language.types.array.php – Richard Tyler Miles Oct 14 '19 at 15:40
  • Thanks a lot! This helps! Maybe the next step is to big for just one question, but how would you put a loop, that the code runs for each dropzone and each item per dropzone? All the best – Marc J. Jerschov Oct 14 '19 at 17:47
0

This is the 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"
  }
}

and this is the var_dump ($content)

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

how do build a variable that refers to item, dropzone or id? Like how do I access for just one part of the array?