简体   繁体   English

使用php将Excel数据导入到SQL数据库

[英]Importing excel data in to sql database using php

I am trying to import data from excel sheet to my sql database using the following php code: 我正在尝试使用以下php代码将数据从excel工作表导入到我的sql数据库:

$hostname = "localhost";
$username = "root";
$password = "";
$database = "unhrd_fund_balance";


$conn = mysqli_connect("$hostname","$username","$password","$database") or die(mysql_error());


?>

<form name="import" method="post" enctype="multipart/form-data">
        <input type="file" name="file" /><br />
        <input type="submit" name="submit" value="Submit" />
</form>



<?php
if(isset($_POST["submit"]))
{
    $file = $_FILES['file']['tmp_name'];
    $handle = fopen($file, "r");
    $c = 0;
    while(($filesop = fgetcsv($handle, 1000, ",")) !== false)
    {
        $progkey = $filesop[0];
        $grantkey = $filesop[1];
        $TOD = $filesop[2];
        $TDD=$filesop[3];
        $fund=$filesop[4];
        $orderkey=$filesop[5];
        $budget=$filesop[6];
        $precommit=$filesop[7];
        $commit=$filesop[8];
        $actuals=$filesop[9];
        $totalcommit=$filesop[10];
        $availbudget=$filesop[11];

        $sql = mysqli_query("INSERT INTO csv (FUNDED_PROG_KEY, GRANT_KEY, TOD, TDD, FUND, ORDER_KEY, BUDGET_ALLOC, PRE_COMMIT, COMMIT, ACTUALS, TOTAL_COMMIT,AVAILABLE_BUDGET) VALUES ('$progkey', '$grantkey','$TOD','$TDD','$fund','$orderkey','$budget','$precommit','$commit,'$actuals','$totalcommit','$availbudget')");


    }

        if($sql){
            echo "You database has imported successfully";
        }else{
            echo "Sorry! There is some problem.";
        }
}
?>

My SQL database is: 我的SQL数据库是:

CREATE TABLE `fund_balances` (
  `FUNDED_PROG_KEY` varchar(20) NOT NULL,
  `GRANT_KEY` varchar(25) NOT NULL,
  `TOD` date NOT NULL,
  `TDD` date NOT NULL,
  `FUND` varchar(35) NOT NULL,
  `ORDER_KEY` int(30) NOT NULL,
  `BUDGET_ALLOC` bigint(255) NOT NULL,
  `PRE_COMMIT` bigint(255) NOT NULL,
  `COMMIT` bigint(255) NOT NULL,
  `ACTUALS` bigint(255) NOT NULL,
  `TOTAL_COMMIT` bigint(255) NOT NULL,
  `AVAILABLE_BUDGET` bigint(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

I am getting the following errors when I attach the xlsx file and click on submit in the form: 附加xlsx文件并单击表单中的提交时,出现以下错误:

Notice: Undefined offset: 3 in /Applications/XAMPP/xamppfiles/htdocs/Web/Form1.php on line 36 注意:未定义的偏移量:第36行的/Applications/XAMPP/xamppfiles/htdocs/Web/Form1.php中的3

Notice: Undefined offset: 4 in /Applications/XAMPP/xamppfiles/htdocs/Web/Form1.php on line 37 注意:未定义的偏移量:第37行的/Applications/XAMPP/xamppfiles/htdocs/Web/Form1.php中的4

Notice: Undefined offset: 5 in /Applications/XAMPP/xamppfiles/htdocs/Web/Form1.php on line 38 注意:未定义的偏移量:第38行的/Applications/XAMPP/xamppfiles/htdocs/Web/Form1.php中的5

Notice: Undefined offset: 6 in /Applications/XAMPP/xamppfiles/htdocs/Web/Form1.php on line 39 注意:未定义的偏移量:第39行的/Applications/XAMPP/xamppfiles/htdocs/Web/Form1.php中的6

Notice: Undefined offset: 7 in /Applications/XAMPP/xamppfiles/htdocs/Web/Form1.php on line 40 注意:未定义的偏移量:第40行的/Applications/XAMPP/xamppfiles/htdocs/Web/Form1.php中的7

Notice: Undefined offset: 8 in /Applications/XAMPP/xamppfiles/htdocs/Web/Form1.php on line 41 注意:未定义的偏移量:第41行的/Applications/XAMPP/xamppfiles/htdocs/Web/Form1.php中的8

Notice: Undefined offset: 9 in /Applications/XAMPP/xamppfiles/htdocs/Web/Form1.php on line 42 注意:未定义的偏移量:第42行的/Applications/XAMPP/xamppfiles/htdocs/Web/Form1.php中的9

Notice: Undefined offset: 10 in /Applications/XAMPP/xamppfiles/htdocs/Web/Form1.php on line 43 注意:未定义的偏移量:/Applications/XAMPP/xamppfiles/htdocs/Web/Form1.php中的第10行上的10

Notice: Undefined offset: 11 in /Applications/XAMPP/xamppfiles/htdocs/Web/Form1.php on line 44 注意:未定义的偏移量:第44行/Applications/XAMPP/xamppfiles/htdocs/Web/Form1.php中的11

First, those are not errors, but notices telling you there is no N'th element in the $filesop array. 首先,这些不是错误,但是会通知$filesop数组中没有第N个元素。

Second, I do not think you can read an XLSX file with fgetcsv , you will need an external library for that ( PHPExcel for example). 其次,我认为您无法使用fgetcsv读取XLSX文件,您将需要一个外部库(例如PHPExcel )。

Third, you are INSERT ing into csv , while the name of your table is fund_balances . 第三,您要INSERT csv ,而表名是fund_balances

Furthermore, this: "$hostname" is useless, just use $hostname . 此外,这: "$hostname"是没有用的,只需使用$hostname

Finally, read up on SQL injection and prepared statements . 最后,阅读SQL注入准备好的 语句

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM