简体   繁体   English

使用php将csv上传到数据库表

[英]uploading csv to database table using php

I'm quite new to this, so hopefully I'm not way off base here: 我对此很陌生,所以希望我离这里不远:

I have a database named BT Database, with a table in it called cases_cases. 我有一个名为BT Database的数据库,其中有一个名为case_cases的表。 I want to be able to automatically upload a .csv file into it using a php script. 我希望能够使用php脚本自动将.csv文件上传到其中。 I made a php script and a test csv file, put them in together on the desktop of the XUbuntu machine that's hosting the database. 我制作了一个php脚本和一个测试csv文件,将它们放到托管数据库的XUbuntu计算机的桌面上。

I'm testing them by opening terminal, navigating to desktop, then using php -e (testimport.php) to run the script. 我通过打开终端,导航到桌面,然后使用php -e(testimport.php)运行脚本来测试它们。 I'm getting no errors, just a blank line, so it seems to be running okay, but when I go into the table, the data from the csv hasn't been uploaded. 我没有任何错误,只是一个空行,所以它似乎可以正常运行,但是当我进入表时,csv的数据尚未上传。 can anyone advise as to what I'm doing wrong here? 有人可以建议我在这里做错什么吗? my php script is below: 我的PHP脚本如下:

<?php
//connect to the database
    $connect = mysql_connect("localhost","user","password")
    or die("Could not connect.")
;
    mysql_select_db("BT Database",$connect);

//get the csv file 
    $handle = fopen("import.csv","r")
    or die("Couldn't find or open csv file")
;

//set up the query to load data into the table, then run it.
    $loadcases = "LOAD DATA INFILE 'import.csv'
                  INTO TABLE cases_cases
                  FIELDS TERMINATED BY ''
                  OPTIONALLY ENCLOSED BY '\"'
                  LINES TERMINATED BY ',,,\\r\\n'
                  IGNORE 1 LINES
                  (name, @dummy, case_specific_notes, type, initiation_date, @dummy,)"
    or die("error loading file.")
;
    mysql_query($loadcases)
;
?>

EDIT 1: Following everyone's good advice, I removed the comma, upgraded my script to mysqli, and called the actual mysql errors in my script. 编辑1:按照所有人的建议,我删除了逗号,将脚本升级为mysqli,并在脚本中调用了实际的mysql错误。 This showed a few previously unseen issues which were easily fixed. 这显示了一些以前看不见的问题,很容易解决。

However, now I'm getting the same issue as before, namely that it runs and gives no errors, but nothing shows up in the database table. 但是,现在我遇到了与以前相同的问题,即它运行并且没有错误,但是数据库表中没有任何显示。 The new script is below. 新脚本如下。 Any ideas what might be going wrong? 任何想法可能出什么问题吗?

<?php

$mysqli  =  new mysqli("localhost","xampp","bt4521","BT Database");
/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

$sql = "LOAD DATA INFILE 'sfimport/sfdumptest.csv' INTO TABLE cases_cases
        FIELDS TERMINATED BY ','
        LINES TERMINATED BY '\\r\\n'
        IGNORE 1 LINES
        (name, @dummy, case_specific_notes, type, initiation_date, @dummy, @dummy)
";

//Try to execute query (not stmt) and catch mysqli error from engine and php error
if (!($stmt = $mysqli->query($sql))) {
    echo "\nQuery execute failed: ERRNO: (" . $mysqli->errno . ") " . $mysqli->error;
};
?>

Seeing you may not be getting notifications for comments left under your question or if you've read them or not, am submitting the following in the answers area instead. 看到您可能没有收到问题下方的评论通知,或者您是否已阅读它们,请在“答案”区域中提交以下内容。

The trailing comma in @dummy,) shouldn't be there. @dummy,)的结尾逗号不应该出现。

Using die(mysql_error($connect)) would have thrown you a syntax error, rather than using a generic echo'd text. 使用die(mysql_error($connect))会抛出语法错误,而不是使用通用的echo'd文本。


Sidenote: Sure hoping that wasn't a bad paste/typo. 旁注:当然希望那不是不好的粘贴/打字错误。

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

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