I am trying to insert data inside 'products' table in my SQL server. I am not able to populate the table with my code. I do not know what is the mistake in my code.
Below is my code:
<?php
/* Specify the server and connection string attributes. */
$serverName = "DBIT-NB1415546\SQLEXPRESS(SQL Server 1)"; // Change this to the name of your own SQL Server
$connectionInfo = array( "Databases"=>"CA1_OLTP");
/* Connect using Windows Authentication. */
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn === false ) {
echo "Unable to connect.</br>";
die( print_r( sqlsrv_errors(), true));
}
//read JSON data from the cloud DB
$json = file_get_contents("http://bi.edisonsiow.com/ay1516s2/ca1/getProducts.php");
$jsonObject = json_decode($json);
//go through each record in the JSON object and create a record in employees table
//in MS SQL Server DB
foreach ($jsonObject as $key => $item ){
$sql = "INSERT INTO products(productCode, productName,".
"productLine,productScale,productVendor,".
"productDescription,quantityInStock,buyPrice,MSRP)".
"VALUES(?,?,?,?,?,?,?,?,?)";
$param = array(&$item->productCode, &$item->productName, &$item->productLine, &$item->productScale,
&$item->productVendor, &$item->productDescription, &$item->quantityInStock, &$item->buyPrice,
&$item->MSRP);
$stmt = sqlsrv_prepare($conn, $sql, $param);
if( sqlsrv_execute($stmt) === false ) {
$errors = sqlsrv_errors();
if($errors[0]['code']==2627) {
die("Employee already exist in employeeDIM. No duplication is allowed.");
} //end inner if
} //end if
}
echo "<h3>Records Created..</h3>";
?>