mysql - How to get json array and insert in the database. php -


i have table on invitation. passing data in json format postman.

i want send many invitations @ time. want insert multiple invitations.

how can this?

i have created 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 * invitation 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 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 * invitation sender_id=?");             $stmt->execute(array($id));             $invitation = $stmt->fetchall(pdo::fetch_assoc);              if ($rows < 1) {                 $response = array("status" => -1, "message" => "failed 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 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 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 $jsn) {     foreach($jsn $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 accept json array , insert table records.

can please? thank you..

  <?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 $jsn) {    foreach($jsn $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));  ?>  have used foreach array. 

Comments

Popular posts from this blog

mysql - Dreamhost PyCharm Django Python 3 Launching a Site -

java - Sending SMS with SMSLib and Web Services -

python 3.5 - Pyqtgraph string in x tick -