Skip to content
Advertisement

Insert null or something if value of csv is null

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

Advertisement

Answer

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);
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement