使用 php、html 将 .csv 文件上传到 phpmyadmin
Using php, html to upload a .csv file to phpmyadmin
你好,我有这段代码是我通过这个问题 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\www21DIG\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\www21DIG\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\www21DIG\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
我使用的测试数据是
|编号 |名称 |业主 |附近 |产能 |规格 |
| 4 |黑水开采 |约翰·麦吉 |麦凯 | 200升|农业 |
| 5 |戴萨特矿业 |鲍勃·布朗 |翡翠| 210升|发电 |
| 6 |翡翠矿业 |科特·唐纳德 |罗坎普顿 | 160L |发电 |
如果你能帮忙,那就太好了,12 年级已经够难了,不需要做复杂的编码。
答案是“您正在使用 $items[1] 作为 ID,数组从 0 开始,所以这应该是 $items[0]。您还应该调整所有其他索引。”
你好,我有这段代码是我通过这个问题 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\www21DIG\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\www21DIG\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\www21DIG\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
我使用的测试数据是
|编号 |名称 |业主 |附近 |产能 |规格 |
| 4 |黑水开采 |约翰·麦吉 |麦凯 | 200升|农业 |
| 5 |戴萨特矿业 |鲍勃·布朗 |翡翠| 210升|发电 |
| 6 |翡翠矿业 |科特·唐纳德 |罗坎普顿 | 160L |发电 |
如果你能帮忙,那就太好了,12 年级已经够难了,不需要做复杂的编码。
答案是“您正在使用 $items[1] 作为 ID,数组从 0 开始,所以这应该是 $items[0]。您还应该调整所有其他索引。”