php - 如何获取json数组并插入数据库。 php

标签 php mysql json associative-array

我有一张关于邀请函的 table 。我从 postman 以 json 格式传递数据。

我想一次发送很多邀请。所以我想插入多个邀请。

我该怎么做?

我创建了一个邀请。

邀请:

    class Invitation
{
    private $sender_id,$date,$invitee_no,$status;

    function Invitation($sender_id,$date,$invitee_no,$status)
    {

        $this->sender_id = $sender_id;
        $this->date= $date;
        $this->invitee_no = $invitee_no;
        $this->status = $status;

    }
    function sendInvite()
    {

        $database = new Database(ContactsConstants::DBHOST,ContactsConstants::DBUSER,ContactsConstants::DBPASS,ContactsConstants::DBNAME);
        $dbConnection = $database->getDB();

        $stmt = $dbConnection->prepare("select * from Invitation where invitee_no =?");
        $stmt->execute(array($this->invitee_no));
        $rows = $stmt->rowCount();

        if($rows > 0)
        {
            $response = array("status"=>-3,"message"=>"Invitation exists.");
            return $response;
        }

            $stmt = $dbConnection->prepare("insert into Invitation(date,invitee_no,status) values(?,?,?)");
            $stmt->execute(array($this->date, $this->invitee_no, $this->status));
            $rows = $stmt->rowCount();
            $Id = $dbConnection->lastInsertId();

            $stmt = $dbConnection->prepare("select * from Invitation where sender_id=?");
            $stmt->execute(array($Id));
            $invitation = $stmt->fetchAll(PDO::FETCH_ASSOC);

            if ($rows < 1) {
                $response = array("status" => -1, "message" => "Failed to send Invitation., unknown reason");
                return $response;
            } else {
                $response = array("status" => 1, "message" => "Invitation sent.", "Invitation:" => $invitation);
                return $response;
            }

    }
}

sendInvite.php

    <?php

error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
ini_set('display_errors', '1');

require 'Invitation.php';

$jsonText = file_get_contents('php://input');

if(empty($jsonText))
{
    $response = array("status"=>-2,"message"=>"Empty request");
    die(json_encode($response));
}

$json = json_decode($jsonText);

$date= $json -> date;
$invitee_no = $json -> invitee_no;
$status = $json -> status;

$invitation = new Invitation("",$date,$invitee_no,$status);
$response = $invitation->sendInvite();

echo(json_encode($response));

?>

postman 的输入:

{

"date" : "12/08/2016",
"invitee_no" : "5258",
"status" : "1"
}

输出:

   {
  "status": 1,
  "message": "Invitation sent.",
  "Invitation:": [
    {
      "sender_id": "29",
      "date": "12/08/2016",
      "invitee_no": "5259",
      "status": "1"
    }
  ]
}

编辑:

在 Send Invite() 函数中:

if ($rows < 1) {

        $response = array("status" => -1, "message" => "Failed to send Invitation., unknown reason");
        echo(json_encode($response));

    } else {
        $response = array("status" => 1, "message" => "Invitation sent.", "Invitation:" => $invitation);
        echo(json_encode($response));

    }

在 senInvite.php 文件中:

    foreach ($json as $jsn) {
    foreach($jsn as $j)
    {
        $date= $j -> date;
        $invitee_no = $j -> invitee_no;
        $status = $j -> status;
        $invitation = new Invitation("",$date,$invitee_no,$status);
        $response = $invitation->sendInvite();

        var_dump($response);
        die();

        echo(json_encode($response));
    }

}

变量转储:

{"status":-3,"message":"Invitation exists.","invitee_no":"5856"}array(3) {
  ["status"]=>
  int(-3)
  ["message"]=>
  string(18) "Invitation exists."
  ["invitee_no"]=>
  string(4) "5856"
}

给出语法错误:意外的“S”

我想接受它作为 json 数组并将所有记录插入表中。

有人可以帮忙吗?谢谢..

最佳答案

  <?php

   error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
   ini_set('display_errors', '1');

   require 'Invitation.php';

   $jsonText = file_get_contents('php://input');

   if(empty($jsonText))
   {
      $response = array("status"=>-2,"message"=>"Empty request");
      die(json_encode($response));
   }

$response = array();

   $json = json_decode($jsonText);
  foreach ($json as $jsn) {
   foreach($jsn as $j)
    {
      $date= $j -> date;
      $invitee_no = $j -> invitee_no;
      $status = $j -> status;
      $invitation = new Invitation("",$date,$invitee_no,$status);

       $response[] = $invitation->sendInvite();


    } 
 }
echo(json_encode($response));
 ?>

I have used foreach for array.

关于php - 如何获取json数组并插入数据库。 php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39114958/

相关文章:

javascript - 脚本 IF ELSE 仅处理 php 中的第一个数据

javascript - 部署 AngularJS 应用程序 : How to make app read JSON data

c# - 在同一响应中返回多个 JSON 对象并解析它

php - MYSQL/PDO PHP系统---寻找大笔画输入

php - 使用 vim Tabularize 插件只匹配第一次出现的定界符

php - 返回 SQL 查询结果,即使未找到精确匹配

ios - 解析 http 多部分响应

php - 具有特定类的 mysql_fetch_object

php - PDO非锁定查询: possible?

mysql - 使用连接从表中获取最新值