将CSV文件上传到SQL数据库

时间:2016-09-16 11:26:00

标签: php mysql csv

我正在尝试将CS​​V文件上传到sql表中,但我不想上传第一行,其中包含将要插入的每个列值的名称。我使用变量c来计算要插入的行数。对于第一行c = 0,所以我只使用条件来上传行中的值,如果c!== 0,但它仍然从第一行上升值。

我的代码如下所示:

$file = $_FILES['file']['tmp_name'];
$handle = fopen($file, "r");
$c = 0;
if ($_FILES["file"]["type"]=='application/vnd.ms-excel')
{
    while(($filesop = fgetcsv($handle, 3000, ",")) !== false)
    {
        $tid = trim($filesop[0]);
        $beneficiar = ucwords(strtolower(trim($filesop[1])));
        $locatie = ucwords(strtolower(trim($filesop[2])));
        $localitate = ucwords(strtolower(trim($filesop[3])));
        $judet = ucwords(strtolower(trim($filesop[4])));
        $adresa = ucwords(strtolower(trim($filesop[5])));
        $model = trim($filesop[6]);

        $qry = mysql_query("SELECT id FROM beneficiari WHERE `nume` = '".$beneficiar."'");
        while ($row = mysql_fetch_assoc($qry)){
            $id_client=$row['id'];
        }

        $qry_id_model=mysqli_query("SELECT id FROM modele WHERE `model` = '".$model."'");
        while ($row = mysql_fetch_assoc($qry_id_model)){
            $id_model=$row['id'];
        }

        $adresa1 = $adresa.",".$localitate;

        if ($c!==0){
            $sql = mysql_query(
                "INSERT INTO pos_equipments_other 
                     (id_client, model, tid, beneficiar, adresa, agentie, judet) 
                 VALUES 
                     ('$id_client','$id_model','$tid','$beneficiar','$adresa1',
                      '$locatie','$judet')"
            );
        }

        $c = $c + 1;
    }
}

0 个答案:

没有答案