-2

its my first time asking a question here, i hope someone can help me with this issue and make me understand this. I am new to php programing language and i am trying to create a small program to retrieve data from a external booking system, into my database.

I created this code to retrieve the data and insert into the database, but nothing happens, its strange because when i check the array value print_r(array_values($array)); i can see the correct results are retrieved from the system, however i am having a hard time inserting this into the database.

<?php

// error reporting
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);


$link = mysqli_connect("127.0.0.1", "db_username", "db_password", "db");

if (!$link) {
    echo "Error: Unable to connect to MySQL." . PHP_EOL;
    echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
    echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
    exit;
}

/*echo "Success: A proper connection to MySQL was made! The my_db database is great." . PHP_EOL;
 echo "Host information: " . mysqli_get_host_info($link) . PHP_EOL;
 */

$auth = array();
$auth['apiKey'] = 'API_KEY:EXTERNAL_SYSTEM';
$auth['propKey'] = 'SPECIFIC_KEY';

$data = array();
$data['authentication'] = $auth;

$data['bookId'] = 'BOOKING_ID';
$data['email'] = 'EMAIL';


$json = json_encode($data);

$url = "EXTERNAL_WEBSITE/json/getBookings";

$ch=curl_init();
curl_setopt($ch, CURLOPT_POST, 1) ;
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
$result = curl_exec($ch);
curl_close ($ch);

$array = json_decode($result, true);

foreach ($array as $row) {

$sql = "INSERT INTO bookings (bookId, roomId, unitId, roomQty, status, firstNight, lastNight) VALUES ('".row["bookId"]."', '".row["roomId"]."', 
'".row["unitId"]."', '".row["roomQty"]."', '".row["status"]."', '".row["firstNight"]."', '".row["lastNight"]."' )";

mysqli_query ($link, $sql);
}

?>
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141

1 Answers1

-1

You forgot $ before row. All variables name start from $ symbol

foreach ($array as $row) {

    $sql = "INSERT INTO bookings (bookId, roomId, unitId, roomQty, status, firstNight, lastNight) 
    VALUES ('".$row["bookId"]."', '".$row["roomId"]."', '".$row["unitId"]."', '".$row["roomQty"]."', '".$row["status"]."', '".$row["firstNight"]."', '".$row["lastNight"]."' )";

    mysqli_query ($link, $sql);
}
Mike Foxtech
  • 1,633
  • 1
  • 6
  • 7