Hi,
Im trying to insert/update data from excel into sql server.
I tried and it works for both insert and update but it only read one row of excel, but i have about 10 rows in excel file. Im using phpExcel. Anyone can help me?
Thanks.
for($row = 1; $row <= $highestRow; $row++) {
if($row != 1)
{
$name= $sheet->getCell('A'.$row)->getValue();
$CustId= $sheet->getCell('B'.$row)->getValue();
$Address= $sheet->getCell('C'.$row)->getValue();
$query = "SELECT * FROM Customer WHERE CustID= ?";
$checkParam = array($CustId);
$result = sqlsrv_query($conn, $query, $checkParam);
$row = sqlsrv_fetch_array($result);
if(is_null($row))
{
$sql = "INSERT INTO Customer(Name, CustID, Address, iSelected) VALUES (?, ?, ?, 0)";
$params = array($name, $CustId, $Address);
$stmt = sqlsrv_query( $conn, $sql, $params);
echo "New record insert - ".$CustId."<br>";
}
else
{
$sql_update = "UPDATE Customer
SET Name= ?,
Address= ?
WHERE CustID= ?";
$param_update = array($Name, $CustId, $Address);
$stat_update = sqlsrv_query($conn, $sql_update, $param_update);
if($stat_update === false)
{
echo '<pre>';die( print_r( sqlsrv_errors(), true));echo '</pre>';
} echo "Record updated - ".$CustId."<br>";
}
}
}
echo “Table tblPlatformList : DONE”."\n";