dsoxcj7276
2016-08-24 05:18
浏览 167

如何获取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.
    
    已采纳该答案
    打赏 评论

相关推荐 更多相似问题