将excel文件导入数据库的PHP代码

标签 php mysql phpexcel

我想使用 PHPExcel 将 Excel 数据导入到我的数据库中。下面是我的代码。

当我上传 Excel 文件并提交表单时,我可以回显表格,但数据库中没有存储任何数据,任何人都可以帮我解决这个问题。 SQL 部分或创建数据数组出现问题。请任何人检查并帮助我。我不能使用 csv 文件,否则会更容易。

错误:您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,了解在“使用、标记、图像、链接”附近使用的正确语法 VALUES ('NULL', 'Silk 18', 'Silk 18', 'Machine Made', '1'在第 1 行

<?php
/************************ YOUR DATABASE CONNECTION START HERE   ****************************/


# Database Connection
$dbc = mysqli_connect('localhost', 'root', 'ron143', 'tcc') OR die('Error: '.mysqli_connect_error());


/************************ YOUR DATABASE CONNECTION END HERE  ****************************/


/** PHPExcel_IOFactory */
include 'PHPExcel/Classes/PHPExcel.php';

/** PHPExcel_IOFactory */
include 'PHPExcel/Classes/PHPExcel/IOFactory.php';

# Upload the file to server
$file = $_FILES["file"]["name"];
$target_dir = "../../temp/";
$target_file = $target_dir . basename($file);
move_uploaded_file($_FILES["file"]["tmp_name"], $target_file);
$xls_url = "$target_dir$file";

// This is the file info to be uploaded.
$inputFileName = $xls_url;
$inputFileType = $_POST['file_type'];
$sheetname = '0'; /** input the worksheet number to read. 0 for first and 1 for second worksheet */

try {

    /**  Create a new Reader of the type defined in $inputFileType  **/
    $objReader = PHPExcel_IOFactory::createReader($inputFileType);

    /**  Advise the Reader that we only want to load cell data and not its formating or any formula set on it **/
    #$objReader->setReadDataOnly(true);

    /**  Load $inputFileName to a PHPExcel Object  **/
    $objPHPExcel = $objReader->load($inputFileName);

} catch(Exception $e) {

    die('Error loading file "'.pathinfo($inputFileName,PATHINFO_BASENAME).'": '.$e->getMessage());

}

$allDataInSheet = $objPHPExcel->getSheet($sheetname)->toArray(null,true,true,true);
$arrayCount = count($allDataInSheet);  // Here get total count of row in that Excel sheet
 echo $arrayCount;

for($i=2;$i<=$arrayCount;$i++){
$product_name = trim($allDataInSheet[$i]["A"]);
$product_code = trim($allDataInSheet[$i]["B"]);
$category1 = trim($allDataInSheet[$i]["C"]);
$category1_id = trim($allDataInSheet[$i]["D"]);
$category2 = trim($allDataInSheet[$i]["E"]);
$category2_id = trim($allDataInSheet[$i]["F"]);
$stylename = trim($allDataInSheet[$i]["G"]);
$grams = trim($allDataInSheet[$i]["H"]);
$thickness = trim($allDataInSheet[$i]["I"]);
$width = trim($allDataInSheet[$i]["J"]);
$length = trim($allDataInSheet[$i]["K"]);
$color_ground = trim($allDataInSheet[$i]["L"]);
$color_border = trim($allDataInSheet[$i]["M"]);
$material = trim($allDataInSheet[$i]["N"]);
$backing = trim($allDataInSheet[$i]["O"]);
$reed = trim($allDataInSheet[$i]["P"]);
$weave = trim($allDataInSheet[$i]["Q"]);
$ply = trim($allDataInSheet[$i]["R"]);
$pile = trim($allDataInSheet[$i]["S"]);
$care = trim($allDataInSheet[$i]["T"]);
$precaution = trim($allDataInSheet[$i]["U"]);
$use = trim($allDataInSheet[$i]["V"]);
$tag = trim($allDataInSheet[$i]["W"]);
$image = trim($allDataInSheet[$i]["X"]);
$link = trim($allDataInSheet[$i]["Y"]);


$query = "SELECT product_code FROM products WHERE product_code = '".$product_code."'";
$sql = mysqli_query($dbc, $query);
$recResult = mysqli_fetch_assoc($sql);
$existCode = $recResult["product_code"];

$q = "INSERT INTO products (product_id, product_name, product_code, category1, category1_id, category2, category2_id, stylename, grams, thickness, width, length, color_ground, color_border, material, backing, reed, weave, ply, pile, care, precaution, use, tag, image, link)
VALUES ('NULL', '".$product_name."', '".$product_code."', '".$category1."', '".$category1_id."', '".$category2."', '".$category2_id."', '".$stylename."', '".$grams."', '".$thickness."', '".$width."', '".$length."', '".$color_ground."', '".$color_border."', '".$material."', '".$backing."', '".$reed."', '".$weave."', '".$ply."', '".$pile."', '".$care."', '".$precaution."', '".$use."', '".$tag."', '".$image."', '".$link."')";

     if (mysqli_query($dbc, $q)) {
    echo "New record created successfully";
} else {
    echo "Error: " . $q . "<br>" . mysqli_error($dbc);
}


}

echo "<div style='font: bold 18px arial,verdana;padding: 45px 0 0 500px;'>".$msg."</div>";

?>

最佳答案

您最好将 Excel 文件保存为 csv 文件并使用 php 解析它,而不使用任何其他库。

  • 使用fgets : 用于获取文件的一行
  • 使用explode :将 csv 文件行分解为数组。

关于将excel文件导入数据库的PHP代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29866702/

相关文章:

mysql - Snap Web 应用程序与 MySQL 连接

PHPExcel 输出不可读并且没有抛出错误

PHPExcel - 读取单元格并将其传递到 MYSQL

php - 可以从 php/c 文件创建 sql 函数

java - Windows 命令不在我的 PHP 脚本中执行

javascript - 使用 base64 编码数据更新 SQL 查询

mysql - 错误代码 1215 : cannot add foreign key constraint MySQL

mysql - 从另一台服务器恢复MySQL数据库,会影响当前数据库吗?

php - MySQL Group By using MAX 错误结果