I am adding a csv file upload script for my webid auction site.
I tested it outside the webid script in a simple test database and it worked fine.
I need to make some changes to it. It should catch the user's id and create an auto increment item id. For now I can't even get it to work.
I also don't know if it will work unless all the fields in the csv file have values as they will not, since this is for a bulk lister and not all of the users add data to every field.
Here is the site I downloaded it from
http://www.k-fez.com/?p=101
Help would be very appreciated. This will allow people to upload thousands of listings at once.
Okay I will post a list of the database fields and the code itself.
The fields in the table are
id,user,title,subtitle,starts,description,pict_url,category,secondcat,minimum_bid,shipping_cost,reserve_price,buy_now,auction_type,duration,increment,shipping,payment,international,ends,current_bid,closed,photo_uploaded,quantity,suspended,relist,relisted,num_bids,sold,shipping_terms,bn_only,bold,highlighted,featured,current_fee
<?PHP
/*
File Name: importCSV.php
File URI: http://k-fez.com/
Description: This scripts verifies that a CSV file has been upload via HTTP POST and then inserts all rows into a MySQL database.
Author: Kevin Pheasey
Author URI: http://k-fez.com/
Version: 1.0
*/
/* Copyright (C) 2010 Kevin Pheasey (email: kevin at k-fez.com)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
if($_FILES["file"]["type"] != "application/vnd.ms-excel"){
die("This is not a CSV file.");
}
elseif(is_uploaded_file($_FILES['file']['tmp_name'])){
//Connect to the database
$dbhost = 'localhost';
$dbuser = 'root';
$dbname = 'webid';
$link = mysql_connect($dbhost, $dbuser) or die('Error connecting to mysql server');
mysql_select_db($dbname);
//Process the CSV file
$handle = fopen($_FILES['file']['tmp_name'], "r");
$data = fgetcsv($handle, 1000, ","); //Remove if CSV file does not have column headings
while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {
$att0 = mysql_real_escape_string($data[0]);
$att1 = mysql_real_escape_string($data[1]);
$att2 = mysql_real_escape_string($data[2]);
$att3 = mysql_real_escape_string($data[3]);
$att4 = mysql_real_escape_string($data[4]);
$att5 = mysql_real_escape_string($data[5]);
$att6 = mysql_real_escape_string($data[6]);
$att7 = mysql_real_escape_string($data[7]);
$sql = "INSERT INTO `webid_auctions` (
`id` ,
`user` ,
`title` ,
`subtitle` ,
`starts` ,
`description` ,
`pict_url` ,
`category` ,
)
VALUES ('" . $att0 . "', '" . $att1 . "', '" . $att2 . "', '" . $att3 . "', '" . $att4 . "', '" . $att5 . "', '" . $att6 . "', '" . $att7 . "')";
mysql_query($sql);
}
mysql_close($link);
echo "CSV file successfully imported.";
}
else{
die("You shouldn't be here");
}
?>
It doesn't upload anything into the database at this point, but shows the file was successfully uploaded message. I need also to fix the bugs I mentioned above. Thanks