-1

with this update, It no shows any error, and when I sent the print, it shows the information that I want to add into the database but it does not add it...

if(isset($_POST["submit_file"]))
$file = $_FILES["file"]["tmp_name"];
$file_open = fopen($file,"r");
$count = 0; 
while(($csv = fgetcsv($file_open, 100, ",")) !== false) 
{ 
      $agente = isset($csv[0])? $csv[0]: null;
    $revenue = isset($csv[1])? $csv[1]: null;
    $css = isset($csv[2])? $csv[2]: null;
    $rating = isset($csv[3])? $csv[3]: null;
    $absent = isset($csv[4])? $csv[4]: null;
    $score = isset($csv[5])? $csv[5]: null;
    $count++;
    if($count>1){ 
      $query = "INSERT INTO metricas values ('$agente', $revenue, $css, $rating, $absent, $score)";
  print $query;  }

     }

?>

1 Answers1

0

Learn to indent your code properly, that will help you locate problems with quoting, parenthses, brackets, and braces.

You can also use [php Code Checker][https://phpcodechecker.com/] to locate parsing errors in your code.

<?php
require_once 'connect.php';

if(isset($_POST["submit_file"]))
$file = $_FILES["file"]["tmp_name"];
$file_open = fopen($file,"r");
$count = 0; 
while(($csv = fgetcsv($file_open, 100000, ",")) !== false) {
    //$agente[$csv[0]] = isset($csv[1])? $csv[1]: null; // This is wrong in terms of semantics. It should be as per the following line
    $agente = isset($csv[1])? $csv[1]: null;
    // Below lines also should be changed. Hence did change them.
    $revenue = isset($csv[2])? $csv[2]: null;
    $css = isset($csv[3])? $csv[3]: null;
    $rating = isset($csv[4])? $csv[4]: null;
    $absent = isset($csv[5])? $csv[5]: null;
    $score = isset($csv[6])? $csv[6]: null;
    $count++;
    if($count>1){ 
        $query = "INSERT INTO metricas values (null, '$agente', $revenue, $css, $rating, $absent, $score)";
    }
}   //  Added this to close the while loop.

?>

EDIT Modified the code to take out Warning: Array to String conversion.

Romeo Sierra
  • 1,666
  • 1
  • 17
  • 35
Sloan Thrasher
  • 4,953
  • 3
  • 22
  • 40