CSV File With PHP and MySQL - Data not uploading properly in database -
i download script - http://www.k-fez.com/?p=101 website , trying upload csv file on server. uploading file blank - please check image
<?php session_start(); if($_files["file"]["type"] != "application/vnd.ms-excel"){ die("this not csv file."); } elseif(is_uploaded_file($_files['file']['tmp_name'])){ //connect database $dbhost = 'localhost'; $dbuser = 'root'; $dbpass = 'password'; $dbname = 'auction_tbl'; $link = mysql_connect($dbhost, $dbuser, $dbpass) or die('error connecting mysql server'); mysql_select_db($dbname); //process csv file $findings = " <form method=\"post\" action=\"importcsv.php\"> <table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\"> <tr> <td>checking...</td> "; $handle = fopen($_files['file']['tmp_name'], "r"); $data = fgetcsv($handle, 10000, ";"); //remove if csv file not have column headings while (($data = fgetcsv($handle, 10000, ";")) !== false) { $auction_id = mysql_real_escape_string($data[0]); $name = mysql_real_escape_string($data[1]); $description = mysql_real_escape_string($data[2]); $owner_id = mysql_real_escape_string($data[3]); $list_in = mysql_real_escape_string($data[4]); $auction_type = mysql_real_escape_string($data[5]); $start_time = mysql_real_escape_string($data[6]); $start_time_type = mysql_real_escape_string($data[7]); $end_time = mysql_real_escape_string($data[8]); $end_time_type = mysql_real_escape_string($data[9]); $duration = mysql_real_escape_string($data[10]); $quantity = mysql_real_escape_string($data[11]); $category_id = mysql_real_escape_string($data[12]); $addl_category_id = mysql_real_escape_string($data[13]); $currency = mysql_real_escape_string($data[14]); $start_price = mysql_real_escape_string($data[15]); $reserve_price = mysql_real_escape_string($data[16]); $buyout_price = mysql_real_escape_string($data[17]); $is_offer = mysql_real_escape_string($data[18]); $offer_min = mysql_real_escape_string($data[19]); $offer_max = mysql_real_escape_string($data[20]); $enable_swap = mysql_real_escape_string($data[21]); $bid_increment_amount = mysql_real_escape_string($data[22]); $zip_code = mysql_real_escape_string($data[23]); $state = mysql_real_escape_string($data[24]); $country = mysql_real_escape_string($data[25]); $postage_amount = mysql_real_escape_string($data[26]); $insurance_amount = mysql_real_escape_string($data[27]); $item_weight = mysql_real_escape_string($data[28]); $shipping_method = mysql_real_escape_string($data[29]); $shipping_int = mysql_real_escape_string($data[30]); $type_service = mysql_real_escape_string($data[31]); $shipping_details = mysql_real_escape_string($data[32]); $payment_methods = mysql_real_escape_string($data[33]); $direct_payment = mysql_real_escape_string($data[34]); $hpfeat = mysql_real_escape_string($data[35]); $catfeat = mysql_real_escape_string($data[36]); $bold = mysql_real_escape_string($data[37]); $hl = mysql_real_escape_string($data[38]); $hidden_bidding = mysql_real_escape_string($data[39]); $apply_tax = mysql_real_escape_string($data[40]); $auto_relist_bids = mysql_real_escape_string($data[41]); $auto_relist_nb = mysql_real_escape_string($data[42]); $force_payment = mysql_real_escape_string($data[43]); $fb_decrement_amount = mysql_real_escape_string($data[44]); $fb_decrement_interval = mysql_real_escape_string($data[45]); $fb_next_decrement = mysql_real_escape_string($data[46]); $images_details = mysql_real_escape_string($data[47]); $media_details = mysql_real_escape_string($data[48]); $dd_details = mysql_real_escape_string($data[49]); $custom_fields_details = mysql_real_escape_string($data[50]); $import_date = mysql_real_escape_string($data[51]); //check if row in database $sql = "select * `bulk_listings` `auction_id` = '" . $auction_id . "'"; //in example attribute0 primary key $result = mysql_query($sql); $count = mysql_num_rows($result); if($count > 0){ $findings = $findings . " <tr> <td bgcolor=\"#ff0000\">db duplicate</td> <td>" . $auction_id . "</td> <td>" . $name . "</td> <td>" . $description . "</td> <td>" . $owner_id . "</td> <td>" . $list_in . "</td> <td>" . $auction_type . "</td> <td>" . $start_time . "</td> <td>" . $start_time_type . "</td> <td>" . $end_time . "</td> <td>" . $end_time_type . "</td> <td>" . $duration . "</td> <td>" . $quantity . "</td> <td>" . $category_id . "</td> <td>" . $addl_category_id . "</td> <td>" . $currency . "</td> <td>" . $start_price . "</td> <td>" . $reserve_price . "</td> <td>" . $buyout_price . "</td> <td>" . $is_offer . "</td> <td>" . $offer_min . "</td> <td>" . $offer_max . "</td> <td>" . $enable_swap . "</td> <td>" . $bid_increment_amount . "</td> <td>" . $zip_code . "</td> <td>" . $state . "</td> <td>" . $country . "</td> <td>" . $postage_amount . "</td> <td>" . $insurance_amount . "</td> <td>" . $item_weight . "</td> <td>" . $shipping_method . "</td> <td>" . $shipping_int . "</td> <td>" . $type_service . "</td> <td>" . $shipping_details . "</td> <td>" . $payment_methods . "</td> <td>" . $direct_payment . "</td> <td>" . $hpfeat . "</td> <td>" . $catfeat . "</td> <td>" . $bold . "</td> <td>" . $hl . "</td> <td>" . $hidden_bidding . "</td> <td>" . $apply_tax . "</td> <td>" . $auto_relist_bids . "</td> <td>" . $auto_relist_nb . "</td> <td>" . $force_payment . "</td> <td>" . $fb_decrement_amount . "</td> <td>" . $fb_decrement_interval . "</td> <td>" . $fb_next_decrement . "</td> <td>" . $images_details . "</td> <td>" . $media_details . "</td> <td>" . $dd_details . "</td> <td>" . $custom_fields_details . "</td> <td>" . $import_date . "</td> </tr>"; } //check if row in insert queue elseif(strpos($_session['insert'], "'" . $auction_id . "'") != false){ $findings = $findings . " <tr> <td bgcolor=\"#ff0000\">file duplicate</td> <td>" . $auction_id . "</td> <td>" . $name . "</td> <td>" . $description . "</td> <td>" . $owner_id . "</td> <td>" . $list_in . "</td> <td>" . $auction_type . "</td> <td>" . $start_time . "</td> <td>" . $start_time_type . "</td> <td>" . $end_time . "</td> <td>" . $end_time_type . "</td> <td>" . $duration . "</td> <td>" . $quantity . "</td> <td>" . $category_id . "</td> <td>" . $addl_category_id . "</td> <td>" . $currency . "</td> <td>" . $start_price . "</td> <td>" . $reserve_price . "</td> <td>" . $buyout_price . "</td> <td>" . $is_offer . "</td> <td>" . $offer_min . "</td> <td>" . $offer_max . "</td> <td>" . $enable_swap . "</td> <td>" . $bid_increment_amount . "</td> <td>" . $zip_code . "</td> <td>" . $state . "</td> <td>" . $country . "</td> <td>" . $postage_amount . "</td> <td>" . $insurance_amount . "</td> <td>" . $item_weight . "</td> <td>" . $shipping_method . "</td> <td>" . $shipping_int . "</td> <td>" . $type_service . "</td> <td>" . $shipping_details . "</td> <td>" . $payment_methods . "</td> <td>" . $direct_payment . "</td> <td>" . $hpfeat . "</td> <td>" . $catfeat . "</td> <td>" . $bold . "</td> <td>" . $hl . "</td> <td>" . $hidden_bidding . "</td> <td>" . $apply_tax . "</td> <td>" . $auto_relist_bids . "</td> <td>" . $auto_relist_nb . "</td> <td>" . $force_payment . "</td> <td>" . $fb_decrement_amount . "</td> <td>" . $fb_decrement_interval . "</td> <td>" . $fb_next_decrement . "</td> <td>" . $images_details . "</td> <td>" . $media_details . "</td> <td>" . $dd_details . "</td> <td>" . $custom_fields_details . "</td> <td>" . $import_date . "</td> </tr>"; } //row unique else{ //add insert statement insert queue $_session['insert'] .= "insert `bulk_listings` ( `auction_id`, `name`, `description`, `owner_id`, `list_in`, `auction_type`, `start_time`, `start_time_type`, `end_time`, `end_time_type`, `duration`, `quantity`, `category_id`, `addl_category_id`, `currency`, `start_price`, `reserve_price`, `buyout_price`, `is_offer`, `offer_min`, `offer_max`, `enable_swap`, `bid_increment_amount`, `zip_code`, `state`, `country`, `postage_amount`, `insurance_amount`, `item_weight`, `shipping_method`, `shipping_int`, `type_service`, `shipping_details`, `payment_methods`, `direct_payment`, `hpfeat`, `catfeat`, `bold`, `hl`, `hidden_bidding`, `apply_tax`, `auto_relist_bids`, `auto_relist_nb`, `force_payment`, `fb_decrement_amount`, `fb_decrement_interval`, `fb_next_decrement`, `images_details`, `media_details`, `dd_details`, `custom_fields_details`, `import_date` ) values ('" . $auction_id . "', '" . $name . "', '" . $description . "', '" . $owner_id . "', '" . $list_in . "', '" . $auction_type . "', '" . $start_time . "', '" . $start_time_type . "', '" . $end_time . "', '" . $end_time_type . "', '" . $duration . "', '" . $quantity . "', '" . $category_id . "', '" . $addl_category_id . "', '" . $currency . "', '" . $start_price . "', '" . $reserve_price . "', '" . $buyout_price . "', '" . $is_offer . "', '" . $offer_min . "', '" . $offer_max . "', '" . $enable_swap . "', '" . $bid_increment_amount . "', '" . $zip_code . "', '" . $state . "', '" . $country . "', '" . $postage_amount . "', '" . $insurance_amount . "', '" . $item_weight . "', '" . $shipping_method . "', '" . $shipping_int . "', '" . $type_service . "', '" . $shipping_details . "', '" . $payment_methods . "', '" . $direct_payment . "', '" . $hpfeat . "', '" . $catfeat . "', '" . $bold . "', '" . $hl . "', '" . $hidden_bidding . "', '" . $apply_tax . "', '" . $auto_relist_bids . "', '" . $auto_relist_nb . "', '" . $force_payment . "', '" . $fb_decrement_amount . "', '" . $fb_decrement_interval . "', '" . $fb_next_decrement . "', '" . $images_details . "', '" . $media_details . "', '" . $dd_details . "', '" . $custom_fields_details . "', '" . $import_date . "');"; //add row row findings table , mark unique $findings = $findings . " <tr> <td bgcolor=\"#00ff00\"> </td> <td>" . $auction_id . "</td> <td>" . $name . "</td> <td>" . $description . "</td> <td>" . $owner_id . "</td> <td>" . $list_in . "</td> <td>" . $auction_type . "</td> <td>" . $start_time . "</td> <td>" . $start_time_type . "</td> <td>" . $end_time . "</td> <td>" . $end_time_type . "</td> <td>" . $duration . "</td> <td>" . $quantity . "</td> <td>" . $category_id . "</td> <td>" . $addl_category_id . "</td> <td>" . $currency . "</td> <td>" . $start_price . "</td> <td>" . $reserve_price . "</td> <td>" . $buyout_price . "</td> <td>" . $is_offer . "</td> <td>" . $offer_min . "</td> <td>" . $offer_max . "</td> <td>" . $enable_swap . "</td> <td>" . $bid_increment_amount . "</td> <td>" . $zip_code . "</td> <td>" . $state . "</td> <td>" . $country . "</td> <td>" . $postage_amount . "</td> <td>" . $insurance_amount . "</td> <td>" . $item_weight . "</td> <td>" . $shipping_method . "</td> <td>" . $shipping_int . "</td> <td>" . $type_service . "</td> <td>" . $shipping_details . "</td> <td>" . $payment_methods . "</td> <td>" . $direct_payment . "</td> <td>" . $hpfeat . "</td> <td>" . $catfeat . "</td> <td>" . $bold . "</td> <td>" . $hl . "</td> <td>" . $hidden_bidding . "</td> <td>" . $apply_tax . "</td> <td>" . $auto_relist_bids . "</td> <td>" . $auto_relist_nb . "</td> <td>" . $force_payment . "</td> <td>" . $fb_decrement_amount . "</td> <td>" . $fb_decrement_interval . "</td> <td>" . $fb_next_decrement . "</td> <td>" . $images_details . "</td> <td>" . $media_details . "</td> <td>" . $dd_details . "</td> <td>" . $custom_fields_details . "</td> <td>" . $import_date . "</td> </tr>"; } } mysql_close($link); $findings = $findings . " <tr> <td colspan=\"5\"><div align=\"center\"><input type=\"submit\" value=\"confirm\" /></div></td> </tr> </table> </form>"; echo $findings; } else{ die("you shouldn't here"); } ?>
importcsv.php (sorry forgot not file code.)
<?php session_start(); if(!isset($_session['insert'])){ die("you shouldn't here."); } //connect database $dbhost = 'localhost'; $dbuser = 'root'; $dbpass = 'password'; $dbname = 'auction_tbl'; $link = mysql_connect($dbhost, $dbuser, $dbpass) or die('error connecting mysql server'); mysql_select_db($dbname); $queries = explode(';', $_session['insert']); foreach($queries $query){ if($query != ""){ mysql_query($query); } } mysql_close($link); echo "done"; ?>
thanks
//get csv file $file = $_files[csv][tmp_name]; $handle = fopen($file,"r");
//loop through csv file , insert database { if ($data[0]) { mysql_query("insert contacts_tmp (contact_first, contact_last, contact_email) values ( '".addslashes($data[0])."', '".addslashes($data[1])."', '".addslashes($data[2])."' ) "); } } while ($data = fgetcsv($handle,1000,",","'"));
Comments
Post a Comment