dsoxcj7276 2016-08-24 05:18
浏览 186
已采纳

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

I have a table on an Invitation. I am passing the data in json format from postman.

I want to send many invitations at a time. So I want to insert multiple invitations.

How can I do this?

I have created a single invitation.

Invitaion :

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

?>

Input from postman:

{

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

Output:

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

EDIT:

In Send Invite() function:

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

    }

In senInvite.php file :

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

}

var dump:

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

Gives syntax error: unexpeted 'S'

I want to accept this as json array and insert into the table all the records.

Can anyone help please? Thank you..

  • 写回答

1条回答 默认 最新

  • du1843 2016-08-24 05:44
    关注
      <?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.
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?