Upload CSV to Mysql
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | <?php if(isset($_POST['upload_excel'])) { error_reporting(0); include ("conn.php"); //-- Truncate/Delete Table data-- $sql = "TRUNCATE TABLE products"; mysqli_query($db,$sql); //--// if(isset($_POST["upload_excel"])) { $file = $_FILES['file']['tmp_name']; $handle = fopen($file, "r"); $c = 0; while(($filesop = fgetcsv($handle, 10000000, ",")) !== false) { $code=time(); $product_name=$filesop[1]; $category=$filesop[2]; //============================== $price=$filesop[3]; //---------- $sql=mysqli_query($db,"INSERT INTO products (product_name,category,price) VALUES ('$product_name','$category','$price')") or die(mysqli_error($db)); $c = $c + 1; } if($sql){ echo "<div class='alert alert-success' role='alert'>You database has imported successfully. You have inserted ". $c ." recoreds </div>"; }else{ echo "<div class='alert alert-danger' role='alert'>Sorry! There is some problem.</div>"; } } } ?> |