Home Upload CSV File into multiple table in mysql while loop
Reply: 0

Upload CSV File into multiple table in mysql while loop

user2777
1#
user2777 Published in May 22, 2018, 4:35 am

i've been read similar question in this forum but i still cannot solve my problem so i decide to make the new one.

i want to upload CSV file (single) into mysql database, but i want insert it on different table, here is my code :

$handle = fopen($file_tmp, 'r');
  while (($myData = fgetcsv($handle,1000,';')) !== FALSE) {
    $p1 = mysqli_real_escape_string($db,$myData[0]);
    $p2 = mysqli_real_escape_string($db,$myData[1]);
    $p3 = mysqli_real_escape_string($db,$myData[2]);
    $p4 = mysqli_real_escape_string($db,$myData[3]);
    $p5 = mysqli_real_escape_string($db,$myData[4]);
    $p6 = mysqli_real_escape_string($db,$myData[5]);
    $p7 = mysqli_real_escape_string($db,$myData[6]);
    $p8 = mysqli_real_escape_string($db,$myData[7]);
    $p9 = mysqli_real_escape_string($db,$myData[8]);
    $p10 = mysqli_real_escape_string($db,$myData[9]);
    $p11 = mysqli_real_escape_string($db,$myData[10]);
    $p12 = mysqli_real_escape_string($db,$myData[11]);
    $p13 = mysqli_real_escape_string($db,$myData[12]);
    $p14 = mysqli_real_escape_string($db,$myData[13]);
    $p15 = mysqli_real_escape_string($db,$myData[14]);
    $p16 = mysqli_real_escape_string($db,$myData[15]);
    $p17 = mysqli_real_escape_string($db,$myData[16]);
    $p18 = mysqli_real_escape_string($db,$myData[17]);
    $p19 = mysqli_real_escape_string($db,$myData[18]);
    $p20 = mysqli_real_escape_string($db,$myData[19]);
    $p21 = mysqli_real_escape_string($db,$myData[20]);
    $p22 = mysqli_real_escape_string($db,$myData[21]);

    $sql = "START TRANSACTION;";
    $sql .= "INSERT INTO `kualitas_produk`(`responden`,`p1`,`p2`,`p3`,`p4`,`p5`,`p6`,`p7`,`p8`,`p9`) VALUES ('','$p1','$p2','$p3','$p4','$p5','$p6','$p7','$p8','$p9');";
    $sql .= "SELECT LAST_INSERT_ID() INTO @LASTID;";
    $sql .= "INSERT INTO `harga_produk`(`responden`,`p10`,`p11`,`p12`) VALUES (@LASTID,'$p10','$p11','$p12');";
    $sql .= "INSERT INTO `keputusan_pembelian`(`responden`,`p13`,`p14`,`p15`,`p16`,`p17`,`p18`,`p19`,`p20`,`p21`,`p22`) VALUES (@LASTID, '$p13','$p14','$p15', '$p16','$p17','$p18', '$p19','$p20','$p21', '$p22');";
    $sql .= "INSERT INTO `recap`(`responden`,`kualitas`,`harga`,`keputusan`) VALUES (@LASTID,@LASTID,@LASTID,@LASTID);";
    $sql .= "COMMIT;";

    mysqli_multi_query($db,$sql) or die("Error: ".mysqli_error($db));

  }

  fclose($handle);
  echo "Data Added";
  mysqli_close($db);

but i got this error

Commands out of sync; you can't run this command now

i already read about

mysqli_free_result, mysqli_next_result, mysqli_store_result

but i still dont understand how to use it. Any help will be appreciated.

Thank you.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.309593 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO