如何只在我的数组中插入一个与json一起使用php到mysql的数组

时间:2021-03-14 21:33:39

I have a php file that i am inserting data from a json into a mysql database, and i am using a foreach() to list the tech's and then insert them into mysql. my problem is it seems to just insert 3 of the same things into the database and not 1 for each tech.

我有一个php文件,我将数据从json插入到mysql数据库中,我使用foreach()列出技术,然后将它们插入到mysql中。我的问题是它似乎只是将3个相同的东西插入数据库而不是每个技术1个。

Example

id 1, 1, 1
name mike, mike, mike
number 10, 10, 10

the above should be more like 

id 1, 2, 3
name mike, sandy, joe
number 10, 11, 12

I can see what i am trying to do but when i google i get nothing but how to count with count().

我可以看到我想要做什么但是当我谷歌我什么也得不到,但如何计数与count()。

$ijobid = $data['invoice']['jobId'];
    foreach($data['jobAssignments'] as $chunk){
        $jatech = $chunk['technician'];
        $jatechid = $jatech['id'];
        $jatechname = $jatech['name'];
        $jasplit = $chunk['split'];
        $jadriving = $chunk['totalDrivingHours'];
        $jaworking = $chunk['totalWorkingHours'];
        $jaassigned = $chunk['assignedOn'];
        $jatechstatus = $chunk['status'];
        $jatechfinished = array($jatechid, $jatechname);
        $jobassignreults[] = $jatechfinished;
    }
    foreach($jatech as $key => $techs){
        $sql = "INSERT INTO `techtable` (`ijobid`,`jtid`,`jtname`) VALUES ('$ijobid', '$jatechid', '$jatechname')";
        if (mysqli_query($conn, $sql)) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}

}

this is my php code, i know it works to connect because i get data but it doesn't seem to work correctly. the tech data in the json is not static and can be different every time i pass it, so today it could list 1 tech tomorrow 5.

这是我的PHP代码,我知道它可以连接,因为我得到数据,但它似乎无法正常工作。 json中的技术数据不是静态的,每次通过时都可以不同,所以今天它可以列出明天的1技术5。

Thank you in advance.

先谢谢你。

Edit - here is the json section i am pulling from:

编辑 - 这是我要拉的json部分:

"jobAssignments": [
    {
      "id": 15797,
      "jobId": 15792,
      "jobNumber": "15792",
      "technician": {
        "id": 156,
        "name": "Mike"
      },
      "split": 100,
      "totalDrivingHours": 1680,
      "totalWorkingHours": 7680,
      "assignedOn": "2015-11-05T09:08:22.5680879",
      "status": "Done"
    }
  ],

1 个解决方案

#1


2  

You have 2 foreach loops. The first consumes all the input data so leaving only the last occurance in the scalar variables. Your second foreach loops around $jatech which is the LAST version of $jatech = $chunk['technician'];

你有2个foreach循环。第一个消耗所有输入数据,因此只留下标量变量中的最后一个。你的第二个foreach绕着$ jatech循环,这是$ jatech = $ chunk ['technician']的最新版本;

This should work a little better

这应该会好一点

$ijobid = $data['invoice']['jobId'];
foreach($data['jobAssignments'] as $chunk){
    $jatech = $chunk['technician'];
    $jatechid = $jatech['id'];
    $jatechname = $jatech['name'];
    $jasplit = $chunk['split'];
    $jadriving = $chunk['totalDrivingHours'];
    $jaworking = $chunk['totalWorkingHours'];
    $jaassigned = $chunk['assignedOn'];
    $jatechstatus = $chunk['status'];
    $jatechfinished = array($jatechid, $jatechname);
    $jobassignreults[] = $jatechfinished;

    $sql = "INSERT INTO `techtable` (`ijobid`,`jtid`,`jtname`) 
                VALUES ('$ijobid', '$jatechid', '$jatechname')";
    if (mysqli_query($conn, $sql)) {
        echo "New record created successfully";
    } else {
        echo "Error: " . $sql . "<br>" . mysqli_error($conn);
    }
}

