0

我正在尝试使用 php 将子对象发布到数据库中,但无法弄清楚,感谢您的帮助。invAmount 的顶级 clientId 帖子很好,但是 invoiceDetails 子对象是我感到困惑的地方,我尝试了一个 while 循环但没有运气,我是一个 ui 人而不是 php,所以我没有想法。

帖子:

{
"clientId": "5",
"invNumber": "2",
"invProject": "Test Project",
"invDescription": "Test",
"invDate": "09/20/2013",
"invAmount": "5000",
"invoiceDetails": [
    {
        "invRowDescription": "Description 1",
        "invRowHours": "50",
        "invRowRate": "50",
        "invRowTotal": 2500
    },
    {
        "invRowDescription": "Description 2",
        "invRowHours": "50",
        "invRowRate": "50",
        "invRowTotal": 2500
    }
]
}

php控制器

<?php
$_POST = json_decode(file_get_contents('php://input'), true);


// Independent configuration
require 'medoo.php';
$database = new medoo(array( 
 // required 
  'database_type' => 'mysql', 
 'database_name' => 'dbname', 
 'server' => 'server', 
 'username' => 'user', 
 'password' => 'pw' 

));

$database->insert("invoiceSummary", array(
     "clientId" => $_POST['clientId'],
     "number" => $_POST['invNumber'],
    "project" => $_POST['invProject'],
    "description" => $_POST['invDescription'],
     "date" => $_POST['invDate'],
     "amount" => $_POST['invAmount']
));

while($rowInv = mysqli_fetch_array($_POST['invoiceDetails'])) {
    $database->insert("invoiceDetails", array(
       "clientId" => $_POST['clientId'],
"number" => $_POST['invNumber'],
    "description" => $_POST['invRowDescription'],
    "hours" => $_POST['invRowHours'],
    "rate" => $_POST['invRowRate'],
    "total" => $_POST['invRowTotal'],
));
}

   echo json_encode('success');
   ?>
4

0 回答 0