dongzhitao4839
2013-05-08 15:34
浏览 46
已采纳

使用PHP和MySQL的CSV文件 - 数据无法在数据库中正确上传

I download the script from - http://www.k-fez.com/?p=101 website and trying to upload CSV file on my server. It uploading file successfully but Blank - please check image

<?PHP
session_start();

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';
    $dbpass = 'password';
    $dbname = 'auction_tbl';
    $link = mysql_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to mysql server');
    mysql_select_db($dbname);

    //Process the 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 does 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 is in database already
        $sql = "SELECT *
                FROM `bulk_listings`
                WHERE `auction_id` = '" . $auction_id . "'";    //In this example attribute0 is the 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 is already 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 is unique
        else{
            //Add INSERT statement to INSERT queue
            $_SESSION['insert'] .= "INSERT INTO `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 for row to findings table and mark unique
            $findings = $findings . "
  <tr>
    <td bgcolor=\"#00FF00\">&nbsp;</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 be here");
}
?>

importCSV.php (Sorry i forgot to not another file code.)

<?PHP
session_start();
if(!isset($_SESSION['insert'])){
    die("You shouldn't be here.");
}

//Connect to the database
    $dbhost = 'Localhost';
    $dbuser = 'root';
    $dbpass = 'password';
    $dbname = 'auction_tbl';
    $link = mysql_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to mysql server');
    mysql_select_db($dbname);

$queries = explode(';', $_SESSION['insert']);

foreach($queries as $query){
    if($query != ""){
        mysql_query($query);
    }
}

mysql_close($link);

echo "Done";
?>

enter image description here

Thanks

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • doulin1867 2013-05-09 11:34
    已采纳

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

    //loop through the csv file and insert into database
    do {
        if ($data[0]) {
            mysql_query("INSERT INTO contacts_tmp (contact_first, contact_last, contact_email) VALUES
                (
                    '".addslashes($data[0])."',
                    '".addslashes($data[1])."',
                    '".addslashes($data[2])."'
                )
            ");
        }
    } while ($data = fgetcsv($handle,1000,",","'"));
    
    点赞 评论
  • dragon8837 2013-05-09 11:39

    Check Database Field Data type Properly .. I think database field data type is wrong for this problem..

    点赞 评论

相关推荐 更多相似问题