Insert / Update excel records into database


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?

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

Well, our guess is that you did not create the $highestRow correctly.
If you have 10 rows, replace that with 10…

1 Like

Try looping the procedure of fetching data fir every single row individually, just as you did for reading a single row, once you set the loop correctly then you will be able to read all 10 rows one by one.

Sponsor our Newsletter | Privacy Policy | Terms of Service