【发布时间】:2021-06-18 03:26:00
【问题描述】:
您好,我有这段代码是通过这个问题https://www.sitepoint.com/community/t/how-to-upload-a-csv-file-using-php/6270/13 得到的,我已经对其进行了修改以满足我的需要,但我似乎遇到了问题中的人没有遇到的问题
这是我的代码
<?php
$has_title_row = true;
if ($_SERVER['REQUEST_METHOD'] == 'POST'){
if(is_uploaded_file($_FILES['csvfile']['tmp_name'])){
$filename = basename($_FILES['csvfile']['name']);
if(substr($filename, -3) == 'csv'){
$tmpfile = $_FILES['csvfile']['tmp_name'];
if (($fh = fopen($tmpfile, "r")) !== FALSE) {
$i = 0;
while (($items = fgetcsv($fh, 10000, ",")) !== FALSE) {
if($has_title_row === true && $i == 0){ // skip the first row if there is a tile row in CSV file
$i++;
continue;
}
// print_r($items);
// $i++;
}
}
}
else{
die('Invalid file format uploaded. Please upload CSV.');
}
}
else{
die('Please upload a CSV file.');
}
}
//data
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "mine_water_test";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$has_title_row = true;
$not_done = array();
if ($_SERVER['REQUEST_METHOD'] == 'POST'){
if(is_uploaded_file($_FILES['csvfile']['tmp_name'])){
$filename = basename($_FILES['csvfile']['name']);
if(substr($filename, -3) == 'csv'){
$tmpfile = $_FILES['csvfile']['tmp_name'];
if (($fh = fopen($tmpfile, "r")) !== FALSE) {
$i = 0;
while (($items = fgetcsv($fh, 10000, ",")) !== FALSE) {
if($has_title_row === true && $i == 0){ // skip the first row if there is a tile row in CSV file
$i++;
continue;
}
//print_r($items);
//$sql = "INSERT into water_info_test (Id,Namems,Owner,Nearbyrc,Capacity,Specifications)
//values (?,?,?,?,?,?)";
$sql = "INSERT INTO water_info_test SET
Id='{$items[1]}',
Namems='{$items[2]}',
Owner='{$items[3]}',
Nearbyrc='{$items[4]}',
Capacity='{$items[5]}',
Specifications='{$items[6]}'";
if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
$i++;
}
}
// if there are any not done records found:
if(!empty($not_done)){
echo "<strong>There are some records could not be inseted</strong><br />";
print_r($not_done);
}
}
else{
die('Invalid file format uploaded. Please upload CSV.');
}
}
else{
die('Please upload a CSV file.');
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<meta name="author" content="Raju Gautam" />
<title>Test</title>
</head>
<body>
<form enctype="multipart/form-data" action="" method="post" id="add-courses">
<table cellpadding="5" cellspacing="0" width="500" border="0">
<tr>
<td class="width"><label for="image">Upload CSV file : </label></td>
<td><input type="hidden" name="MAX_FILE_SIZE" value="10000000" /><input type="file" name="csvfile" id="csvfile" value=""/></td>
<td><input type="submit" name="uploadCSV" value="Upload" /></td>
</tr>
</table>
</form>
</body>
</html>
它不断出现这些错误消息
( ! ) 注意:未定义的偏移量:67 行 C:\wamp64\www\2021DIG\test.php 中的 6 调用栈
时间记忆功能定位
1 0.0005 411136 {main}( ) ...\test.php:0 错误:INSERT INTO water_info_test SET Id='Black Water Mining',Namems='John Mcgee',Owner='Mackay',Nearbyrc='200L',Capacity='Farming',Specifications='' 不正确的整数值:第 1 行的“Id”列的“Black Water Mining” ( ! ) 注意:未定义的偏移量:第 67 行 C:\wamp64\www\2021DIG\test.php 中的 6 调用栈
时间记忆功能定位
1 0.0005 411136 {main}( ) ...\test.php:0 错误:INSERT INTO water_info_test SET Id='Dysart Mining',Namems='Bob Brown',Owner='Emerald',Nearbyrc='210L',Capacity='Power generation',Specifications='' 不正确的整数值:第 1 行的列“Id”的“Dysart Mining” ( ! ) 注意:未定义的偏移量:第 67 行 C:\wamp64\www\2021DIG\test.php 中的 6 调用栈
时间记忆功能定位
1 0.0005 411136 {main}( ) ...\test.php:0 错误:INSERT INTO water_info_test SET Id='Emerald Mining',Namems='Kert Dolanld',Owner='Rockamption',Nearbyrc='160L',Capacity='Power generation',Specifications='' 不正确的整数值:第 1 行的“Id”列的“Emerald Mining”
如果图像更清晰,这里是图像 error messages
我使用的测试数据是
|身份证 |姓名 |所有者 | Nearbyrc |容量 |规格 |
| 4 |黑水开采 |约翰·麦基 |麦凯 | 200L |农业 |
| 5 | Dysart 矿业 |鲍勃布朗 |翡翠 | 210L |发电 |
| 6 |翡翠开采 |克特·多南德 |罗克安普顿 | 160L |发电 |
如果您能提供帮助,那就太好了,12 年级已经足够难了,无需进行复杂的编码。
【问题讨论】:
-
您使用
$items[1]作为 ID,数组从 0 开始,所以应该是$items[0]。您还应该调整所有其他索引。 -
"上传 .csv 文件到 phpmyadmin" 很确定
phpMyAdmin已经能够上传.csv文件。您是否知道phpMyAdmin“只是”一个用于管理您的MySQL服务器/数据库的界面/UI? -
感谢 Nigel,提供了这么多的帮助,现在一切正常
-
我建议你关闭/删除这个然后
标签: php html mysql phpmyadmin