-1

该脚本运行良好,但从 excel 文件(标题名称)中添加了第一行。我想忽略循环中的第一行 - 请检查脚本 -

<?php

//connect to the database
$connect = mysql_connect("localhost","root","password");
mysql_select_db("mydatadb",$connect); //select the table
//

if ($_FILES[csv][size] > 0) {

    //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 bulk_listings (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) VALUES
                (
                    '".addslashes($data[1])."',
                    '".addslashes($data[2])."',
                    '".addslashes($data[3])."',
                    '".addslashes($data[4])."',
                    '".addslashes($data[5])."',
                    '".addslashes($data[6])."',
                    '".addslashes($data[7])."',
                    '".addslashes($data[8])."',
                    '".addslashes($data[9])."',
                    '".addslashes($data[10])."',
                    '".addslashes($data[11])."',
                    '".addslashes($data[12])."',
                    '".addslashes($data[13])."',
                    '".addslashes($data[14])."',
                    '".addslashes($data[15])."',
                    '".addslashes($data[16])."',
                    '".addslashes($data[17])."',
                    '".addslashes($data[18])."',
                    '".addslashes($data[19])."',
                    '".addslashes($data[20])."'
                )
            ");
        }
    }
    while ($data = fgetcsv($handle,10000,",")); 
    //redirect
    header('Location: import.php?success=1'); die;
}
?>

另一个名为 Import.php 的文件

<?php

//connect to the database

mysql_connect ("localhost","root","password")
  or die ('Error: ' . mysql_error());

echo "connected to database!";

mysql_select_db ("mydatadb");

// Insert data into table

        $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]);

$action = mysql_real_escape_string('insert php code for button here');

$query = "INSERT INTO bulk_listings
         (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)
         VALUES
         ('$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') ";
mysql_query($query) or die ('Error updating database');

echo "Database updated successfully!";

?>
</body>
</html>

我试过 - fgetcsv() & while ($data = fgetcsv($handle,10000,",")); 但仍然无法正常工作。

请帮助我并告诉我哪里做错了?

谢谢

4

2 回答 2

0

On the first part of the posted code you need to add a line after you create the handle, so line:

$handle = fopen($file,"r");

becomes:

$handle = fopen($file,"r");
fgetcsv($handle,10000,",");

This will get the first line and then ignore it in the loop because the pointer will be in the second line when the loop starts.

于 2013-05-09T10:27:08.987 回答
0

你基本上想从数组中删除第一个索引,这是你的第一行..

$data = fgetcsv($handle,10000,","));
$data = array_shift($data);
于 2013-05-09T10:36:19.793 回答