1

This question has probably been asked millions of times but I don't know why I can't find a code that works for me. I'm assigned this task to write a quite complex script to populate the database with an excel CSV (should accept both formats) spreadsheet. All I'm trying now is to see if I can even read the file Properly (before doing any manipulation/parsing). Below is what I have, can someone help to to have a basic working script for such a simple example?

PHP

<?php include 'connection.php';?>

<?php        


if(isset($_POST["search"]))
{
 $file = $_FILES['file_save']['tmp_name'];
 $handle = fopen($file, "r");

 while(($filesop = fgetcsv($handle, ",")) !== false)
 {
 $name = $filesop[0];
 $email = $filesop[1];

 $query = mysqli_query("INSERT INTO $dbname.CSV (fname, lname) VALUES ('$name','$email')");
 $result = mysqli_query($conn,$query);
 echo $query;
 }

}


?>

HTML

 <form method="post" action="#">

  Market Share:<br>
  <input type="file" name="file_save">
  <button style="" type="submit" class="btn btn-default" name="search">Save</button>
</form>
    <?php include 'save_import.php';?>
Bobby
  • 496
  • 5
  • 18
  • add **enctype="multipart/form-data"** in your form tag. – Mittul Chauhan Aug 23 '16 at 12:37
  • 1
    So what is your code doing and how does that differ from what you want it to do? – Patrick Q Aug 23 '16 at 12:38
  • @PatrickQ The above code is not seem to do anything even after adding "enctype="multipart/form-data"" – Bobby Aug 23 '16 at 12:42
  • $dbname `.csv` will be invalid, I think. – Teddy Codes Aug 23 '16 at 12:42
  • can you check this **if(isset($_POST["search"])){ print_r($_FILES); }** – Mittul Chauhan Aug 23 '16 at 12:43
  • @Robert I dont think that's the problem. "$dbname" is just the DB name name and CSV is the table name – Bobby Aug 23 '16 at 12:44
  • @Mit.agile this is the output `Array ( [file_save] => Array ( [name] => testfile.csv [type] => application/octet-stream [tmp_name] => /tmp/phpfLFS5j [error] => 0 [size] => 22 ) )` – Bobby Aug 23 '16 at 12:46
  • can you have a look at this please once http://stackoverflow.com/questions/1269562/how-to-create-an-array-from-a-csv-file-using-php-and-the-fgetcsv-function if that code works for you. – Mittul Chauhan Aug 23 '16 at 12:48
  • It's obviously doing _something_. It's up to you to figure out _what_. Put in some basic debugging. What is the result of your `fopen()` call? What's the result of your `fgetcsv()` call? Are you getting into your `while` loop? Etc, etc. – Patrick Q Aug 23 '16 at 12:50
  • or just **print_r($filesop)** in your while loop to make sure are you getting any value(s) or not. – Mittul Chauhan Aug 23 '16 at 12:53
  • It does get Into the While loop. But it seems to be an infinite loop, I tried to echo a simple text it worked, however it doesn't print anything when I execute the suggested command (print_r($filesop) (But the loop is still infinite, I can tell by the time taken to load the page) – Bobby Aug 23 '16 at 13:01
  • i did not play with your code yet but if u r ok enough to change your code then you can check this url once http://stackoverflow.com/questions/1269562/how-to-create-an-array-from-a-csv-file-using-php-and-the-fgetcsv-function – Mittul Chauhan Aug 23 '16 at 13:03
  • why are you using a php while loop vs a `load data infile` to a worktable – Drew Aug 23 '16 at 13:04
  • @Mit.agile It's essentially the same think except that the file is not being passed by the user – Bobby Aug 23 '16 at 13:06
  • but in that example, they have a file which exists in directory .. in your case its not. so instead of this line **$file = $_FILES['file_save']['tmp_name'];** if you can just add your static filename with respective file path to check whether its working or not. Like this **$file = 'yourpath/yourfilename.csv';** – Mittul Chauhan Aug 23 '16 at 13:08
  • if m not wrong, you must be following this tut http://coyotelab.org/php/upload-csv-and-insert-into-database-using-phpmysql.html correct ? – Mittul Chauhan Aug 23 '16 at 13:13
  • @Mit.agile Thanks a lot for that link, it helped A LOT!!! – Bobby Aug 23 '16 at 13:39
  • Please explore non PHP solutions for this – Drew Aug 23 '16 at 13:45
  • @Drew sorry for asking, but what will be the point of that? – Bobby Aug 23 '16 at 14:33
  • You can come and chat in [Campaigns](http://chat.stackoverflow.com/rooms/95290/campaigns) if you want and then summarize for others here. I am just trying to help you. – Drew Aug 23 '16 at 14:40
  • @Drew I see the chat but not sure how to type a message – Bobby Aug 23 '16 at 14:46
  • are you saying you cannot see the bottom text area, type something, and hit the Send button? – Drew Aug 23 '16 at 14:52
  • @Bobby glad it helped you .. happy to help. (y) – Mittul Chauhan Aug 24 '16 at 04:08

0 Answers0