0

I have a problem uploading a csv to mysql.

I have the following csv. I need to upload the marked fields the max fields the csv can contain are 48 but some csv contain only 36 or less. I have a form which if csv contains 48 cells uploads succesfully to mysql but if the csv is less than 48 I got error.

This is my form:

<div class="container">
<?php
if(isset($_POST['uploadBtn'])){
    $fileName=$_FILES['myFile']['name'];
    $fileTmpName=$_FILES['myFile']['tmp_name'];
   
    $fileExtension=pathinfo($fileName,PATHINFO_EXTENSION);

    $allowedType = array('csv');
    if(!in_array($fileExtension,$allowedType)){?>

        <div class="alert alert-danger">INVALID FILE</div>
<?php 
    }else{
        $handle = fopen($fileTmpName, 'r');
        $k = 0;
        $hardness = array ();
        while (($myData = fgetcsv($handle,1000,',')) !== FALSE) {
            $k++;
            if ( $k > 13 ) {
                $hardness[] = $myData[3];
            }
        }
            
        $query = "INSERT INTO metlab.resultados_dureza_junta 
            (a_od_r1, a_od_r2, a_od_r3, a_mod_r1, a_mod_r2, 
            a_mod_r3, a_mid_r1, a_mid_r2, a_mid_r3, a_id_r1, 
            a_id_r2, a_id_r3,b_od_r1, 
            b_od_r2, b_od_r3, b_mod_r1, 
            b_mod_r2, b_mod_r3, b_mid_r1, b_mid_r2, b_mid_r3, 
            b_id_r1, b_id_r2, b_id_r3,
            c_od_r1, c_od_r2, c_od_r3, c_mod_r1, c_mod_r2, c_mod_r3, 
            c_mid_r1, c_mid_r2, c_mid_r3, c_id_r1, c_id_r2, c_id_r3,
            d_od_r1) 
        VALUES  ($hardness[0],$hardness[1],$hardness[2],$hardness[3],
                $hardness[4],$hardness[5],$hardness[6],$hardness[7],
                $hardness[8], $hardness[9],$hardness[10],$hardness[11],
                $hardness[12],$hardness[13],$hardness[14],$hardness[15],
                $hardness[16],$hardness[17], $hardness[18], $hardness[19], 
                $hardness[20], $hardness[21],$hardness[22],$hardness[23], 
                $hardness[24],$hardness[25],$hardness[26],$hardness[27],
                $hardness[28],$hardness[29],$hardness[30],$hardness[31],
                $hardness[32],$hardness[33],$hardness[34],$hardness[35],
                IFNULL($hardness[36],DEFAULT(0)))";
            //ON $hardness[36],DEFAULT I was trying to insert something so I can solve the problem but I cant.
                                                                                                                    
    var_dump($hardness);
    $run = mysql_query($query);

    if(!$run){
        die("error in uploading file".mysql_error());
    }else{ ?>
        <div class="alert alert-success">SUCCESS</div>
    <?php   
    }
}
}
?>

<form action="" method="post" enctype="multipart/form-data">
    <h3 class="text-center">
        RESULTS
    </h3></hr>
    <div class="row">
        <div class="col-md-6">
            <div class="form-group">
                <input type="file" name="myFile" class="form-control">
            </div>
        </div>
    </div>
    <div class="row">
        <div class="col-md-6">
            <div class="form-group">
                <input type="submit" name ="uploadBtn" class="btn btn-info">
            </div>
        </div>
    </div>
</form>

CSV example:

csv example

Error:

error

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Jiga
  • 13
  • 8
  • What is your error? I'm going to guess [Undefined index](https://stackoverflow.com/questions/4261133/php-notice-undefined-variable-notice-undefined-index-and-notice-undef)? – Scuzzy Nov 06 '17 at 22:37
  • the error is displayed on the last image of my post, I printed the array so I can see its content, when the script finds a null value on the csv i get that error – Jiga Nov 06 '17 at 22:43
  • Do you have 48 columns for metlab.resultados_dureza_junta? – Bluetree Nov 07 '17 at 01:28
  • I found the error myself dude, thanks for posting anyway :) – Jiga Nov 07 '17 at 16:10

2 Answers2

0

I would simply try padding your $myData with null

$myData = array_pad( $myData, 48, null );

realistically you should improve your SQL query to not process data that is missing, or review your default null value.

Scuzzy
  • 12,186
  • 1
  • 46
  • 46
  • what that 48 means because 48 its the max of cells the csv can contain because there are csv's that contain less than 48 sorry im noob – Jiga Nov 06 '17 at 22:45
  • It will make sure the array is always 48 entries in length, since fgetcsv reads line per line, it has no idea of the max column length, unless you were to loop through the document finding the max cell length first. – Scuzzy Nov 06 '17 at 23:02
  • and what happen if is not 48 entries length? – Jiga Nov 06 '17 at 23:11
  • so that part of code you provided goes inside of while? – Jiga Nov 06 '17 at 23:23
0

dirty solution but it works

$hardness = array (0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
Jiga
  • 13
  • 8