douhao2721 2019-08-10 00:36
浏览 159

通过PHP上传CSV文件并插入MySQL DB

No error messages appear on the page. Upload File button works. Upload button doesn't trigger db insert but the page refreshes as if it did.

I am not sure if the array of data from Excel is being coded correctly on the INSERT Into command. Any help is appreciated but PLEASE keep it simple. I am not a seasoned developer. Very green and primarily use procedural coding. If you use an experienced term, don't assume I know what it means.

PHP - if Post Submit button code. The errorMsg doesn't display if no file is attached and submit button is clicked

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 continued - I'm not seeing the uploaded file populate the database

        } 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 Code for the form to submit uploaded file

<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>
  • 写回答

1条回答 默认 最新

  • dongqiao8417 2019-08-12 06:47
    关注

    Your submit button does not have a name - and thus $_POST['submit'] is not set. Also, fclose($file) should be fclose($handle). And you should be checking with $handle !== FALSE and $sql !== FALSE rather than with is_null().

    评论

报告相同问题?

悬赏问题

  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 关于#python#的问题:自动化测试