首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用php、html将.csv文件上传到phpmyadmin

问使用php、html将.csv文件上传到phpmyadmin
EN

Stack Overflow用户
提问于 2021-03-21 15:36:35
回答 1查看 31关注 0票数 1

您好,我有这个代码,我通过这个问题https://www.sitepoint.com/community/t/how-to-upload-a-csv-file-using-php/6270/13,我已经修改它,以满足我的需要,但我似乎遇到了一个问题,从问题中的人没有遇到

以下是我的代码

代码语言:javascript
复制
   <?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>

它不断地出现以下错误消息

(!)注意:未定义的偏移量: C:\wamp64\www\2021DIG\test.php中的第67行调用堆栈中的6

时间记忆功能位置

1 0.0005 411136 {main}( ) ...\test.php:0错误:插入到water_info_test集合Id=‘黑水挖掘’,名称=‘John Mcgee',所有者=’Mackay‘,Nearbyrc='200L',容量=’‘农场’,规格=‘’不正确的整数值:‘黑水挖掘’,列'Id‘在第1行(!)注意:未定义的偏移量: C:\wamp64\www\2021DIG\test.php中的第67行调用堆栈中的6

时间记忆功能位置

1 0.0005 411136 {main}( ) ...\test.php:0错误:插入到water_info_test集合Id='Dysart Mining',Namems='Bob Brown',Owner='Emerald',Nearbyrc='210L',Capacity='Power generation',Specification=‘’不正确的整数值:'Dysart Mining‘,列'Id’在第1行(!)注意:未定义的偏移量: C:\wamp64\www\2021DIG\test.php中的第67行调用堆栈中的6

时间记忆功能位置

1 0.0005 411136 {main}( ) ...\test.php:0错误:插入到water_info_test集合Id='Emerald Mining',Namems='Kert Dolanld',Owner='Rockamption',Nearbyrc='160L',Capacity=‘’发电‘,规格=’‘不正确的整数值:'Emerald Mining’为第1行的列'Id‘

这是如果图像更清晰error messages的图像

我使用的测试数据是

Id | Namems | Owner | Nearbyrc |容量|规格

|4|黑水采矿| John Mcgee | Mackay | 200L |养殖业

|5| Dysart Mining | Bob Brown | Emerald | 210L |发电

|6| Emerald Mining | Kert Donanld | Rockampton | 160L |发电

如果你能帮上忙,那就太好了,12年级已经够难的了,不用做复杂的编码。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-03-22 09:55:11

答案是“您正在使用$items1作为ID,数组从0开始,因此这应该是$items。您还应该调整所有其他索引。”

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66729873

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档