0

Possible Duplicate:
How I can Import data from CSV to MySQL?

i have a rather large database that needs to be updated yearly. i want to be able to just fill out a spreadsheet and have it upload right to my database.

In my first half I am getting the notice : Notice: Undefined index: csv in E:\New folder\xampp\htdocs\Importing\quickimport.php on line 7. Also, when I actually try to upload my chosen CSV file, i get the 404 not found error. Does anyone have any idea as to why this isn't working?

This is my current code: After a few more edits, I now have

<?php  


$connect = mysql_connect("localhost","root"); 
mysql_select_db("contacts",$connect);

if(isset($_FILES['csv'])){
if ($_FILES['csv']['size'] > 0) { 

    $file = $_FILES[csv][tmp_name]; 
    $handle = fopen($file,"r"); 

    do { 
        if ($data[0]) { 
            mysql_query("INSERT INTO contacts (contact_first, contact_last, contact_email) VALUES 
                ( 
                    '".addslashes($data[0])."', 
                    '".addslashes($data[1])."', 
                    '".addslashes($data[2])."' 
                ) 
            "); 
        } 
    } while ($data = fgetcsv($handle,1000,",","'")); 

    header('Location: quickimport.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 with PHP & MySQL</title> 
</head> 

<body> 

<?php if (!empty($_GET['success'])) { echo "<b>Your file has been imported.</b><br><br>"; }     //generic success notice ?> 

<form 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> 
Community
  • 1
  • 1
Stephen Sparks
  • 113
  • 1
  • 8
  • For the first error, try wrapping your csv handling code in `if(isset($_FILES['csv'])){...}`. For the second, try removing `action=""` from your form tag (just leave the action out - it will post to the current url by default). – Jon Hulka Jan 17 '13 at 19:18
  • @JonHulka K i changed that, as well as some of my other errors, now it makes the table, but does not fill in values. ;_; – Stephen Sparks Jan 17 '13 at 19:23
  • This line: `$file = $_FILES[csv][tmp_name];` should use apostrophes: `$file = $_FILES['csv']['tmp_name'];`. Some servers will not allow you to access the temporary file, so you may need to use `move_uploaded_file()` first. – Patrick Moore Jan 17 '13 at 19:30
  • 1
    It looks like this will fail on `if($data[0])` on the first iteration, since data is not defined yet. change `do{...}while($data = fgetcsv($handle,1000,",","'"));` to `while ($data = fgetcsv($handle,1000,",","'")){...}` – Jon Hulka Jan 17 '13 at 19:31
  • You might also want to echo the sql string to see what it looks like. – Jon Hulka Jan 17 '13 at 19:33

0 Answers0