Я пытаюсь вставить данные в таблицу «products» на моем SQL-сервере. Я не могу заполнить таблицу своим кодом. Я не знаю, в чем ошибка в моем коде.Невозможно вставить данные с помощью php в SQL
Ниже мой код:
<?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>";
?>
http://php.net/manual/en/function.error-reporting.php –