<?php

    ## open connection to the database
    require_once 'conn.php';


    //============================================================================================
    // save entire contact array to MySQL
    //
    //============================================================================================
    ## using POD so it works on other databases
    ## open connection to the database

      // set the PDO error mode to exception
      $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

      // prepare sql and bind parameters
      $stmt = $conn->prepare("INSERT INTO SMSContacts (clientID, firstname, lastname, initials, type, email, phone, notes, localdatetime )
                VALUES (:clientId, :firstname, :lastname, :initials, :type, :email, :phone, :notes, :localdateTime )");



                //Get the data for clients with firstname and name from file
                    $allClientsfile = "data/contacts.json"; //Get existing JSON file

                    $jsondata = file_get_contents($allClientsfile); //Read data from existing JSON file
                    $arr_data = json_decode($jsondata, true); // converts existing JSON data into an Array

                    $size = sizeof($arr_data); //get total number of clients in the exiting client array and display

                    echo "<BR> Existing Contact Array Size: " . $size . "<BR>";

                    foreach ($arr_data as $key1 => $value1) {

                      echo "383 " . $clientId . "<br>";

                            $newClientId = $arr_data[$key1]["clientId"];
                            $firstName = $arr_data[$key1]["firstname"];
                            $lastName = $arr_data[$key1]["lastname"];
                            $initials = $arr_data[$key1]["initials"];
                            $type = $arr_data[$key1]["type"];
                            $email = $arr_data[$key1]["email"];
                            $phonenum =  $arr_data[$key1]["phone"];
                            $notes = $arr_data[$key1]["notes"];

                      $stmt->bindParam(':clientId', $newClientId);
                      $stmt->bindParam(':firstname', $firstName);
                      $stmt->bindParam(':lastname', $lastName);
                      $stmt->bindParam(':initials', $initials);
                      $stmt->bindParam(':type', $type);
                      $stmt->bindParam(':email', $email);
                      $stmt->bindParam(':phone', $phonenum);
                      $stmt->bindParam(':notes', $notes);
                      $stmt->bindParam(':localdateTime', $dateTime);
                      $stmt->execute();

                      echo "New records created successfully";

                      //catch(PDOException $e) {
                    //      echo "Error: " . $e->getMessage();
                      //}

                    };

          $conn = null;


?>



?>