And this would be more efficient

这会更有效率

$ijobid = $data['invoice']['jobId'];

$sql = "INSERT INTO `techtable` (`ijobid`,`jtid`,`jtname`) 
                VALUES (?,?,?)";
$stmt = $conn->prepare($sql);

foreach($data['jobAssignments'] as $chunk){
    $jatech = $chunk['technician'];
    $jatechid = $jatech['id'];
    $jatechname = $jatech['name'];
    $jasplit = $chunk['split'];
    $jadriving = $chunk['totalDrivingHours'];
    $jaworking = $chunk['totalWorkingHours'];
    $jaassigned = $chunk['assignedOn'];
    $jatechstatus = $chunk['status'];
    $jatechfinished = array($jatechid, $jatechname);
    $jobassignreults[] = $jatechfinished;

    $stmt->bind_param('sss', $ijobid, $jatechid, $jatechname);
    if ( $stmt->execute() ) {
        echo "New record created successfully";
    } else {
        echo "Error: " . $sql . "<br>" . $conn->error;
    }
}

The only thing you may need to check is that all 3 datatypes are in fact strings.

您可能需要检查的唯一事情是所有3种数据类型实际上都是字符串。

 $stmt->bind_param('sss', $ijobid, $jatechid, $jatechname);

#1


2  

You have 2 foreach loops. The first consumes all the input data so leaving only the last occurance in the scalar variables. Your second foreach loops around $jatech which is the LAST version of $jatech = $chunk['technician'];

你有2个foreach循环。第一个消耗所有输入数据,因此只留下标量变量中的最后一个。你的第二个foreach绕着$ jatech循环,这是$ jatech = $ chunk ['technician']的最新版本;

This should work a little better

这应该会好一点

$ijobid = $data['invoice']['jobId'];
foreach($data['jobAssignments'] as $chunk){
    $jatech = $chunk['technician'];
    $jatechid = $jatech['id'];
    $jatechname = $jatech['name'];
    $jasplit = $chunk['split'];
    $jadriving = $chunk['totalDrivingHours'];
    $jaworking = $chunk['totalWorkingHours'];
    $jaassigned = $chunk['assignedOn'];
    $jatechstatus = $chunk['status'];
    $jatechfinished = array($jatechid, $jatechname);
    $jobassignreults[] = $jatechfinished;

    $sql = "INSERT INTO `techtable` (`ijobid`,`jtid`,`jtname`) 
                VALUES ('$ijobid', '$jatechid', '$jatechname')";
    if (mysqli_query($conn, $sql)) {
        echo "New record created successfully";
    } else {
        echo "Error: " . $sql . "<br>" . mysqli_error($conn);
    }
}

And this would be more efficient

这会更有效率

$ijobid = $data['invoice']['jobId'];

$sql = "INSERT INTO `techtable` (`ijobid`,`jtid`,`jtname`) 
                VALUES (?,?,?)";
$stmt = $conn->prepare($sql);

foreach($data['jobAssignments'] as $chunk){
    $jatech = $chunk['technician'];
    $jatechid = $jatech['id'];
    $jatechname = $jatech['name'];
    $jasplit = $chunk['split'];
    $jadriving = $chunk['totalDrivingHours'];
    $jaworking = $chunk['totalWorkingHours'];
    $jaassigned = $chunk['assignedOn'];
    $jatechstatus = $chunk['status'];
    $jatechfinished = array($jatechid, $jatechname);
    $jobassignreults[] = $jatechfinished;

    $stmt->bind_param('sss', $ijobid, $jatechid, $jatechname);
    if ( $stmt->execute() ) {
        echo "New record created successfully";
    } else {
        echo "Error: " . $sql . "<br>" . $conn->error;
    }
}

The only thing you may need to check is that all 3 datatypes are in fact strings.

您可能需要检查的唯一事情是所有3种数据类型实际上都是字符串。

 $stmt->bind_param('sss', $ijobid, $jatechid, $jatechname);