I'm having a bit of a problem trying to import data from a CSV and have a couple of questions on it that I haven't managed to solve myself yet.
First off here's my code to help put things in perspective (tidied it up a bit, removing CSS and DB connection):
<body>
<div id="container">
<div id="form">
<?php
$deleterecords = "TRUNCATE TABLE tablename"; //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 importing(text,number)values('$data[0]','$data[1]')";
mysql_query($import) or die(mysql_error());
}
fclose($handle);
print "Import done";
//view upload form
} else {
print "Upload new csv by browsing to file and clicking on Upload<br />
";
print "<form enctype='multipart/form-data' action='upload.php' method='post'>";
print "File name to import:<br />
";
print "<input size='50' type='file' name='filename'><br />
";
print "<input type='submit' name='submit' value='Upload'></form>";
}
?>
</div>
</div>
</body>
It's basically an adaptation of an example I have found after many many attempts at various methods.
My CSV has two columns of data, the first one being text and the second is integers
The table in the database also has two columns, the first called "text" and the second "number"
So the questions I have are:
- the text being uploaded is just being displayed as 0 in every field and i'm not sure why
- I keep reading about data ending up enclosed in "", if that happens how would I sort it?
- how can I ignore the first X lines of the CSV for headers etc?
- is the data format changed throughout this process or is it ready for me to use in a graph? e.g. would a decimal stay a decimal once placed in the database?
I think that covers everything, thanks in advance for any help!
EDIT:
Just done a test of 10,000 record uploading and got the error:
"Fatal error: Maximum execution time of 30 seconds exceeded"
any thoughts?
Question&Answers:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…