0

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>";

?>
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Pretty Man
  • 11
  • 1

1 Answers1

1

I think problems might be the instance name of the $serverName. I don't think the server name should have the parentheses. For how to check the instance of the server name here link:

Connecting to SQL Server Express - What is my server name?

Community
  • 1
  • 1