I'm trying to update a database from an excel spreadsheet using phpexcel. Everything seems to run normally but the database remains empty.
Here's my code:
<?php
$con=mysqli_connect("localhost","root","password","PIX");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
echo $con->host_info . "
<br><br";
/** PHPExcel_IOFactory */
require_once dirname(__FILE__) . '/Classes/PHPExcel/IOFactory.php';
$fileName = "Highlights.xls";
$objReader = PHPExcel_IOFactory::createReaderForFile($fileName);
$objReader->setReadDataOnly();
$objPHPExcel = $objReader->load($fileName);
$allDataInSheet = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
$arrayCount = count($allDataInSheet); // Total Number of rows in the uploaded EXCEL file
echo $arrayCount."<br>";
for($i=1;$i<=$arrayCount;$i++){
$string = '"'."INSERT INTO data (`pixName`) VALUES ";
$pixName= trim($allDataInSheet[$i]["A"]);
$string .= "('".$pixName."')".'"';
echo $string."<br>";
mysqli_query ($con, $string);
}
mysqli_close($con);
?>
Now, I find it weird because I checked the value of $string and it is what it's supposed to be:
"INSERT INTO data (pixName) VALUES ('20091202tbg001')"
I do not have any error message but nothing appears in my database. What am I not seeing here?
Thank you all so much for your time.
Please try the following:
for($i=1;$i<=$arrayCount;$i++){
$string = "INSERT INTO data (`pixName`) VALUES ";
$pixName= trim($allDataInSheet[$i]["A"]);
$string .= "('".$pixName."')";
echo $string."<br>";
mysqli_query ($con, $string);
}
Your mistake is adding "(double quotes) in this query
You do not need to add "..." quotes in query. Just remove it and check
for($i=1;$i<=$arrayCount;$i++){
$string = "INSERT INTO data (`pixName`) VALUES "; // Removed quotes
$pixName= trim($allDataInSheet[$i]["A"]);
$string .= "('".$pixName."')"; // Removed quotes
echo $string."<br>";
mysqli_query ($con, $string);
}