社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  DATABASE

通过PHP上传CSV文件并插入MySQL数据库

Vets • 5 年前 • 657 次点击  

页面上没有错误消息。上传文件按钮工作。Upload按钮不会触发db insert,但页面会刷新,就像它触发了一样。

我不确定Excel中的数据数组是否在INSERT-to命令中正确编码。任何帮助都很感激,但请保持简单。我不是一个经验丰富的开发人员。非常绿色,主要使用程序编码。如果你用一个有经验的术语,不要以为我知道它的意思。

如果发布提交按钮代码。如果没有附加任何文件并单击提交按钮,则不会显示errorMsg

var_dump($_POST);

  if (isset($_POST['submit'])) {

    //print_r($_FILES);
    $ok = 'true';
    $file = $_FILES['csv_file']['tmp_name'];
    $handle = fopen($file, "r");
    echo ++$x;
        if ($handle !== FALSE){

          $errorMsg = "<br /><div align='center'><font color='red'>Please select a CSV file to import</font></div>";
          $ok = 'false';
          echo ++$x;

PHP继续-我没有看到上传的文件填充数据库

        } else {
            print_r(fgetcsv($handle));
            while(($filesop = fgetcsv($handle, 1000, ",")) !== FALSE){
              $email = mysqli_real_escape_string($con_db, $filesop[0]);
              $f_name = mysqli_real_escape_string($con_db, $filesop[1]);
              $l_name = mysqli_real_escape_string($con_db, $filesop[2]);
              $password = md5(mysqli_real_escape_string($con_db, $filesop[3]));
              $zipcode = mysqli_real_escape_string($con_db, $filesop[4]);
              $co_id = mysqli_real_escape_string($con_db, $filesop[5]);
              $employee = mysqli_real_escape_string($con_db, $filesop[6]);
              $assessment_ct = mysqli_real_escape_string($con_db, $filesop[7]);
                echo ++$x;
              $email = filter_var($email, FILTER_SANITIZE_EMAIL);

                if ( strlen($email) > 0) {
                  echo ++$x;

                  if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
                    echo ++$x;
                    $ok = 'false';
                    $errorMsg .= 'E-mail address is not correct';
                  }
                }
// error handling for password        
                if (strlen($password) >= 5) {
                    echo ++$x;
                    $ok = 'true';
                } else {
                    echo ++$x;
                    $ok = 'false';
                    $errorMsg .= 'Your password is too short (please use at least 5 characters)';
                }
 // If the tests pass we can insert it into the database.       
            if ($ok == 'true') {
                echo ++$x;
              $sql = mysqli_query($con_db, "INSERT INTO `myMembers` (email, f_name, l_name, password, zipcode, co_id, employee, assessment_ct) VALUES ('$email', '$f_name', '$l_name', '$password', '$zipcode', '$co_id', '$employee', '$assessment_ct')") or die ("Shit is not working");
            } else {// close if $ok == 'true'

            $result = print_r($handle);
            echo $handle.'<br>';
            echo ++$x;

            }

        } // close WHILE LOOP

        fclose($handle);

          if ($sql !== FALSE) {
          echo ++$x;
            $successMsg = 'Your database has imported successfully!';
            //print_r($_FILES);
            //header('excel_import.php');
          } else {
          echo ++$x;
            $errorMsg = 'Sorry! There is some problem in the import file.';
            //print_r($_FILES);
            //header('excel_import.php');
          }

    } // close if (!is_null($file)){
  } // close if $post = submit

用于提交上载文件的表单的HTML代码

<form enctype="multipart/form-data" method="POST" action="excel_import.php">
<div align="center">

<label>File Upload: </label><input type="file" id="csv_file"   accept=".csv" >
<p>Only Excel.CSV File Import</p>
<input type="submit" name="csv_file" class="btn myButton" value="Upload">
</div>
</form>
</div>
<div><?php echo $errorMsg; ?><?php echo $successMsg; ?></div>
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/50705
 
657 次点击  
文章 [ 1 ]  |  最新文章 5 年前
IVO GELOV
Reply   •   1 楼
IVO GELOV    5 年前

您的提交按钮没有名称,因此 $_POST['submit'] 未设置。也, fclose($file) 应该是 fclose($handle) . 你应该去看看 $handle !== FALSE $sql !== FALSE 而不是 is_null() .