php - 使用 Ajax 和 PHP 导入 csv 将 csv 导入到 mysql,而不是上传文件

标签 php mysql ajax

我有以下页面

  • index.php
  • 导入数据.php

我正在尝试使用ajax将csv文件导入MySql 我在网上使用了一些示例,但仍然无法工作 索引.php

开头的脚本文件:

$(document).ready(function(){

    $("#but_import").click(function() {
        var formData = new FormData($("#import_form"));     
            $.ajax({
                url:"importData.php",
                data:formData,
                type:'POST',
                success:function(response) 
                {
                    var resp = $.trim(response);
                    $("#output").html(resp);
                }else[

                    $("#output").html("Error");

                });

    });
   }); 

脚本后的 HTML:

<html>
<head>
    <title>Import CSV file data to the MySQL using PHP</title>

</head>
<body>
 <form id="import_form" method="post" action="" enctype="multipart/form-data" >
        <table width="100%">

            <tr>
                <td style="width: 58px">
                    <input type='file' name="score_file" id="score_file">
                </td>

            </tr>
            <tr>
                <td colspan="2" >
                <input type="button" id="but_import" name="but_import" value="Import File"></td>
            </tr>

        </table>
    </form>


<table style="width: 100%">
    <tr>
        <td id="output">&nbsp;</td>
    </tr>
</table>
</body>

最佳答案

我现在设法获取发布的信息,执行以下操作

$(document).ready(function() {

$("#but_import").click(function() {
    var formData = new FormData($('score_file')[0]);        



    alert(formData);    


        $.ajax({
             url: 'importData.php',
               data: formData,
              type: 'POST',
              contentType: false, 
                 processData: false,                
                 success:function(response) {
                        var resp = $.trim(response);
                        $("#message").html("yes");

                        }
        });

});

});

现在在 php 表单上我收到以下错误 未定义索引:importData.php 第 6 行中的 Score_file

这是html文件中的输入框

<form id="import_form" name ="import_form" method="post" action="post" enctype="multipart/form-data" >
        <table width="100%">

            <tr>
                <td style="width: 58px">
                    <input type='file' name="score_file" id="score_file">
                </td>

            </tr>
            <tr>
                <td colspan="2" >
                <input type="button" id="but_import" name="but_import" value="Import File"></td>
            </tr>

        </table>
    </form>

和php文件的前2行

 $csvMimes = array('text/x-comma-separated-values', 'text/comma-separated-values', 'application/octet-stream', 'application/vnd.ms-excel', 'application/x-csv', 'text/x-csv', 'text/csv', 'application/csv', 'application/excel', 'application/vnd.msexcel', 'text/plain');
if(!empty($_FILES['score_file']['name']) && in_array($_FILES['score_file']['type'],$csvMimes)){

错误在第二行

关于php - 使用 Ajax 和 PHP 导入 csv 将 csv 导入到 mysql,而不是上传文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50930349/

相关文章:

php - 根据列值对数据行进行分组

c# - Unity 3d调用带有json请求的post api

javascript - 如何使用Ajax在特定时间后重新加载/刷新 'id'?

php - 没有数据从 Android 插入到 MySQL

c# - .Net MySql 用户定义变量作为输出参数

javascript - jQuery加载动画hide()、show()错误

javascript - 如何将 "string"转换为 "timestamp without time zone"

java - 使用用户签名在服务器文档上签署 PDF

php - 如何显示 SQL 数据库中的数据?

mysql - 使用 If 或 Case(或任何其他方法)根据国家/地区代码递增值