0

I am new to php and am trying to pass an array of objects into a MySQL table. I want to be able to accept input data from the UI and and send it through to my DB table as rows. Currently, I am sending the hard coded array data from my code as shown below:

<?php
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: GET, POST");
header('Content-Type: application/json');

$mysqli = mysqli_connect("localhost", "root", "", "craftsillicon"); 

if ($mysqli->connect_error) {
   die('Error : ('. $mysqli->connect_errno .') '. $mysqli->connect_error);
}

$order_array = [   
    '0' => array('Reports'),
    '1' => array('Invoices'),
    '2' => array('Contacts'),
]; 

postArray($order_array, $mysqli); 

 function postArray($array, $mysqli)  
 {  
      if(is_array($array))  
      {  
           $values = array();  
           foreach($array as $row => $value)  
           {  
                $item_name = mysqli_real_escape_string($mysqli, $value[0]);                
                $values[] = "('$item_name')";  
           }  
           $sql = "INSERT INTO claims(name) VALUES";  
           $sql .= implode(', ', $values);          

           if ($mysqli = mysqli_query($mysqli,$sql)) {
            $rows= array();
            while($row = mysqli_fetch_assoc($mysqli))
             {
               $rows[] = $row;
             }
             echo json_encode($rows);
         } else{    
            echo json_encode(array('result'=>'fail'));
           }
      }
   }  
 ?>

The way it is right now it is posting the data to my db as is shown below: enter image description here

However, I want to be able to send this data through postman as is shown below: enter image description here

How can I refactor my code to accept user input and be able to test this through postman as is illustrated above?

Emmon
  • 377
  • 1
  • 9
  • 24
  • So you're looking to generate a JSON string which you can paste into Postman? – kmoser Jan 23 '21 at 06:12
  • @kmoser Yes, am looking for a way to be able to send the data using postman other than passing the array from within the code – Emmon Jan 23 '21 at 06:15

0 Answers0