将 CSV 文件放入 MYSQL 数据库不起作用
Putting CSV file in MYSQL database doesn't work
我在学校的一个网站上工作,我需要使用 php
在 mysql
数据库中上传一个 csv
文件。
我不能简单地将它上传到 phpMyAdmin。
现在我正在编写一些代码来为我做这件事。但是代码没有完成将我的 CSV 文件放入 mysql
数据库。我已经阅读了其他几个人提出的问题,但那里的答案对我没有帮助
我有 2 个文件,一个是
connection.php
$dbName = "i296297_studie";
$user = "username";
$password = "password";
$host = "localhost";
$connection = mysqli_connect($host, $user, $password, $dbName);
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
那个工作正常,一个是
test1.php
include "connection.php"; //connectie database
$deleterecords = "TRUNCATE TABLE olympischespelen"; //haalt de tabel leeg
mysqli_query($connection, $deleterecords);
//wanneer file is geupload
if (isset($_POST['submit'])) {
$csv_file = $_FILES['filename']['tmp_name'];
if (($getfile = fopen($csv_file, "r")) !== FALSE) {
$data = fgetcsv($getfile, 1000, ";");
while (($data = fgetcsv($getfile, 1000, ";")) !== FALSE) {
$num = count($data);
for ($c=0; $c < $num; $c++) {
$result = $data;
$str = implode(";", $result);
$slice = explode(";", $str);
$col1 = $slice[0];
$col2 = $slice[1];
$col3 = $slice[2];
$col4 = $slice[3];
$col5 = $slice[4];
$col6 = $slice[5];
// SQL Query to insert data into DataBase
$query = "INSERT INTO `i296297_studie`.`olympischespelen` (`sport`, `deelnemers`, `goud`, `zilver`, `brons`, `totaal`)
VALUES ('".$col1."','".$col2."','".$col3."','".$col4."','".$col5."','".$col6."')";
mysqli_query($query, $connection);
}
}
}
echo "File data successfully imported to database!!";
mysqli_close($connect);
}else {
print "Upload new csv by browsing to file and clicking on Upload<br />\n";
print "<form enctype='multipart/form-data' action='test1.php' method='post'>";
print "File name to import:<br />\n";
print "<input size='50' type='file' name='filename'><br />\n";
print "<input type='submit' name='submit' value='Upload'></form>";
}
?>
但是不知道哪里出错了。当我回显 $col1
或其他人时,他们正在输出我想要的数据。
希望你能帮助我谢谢
<?php
//connect to the database
$connect = mysql_connect("localhost","username","password");
mysql_select_db("mydatabase",$connect); //select the table
//
if ($_FILES[csv][size] > 0) {
//get the csv file
$file = $_FILES[csv][tmp_name];
$handle = fopen($file,"r");
//loop through the csv file and insert into database
do {
if ($data[0]) {
mysql_query("INSERT INTO contacts (contact_first, contact_last, contact_email) VALUES
(
'".addslashes($data[0])."',
'".addslashes($data[1])."',
'".addslashes($data[2])."'
)
");
}
} while ($data = fgetcsv($handle,1000,",","'"));
//
//redirect
header('Location: import.php?success=1'); die;
}
?>
<!DOCTYPE html>
<head>
<title>Import a CSV File with PHP & MySQL</title>
</head>
<body>
<?php if (!empty($_GET[success])) { echo "<b>Your file has been imported.</b><br><br>"; } //generic success notice ?>
<form action="" method="post" enctype="multipart/form-data" name="form1" id="form1">
Choose your file: <br />
<input name="csv" type="file" id="csv" />
<input type="submit" name="Submit" value="Submit" />
</form>
</body>
</html>
看看对你有没有帮助。
你调用这个 fgetcsv($getfile, 1000, ";");
两次。
试试这个
if (($getfile = fopen($csv_file, "r")) !== FALSE) {
while (($data = fgetcsv($getfile, 1000, ";")) !== FALSE) {
$num = count($data);
for ($c=0; $c < $num; $c++) {
$result = $data;
$str = implode(";", $result);
$slice = explode(";", $str);
$col1 = $slice[0];
$col2 = $slice[1];
$col3 = $slice[2];
$col4 = $slice[3];
$col5 = $slice[4];
$col6 = $slice[5];
// SQL Query to insert data into DataBase
$query = "INSERT INTO `i296297_studie`.`olympischespelen` (`sport`, `deelnemers`, `goud`, `zilver`, `brons`, `totaal`)
VALUES ('".$col1."','".$col2."','".$col3."','".$col4."','".$col5."','".$col6."')";
mysqli_query($query, $connection);
}
}
}
并且所有数据在插入数据库之前通过mysql_real_escape_string
函数过滤..
您需要将连接 link 作为第一个参数传递
mysqli_query($query, $connection);
应该是
mysqli_query($connection, $query);
我在学校的一个网站上工作,我需要使用 php
在 mysql
数据库中上传一个 csv
文件。
我不能简单地将它上传到 phpMyAdmin。
现在我正在编写一些代码来为我做这件事。但是代码没有完成将我的 CSV 文件放入 mysql
数据库。我已经阅读了其他几个人提出的问题,但那里的答案对我没有帮助
我有 2 个文件,一个是
connection.php
$dbName = "i296297_studie";
$user = "username";
$password = "password";
$host = "localhost";
$connection = mysqli_connect($host, $user, $password, $dbName);
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
那个工作正常,一个是
test1.php
include "connection.php"; //connectie database
$deleterecords = "TRUNCATE TABLE olympischespelen"; //haalt de tabel leeg
mysqli_query($connection, $deleterecords);
//wanneer file is geupload
if (isset($_POST['submit'])) {
$csv_file = $_FILES['filename']['tmp_name'];
if (($getfile = fopen($csv_file, "r")) !== FALSE) {
$data = fgetcsv($getfile, 1000, ";");
while (($data = fgetcsv($getfile, 1000, ";")) !== FALSE) {
$num = count($data);
for ($c=0; $c < $num; $c++) {
$result = $data;
$str = implode(";", $result);
$slice = explode(";", $str);
$col1 = $slice[0];
$col2 = $slice[1];
$col3 = $slice[2];
$col4 = $slice[3];
$col5 = $slice[4];
$col6 = $slice[5];
// SQL Query to insert data into DataBase
$query = "INSERT INTO `i296297_studie`.`olympischespelen` (`sport`, `deelnemers`, `goud`, `zilver`, `brons`, `totaal`)
VALUES ('".$col1."','".$col2."','".$col3."','".$col4."','".$col5."','".$col6."')";
mysqli_query($query, $connection);
}
}
}
echo "File data successfully imported to database!!";
mysqli_close($connect);
}else {
print "Upload new csv by browsing to file and clicking on Upload<br />\n";
print "<form enctype='multipart/form-data' action='test1.php' method='post'>";
print "File name to import:<br />\n";
print "<input size='50' type='file' name='filename'><br />\n";
print "<input type='submit' name='submit' value='Upload'></form>";
}
?>
但是不知道哪里出错了。当我回显 $col1
或其他人时,他们正在输出我想要的数据。
希望你能帮助我谢谢
<?php
//connect to the database
$connect = mysql_connect("localhost","username","password");
mysql_select_db("mydatabase",$connect); //select the table
//
if ($_FILES[csv][size] > 0) {
//get the csv file
$file = $_FILES[csv][tmp_name];
$handle = fopen($file,"r");
//loop through the csv file and insert into database
do {
if ($data[0]) {
mysql_query("INSERT INTO contacts (contact_first, contact_last, contact_email) VALUES
(
'".addslashes($data[0])."',
'".addslashes($data[1])."',
'".addslashes($data[2])."'
)
");
}
} while ($data = fgetcsv($handle,1000,",","'"));
//
//redirect
header('Location: import.php?success=1'); die;
}
?>
<!DOCTYPE html>
<head>
<title>Import a CSV File with PHP & MySQL</title>
</head>
<body>
<?php if (!empty($_GET[success])) { echo "<b>Your file has been imported.</b><br><br>"; } //generic success notice ?>
<form action="" method="post" enctype="multipart/form-data" name="form1" id="form1">
Choose your file: <br />
<input name="csv" type="file" id="csv" />
<input type="submit" name="Submit" value="Submit" />
</form>
</body>
</html>
看看对你有没有帮助。
你调用这个 fgetcsv($getfile, 1000, ";");
两次。
试试这个
if (($getfile = fopen($csv_file, "r")) !== FALSE) {
while (($data = fgetcsv($getfile, 1000, ";")) !== FALSE) {
$num = count($data);
for ($c=0; $c < $num; $c++) {
$result = $data;
$str = implode(";", $result);
$slice = explode(";", $str);
$col1 = $slice[0];
$col2 = $slice[1];
$col3 = $slice[2];
$col4 = $slice[3];
$col5 = $slice[4];
$col6 = $slice[5];
// SQL Query to insert data into DataBase
$query = "INSERT INTO `i296297_studie`.`olympischespelen` (`sport`, `deelnemers`, `goud`, `zilver`, `brons`, `totaal`)
VALUES ('".$col1."','".$col2."','".$col3."','".$col4."','".$col5."','".$col6."')";
mysqli_query($query, $connection);
}
}
}
并且所有数据在插入数据库之前通过mysql_real_escape_string
函数过滤..
您需要将连接 link 作为第一个参数传递
mysqli_query($query, $connection);
应该是
mysqli_query($connection, $query);