'Import CSV file with auto generate code to MySQL database

I just want to ask if how can I import a CSV file to MySQL database with an auto-generated QR code? I can now import CSV file to database however I can't make it work with the QR code.

Here's the working code for importing a CSV file.

$conn = getdb();

 if(isset($_POST["Import"])){
    $filename=$_FILES["file"]["tmp_name"];
     if($_FILES["file"]["size"] > 0){
        $file = fopen($filename, "r");
          while (($getData = fgetcsv($file, 10000, ",")) !== FALSE){
             $sql = "INSERT into users (name)
                   values ('".$getData[0]."')";
                   $result = mysqli_query($conn, $sql);
        if(!isset($result)){
          echo "<script type=\"text/javascript\">
              alert(\"Invalid File:Please Upload CSV File.\");
              window.location = \"ad-bulk.php\"
              </script>";
        } else {
            echo "<script type=\"text/javascript\">
            alert(\"CSV File has been successfully Imported.\");
            window.location = \"ad-bulk.php\"
          </script>";
        }
           }
           fclose($file);
     }
  }

And this is the code that I'm using to generate a QR code. I already tried this code if my approach is to to add user one by one. However, I need to import a bulk of data.

$uniqueid = uniqid('IT-');
$text = $uniqueid;
$path = '../temp/';
$file_name= $path.$uniqueid.".png";
$ecc = 'L';
$pixel_Size = 20;
$frame_Size = 1;

// Generates QR Code and Stores it in directory given
QRcode::png($text, $file_name, $ecc, $pixel_Size, $frame_Size);

I tried this approach but it is not working..

if(isset($_POST["Import"])){
    $filename=$_FILES["file"]["tmp_name"];
     if($_FILES["file"]["size"] > 0){
        $file = fopen($filename, "r");

        $uniqueid = uniqid('IT-');
        $text = $uniqueid;
        $path = '../temp/';
        $file_name = $path.$uniqueid.".png";
        $ecc = 'L';
        $pixel_Size = 20;
        $frame_Size = 1;

        // Generates QR Code and Stores it in directory given
        QRcode::png($text, $file_name, $ecc, $pixel_Size, $frame_Size);

          while (($getData = fgetcsv($file, 10000, ",")) !== FALSE){

             $sql = "INSERT into users (code, name)
                   values ($text,'".$getData[0]."')";
                   $result = mysqli_query($conn, $sql);
        if(!isset($result)){
          echo "<script type=\"text/javascript\">
              alert(\"Invalid File:Please Upload CSV File.\");
              window.location = \"ad-bulk.php\"
              </script>";
        } else {
            echo "<script type=\"text/javascript\">
            alert(\"CSV File has been successfully Imported.\");
            window.location = \"ad-bulk.php\"
          </script>";
        }
           }

           fclose($file);
     }
  }


Solution 1:[1]

This is the solution that I came up with. Thank you!

$conn = getdb();

 if(isset($_POST["Import"])){
    $filename=$_FILES["file"]["tmp_name"];
     if($_FILES["file"]["size"] > 0)
     {
        $file = fopen($filename, "r");

          while (($getData = fgetcsv($file, 10000, ",")) !== FALSE){

            $uniqueid = uniqid('IT-');
            $text = $uniqueid;
            $path = '../temp/';
            $file_name = $path.$uniqueid.".png";
            $ecc = 'L';
            $pixel_Size = 20;
            $frame_Size = 1;

            // Generates QR Code and Stores it in directory given
            QRcode::png($text, $file_name, $ecc, $pixel_Size, $frame_Size);

            echo $getData[0] . "<br>";
            echo $text . "<br>";
            $sql = "INSERT into users (code, name)
                   values ('$text','".$getData[0]."')";
            echo $sql . "<br>" ;

           $result = mysqli_query($conn, $sql);

        if(!isset($result))
        {
          echo "<script type=\"text/javascript\">
              alert(\"Invalid File:Please Upload CSV File.\");
              window.location = \"ad-bulk.php\"
              </script>";
        }
        else {
            echo "<script type=\"text/javascript\">
            alert(\"CSV File has been successfully Imported.\");
            window.location = \"ad-bulk.php\"
          </script>";
        }
        
        }

           fclose($file);
     }
  }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 cjwrk