0

I have gathered some information from the Internet and wrote this small script to import a CSV file into a MySQL database. I think it is correct, but it's not working.

What am I doing wrong?

 <?php //connect to the database $connect = mysql_connect("localhost","testdb","testdb"); mysql_select_db("testdb",$connect); //select the table if ($_FILES[csv][size] > 0) { //get the csv file $file = $_FILES[csv][tmp_name]; $handle = fopen($file,"r"); //loop through the csv file and insert into database do { if ($data[0]) { mysql_query("INSERT INTO class12 (htno, subcode, subname, int_marks, ext_marks, result, credits) VALUES ( '".addslashes($data[0])."', '".addslashes($data[1])."', '".addslashes($data[2])."' '".addslashes($data[3])."' '".addslashes($data[4])."' '".addslashes($data[5])."' '".addslashes($data[6])."' '".addslashes($data[7])."' ) "); } } while ($data = fgetcsv($handle,3000,",","'")); // //redirect header('Location: import.php?success=1'); die; } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Import a CSV File</title> </head> <body> <?php if (!empty($_GET[success])) { echo "<b>Your file has been imported.</b><br><br>"; } //generic success notice ?> <form action="" method="post" enctype="multipart/form-data" name="form1" id="form1"> Choose your file: <br /> <input name="csv" type="file" id="csv" /> <input type="submit" name="Submit" value="Submit" /> </form> </body> </html> 

If possible, someone please include the code to auto-create an SQL table with a filename (without .csv extension) and then importing it.

3
  • stackoverflow.com/questions/18470359/… similar Commented Sep 2, 2013 at 19:18
  • what error do you get? Commented Sep 2, 2013 at 19:34
  • @Miguelo just showing me an empty page.. Commented Sep 2, 2013 at 19:50

2 Answers 2

1

You have missed the commas in the following lines

 '".addslashes($data[4])."' '".addslashes($data[5])."' '".addslashes($data[6])."' '".addslashes($data[7])."'` 

I have tested the code it's working fine.Vote if helpful

2.May be you have put table name here.You have to write database name.

mysql_select_db("testdb",$connect); //select the table

Sign up to request clarification or add additional context in comments.

5 Comments

actually it worked :) but still retuning an empty page.. is it possible to write some echo success message replacing header('Location: import.php?success=1'); die;
You can do that.Is your current filename import.php ?
do one thing, first make the demo of this johnboy.com/blog/tutorial-import-a-csv-file-using-php-and-mysql then manipulate everything slowly.Remove header location, remove mysql_query and put echo .Now loading the page results in printing the sql query.Copy query and run using phpMyAdmin.so that you can test yourself.
is it possible to include the code to auto-create an SQL table with filename (without .csv extension) and then importing it.
Yes. Trim the file name php.net/manual/en/function.substr.php and then create a table similar to johnboy.com/scripts/import-csv-file-with-php-mysql/contacts.sql in PHP code.
0

mysql_query("INSERT INTO class12 (htno, subcode, subname, int_marks, ext_marks, result, credits) VALUES ( '".addslashes($data[0])."', '".addslashes($data[1])."', '".addslashes($data[2])."' '".addslashes($data[3])."' '".addslashes($data[4])."' '".addslashes($data[5])."' '".addslashes($data[6])."' '".addslashes($data[7])."'//I think this is extra your table has 7 col only!

1 Comment

Welcome to SO, please remember to outlined your source code correctly.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.