通过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>
dongsou4301
dongsou4301 它就在那里...就在其他html代码的下方...为了清晰起见,将它添加到代码中...谢谢
大约一年之前 回复
doudi5291
doudi5291 我没有看到你回显$errorMsg或$successMsg-也许这就是为什么你没有在屏幕上看到错误消息?
大约一年之前 回复

1个回答



您的提交按钮没有名称 - 因此 $ _ POST ['submit'] </ code>未设置 。 此外, fclose($ file)</ code>应为 fclose($ handle)</ code>。 你应该检查 $ handle!== FALSE </ code>和 $ sql!== FALSE </ code>而不是 is_null()</ code>。</ p >
</ div>

展开原文

原文

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().

dpmopn8542
dpmopn8542 文件字段的名称应为“csv_file”,提交按钮的名称应为“cmdUpload”
大约一年之前 回复
duanjianfu1398
duanjianfu1398 我想我不知道该怎么做。 应该怎么样......?
大约一年之前 回复
dounan4479
dounan4479 你为什么没有名字就离开文件字段? 浏览器将跳过(即不提交)没有名称的字段。
大约一年之前 回复
duanfu5239
duanfu5239 更新的代码更改...新错误消息:array(1){[“csv_file”] => string(6)“上传”}
大约一年之前 回复
dougang2749
dougang2749 您将提交按钮和文件字段命名为csv_file的原因是什么? 浏览器可能足够智能,可以在POST请求中包含两者 - 但其中一个字段将覆盖$ _POST内的另一个字段。
大约一年之前 回复
dppcyt6157
dppcyt6157 更改按钮以输入...错误现在变得更远:array(1){[“csv_file”] => string(6)“Upload”}
大约一年之前 回复
douzhulv1699
douzhulv1699 array(1){[“csv_file”] => string(0)“”} ...所以它根本不捕获文件?
大约一年之前 回复
duanmajing9332
duanmajing9332 谢谢......我现在就会这样做。
大约一年之前 回复
doucao1888
doucao1888 好吧,你可以暂时在文件的开头放一个var_dump($ _ POST)来检查浏览器的内容。 然后在每个IF语句之后放置一个echo ++ $ x,以查看哪个语句没有发生。
大约一年之前 回复
douyou8047
douyou8047 谢谢......这些更新但功能没有变化。 提交按钮点击,就好像它做了一些事情,但没有任何东西进入数据库。
大约一年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问