0

The data I want to post into the json api body below is from a while loop. So I want to Select from the database and send multiple students data from the database. I can post individually but I'm unable to do it from a loop. How please? Thanks.

{
    "students":[
        {
            "admissionNumber": "2010",
            "class":"js one"
        },
        {
            "admissionNumber": "2020",
            "class":"ss one"
        }
    ],
    "appDomain":"www.example.com"
  }

Here's my code:

if(isset($_POST['submit'])){
    $sql = "SELECT * FROM students";          
    while($row = mysqli_fetch_array($sql){
          $admissionNumber = $row['admNo'];
          $class = $row['class'];

          $data = array('students' => array(['admissionNumber' => $admNo, 'class' => $class]),                         
          'appDomain' => "http://example.com",

            );     
            echo $data;   

                $url = 'https://the-end-point';

                $params = $data;
                $data_string = json_encode($data);
                $curl = curl_init();
                $curl = curl_init();
                curl_setopt_array($curl, array(
                    CURLOPT_URL => $url,
                    CURLOPT_RETURNTRANSFER => true,
                    CURLOPT_ENCODING => "",
                    CURLOPT_MAXREDIRS => 10,
                    CURLOPT_TIMEOUT => 30,
                    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                    CURLOPT_CUSTOMREQUEST => "POST",
                    CURLOPT_POSTFIELDS => $data_string,
                    CURLOPT_HTTPHEADER => array(
                        "cache-control: no-cache",
                        "content-type: application/json",
                    ),
                ));

                $response = curl_exec($curl);
                $err = curl_error($curl);
                curl_close($curl);
                if ($err) {
                    echo "cURL Error #:" . $err;
                } else {
                     echo $response;
                }    

             }       
                        echo "<script>
                          alert(' Student Created Successfully!');
                         window.location = '.../students.php';
                         </script>";    
         }                     
     }
}
  • It is a very bad idea to use `die(mysqli_error($conn));` in your code, because it could potentially leak sensitive information. See this post for more explanation: [mysqli or die, does it have to die?](https://stackoverflow.com/a/15320411/1839439) – Dharman Jun 14 '20 at 16:12
  • You never execute your SQL query. You need to send the SQL string to the MySQL server – Dharman Jun 14 '20 at 16:13
  • Thanks. You can see the if(isset){} function now. I particularly have an issue with the api post request itself. What do you recommend please? If I use static POST variables in the data it sends well, but sending from the while loop fails completely. – Joseph Adegbola Jun 14 '20 at 16:38

0 Answers0