-1

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>
Vets
  • 76
  • 7
  • 1
    I do not see you echoing either the `$errorMsg` or `$successMsg` - perhaps this is why you are not seeing error messages on the screen ? – IVO GELOV Aug 10 '19 at 08:18
  • It's there... just below the other html code... ill add it to code for clarity... thanks – Vets Aug 10 '19 at 22:22

1 Answers1

0

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

IVO GELOV
  • 13,496
  • 1
  • 17
  • 26
  • Thanks ... Did those updates but no change in function. Submit button clicks as if it did something but nothing is going to the database. – Vets Aug 13 '19 at 06:15
  • Well, you could simply temporarily put a `var_dump($_POST)` in the beginning of the file to check what is coming from the browser. And then put an `echo ++$x` after each IF statement to see which statement is not taking place. – IVO GELOV Aug 13 '19 at 12:37
  • Thanks... I will do that now. – Vets Aug 13 '19 at 19:41
  • array(1) { ["csv_file"]=> string(0) "" } ... so it isn't capturing the file at all? – Vets Aug 13 '19 at 19:53
  • Changed button to input ... error gets farther now: array(1) { ["csv_file"]=> string(6) "Upload" } – Vets Aug 13 '19 at 20:11
  • And what is your reason to name both the submit button and the file field as `csv_file` ? The browser may be smart enough to include both in the POST request - but one of the fields will overwrite the other inside $_POST. – IVO GELOV Aug 14 '19 at 06:06
  • updated code changes...new error msg: array(1) { ["csv_file"]=> string(6) "Upload" } – Vets Aug 16 '19 at 16:15
  • Why did you leave the **file** field without a name ? The browser will skip (i.e. not submit) fields without a name. – IVO GELOV Aug 17 '19 at 13:17
  • I guess i don't know how to do it right. How should it be... ? – Vets Aug 18 '19 at 05:50
  • The name of the **file** field should be `"csv_file"` and the name of the **submit** button should be `"cmdUpload"` – IVO GELOV Aug 19 '19 at 06:27