0

How to upload csv file using php into mysql database correctly?

I have a column (uploaded, column name) in phpmyadmin, in this column will store the uploaded csv file.

I have a little script of uploading csv file into mysql database, but I'm getting some error "404: File Not Found" after I clicked the submit button and the csv file is not saving in the database.

Here the php file with html:

<?php
include "dbase.php"; //Connect to Database
$deleterecords = "TRUNCATE TABLE contracts"; //empty the table of its current records
 mysql_query($deleterecords);
//Upload File
if (isset($_POST['submit'])) {
if (is_uploaded_file($_FILES['filename']['tmp_name'])) {
    echo "<h1>" . "File ". $_FILES['filename']['name'] ." uploaded successfully." . "</h1>";
    echo "<h2>Displaying contents:</h2>";
    readfile($_FILES['filename']['tmp_name']);
}
//Import uploaded file to Database
$handle = fopen($_FILES['filename']['tmp_name'], "r");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
  $import="INSERT into contracts(uploaded) values('$data[0],$data[1]')";
    mysql_query($import) or die(mysql_error());
}
fclose($handle);
echo "Import done";
//view upload form
}else {
echo "Upload new csv by browsing to file and clicking on Upload<br />\n";
echo "<form enctype='multipart/form-data' action='upload.php' method='post'>";
echo "File name to import:<br />\n";
echo "<input size='50' type='file' name='filename'><br />\n";
echo "<input type='submit' name='submit' value='Upload'></form>";
 }
?>

Database connection:

 <?
 $filename=$_POST['filename'];
 $db=mysql_connect('localhost', 'username', 'password', '3306')
 or die(mysql_error());
 if(!$db) 
die("no db");
if(!mysql_select_db("databasename",$db))
die("No database selected.");
?>
1
  • why dont you store the file on your server then read? Commented Sep 12, 2014 at 3:55

2 Answers 2

1

I think your script file is named not upload.php, that is why you are getting "Not Found" error.

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

6 Comments

See. I change it, but when I click the upload it returns blank page and still the csv is not saving in the database
@User014019 There is may be some error happening. Add error_reporting(E_ALL); ini_set('display_errors'); on top of your script and execute it again. If there are any errors, you will see them.
I add that little code on top, still im getting this 404: File Not Found after I upload
@User014019 What was the problem?
but how can I remove this in result "Duplicate entry '0' for key 'PRIMARY'"
|
0

Here's a simple script that has worked for me before: http://coyotelab.org/php/upload-csv-and-insert-into-database-using-phpmysql.html

2 Comments

I already see that and tested, unfortunately its not working with me
The csv is not saving in the database

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.