CSV 文件上传和更新 MySQL 数据库


csv file upload and update mysql db

我对php很陌生。 我使用以下代码打开 CSV 文件并更新我的数据库。 我需要检查 CSV 文件的第一行第一列的值。 如果它匹配"一些文本 1",那么我需要运行代码 1,如果是"一些文本 2",运行代码 2, 否则代码3。我可以在 else 条件下使用,但由于我正在使用 while 循环它失败了。谁能帮我。

    $handle = fopen($file_tmp,"r");
        while(($fileop = fgetcsv($handle,",")) !== false) 
        {
                    // I need to check here
        $companycode =  mysql_real_escape_string($fileop[0]);
        $Item = mysql_real_escape_string($fileop[3]);
        $pack = preg_replace('/[^A-Za-z0-9'. -]/', '', $fileop[4]);
        $lastmonth = mysql_real_escape_string($fileop[5]);
        $ltlmonth = mysql_real_escape_string($fileop[6]);
        $op = mysql_real_escape_string($fileop[9]);
        $pur = mysql_real_escape_string($fileop[10]);
        $sale = mysql_real_escape_string($fileop[12]);
        $bal = mysql_real_escape_string($fileop[17]);
        $bval = mysql_real_escape_string($fileop[18]);
        $sval = mysql_real_escape_string($fileop[19]);
        $sq1 = mysql_query("INSERT INTO `sas` (companycode,Item,pack,lastmonth,ltlmonth,op,pur,sale,bal,bval,sval) VALUES ('$companycode','$Item','$pack','$lastmonth','$ltlmonth','$op','$pur','$sale','$bal','$bval','$sval')");
    }

也许您正在寻找开关语句?

情况 1:您必须为每一行触发不同的操作:

$handle = fopen($file_tmp,'r');
while(FALSE!==($fileop = fgetcsv($handle,','))) {
    switch($fileop[0]) {
        case 'some text 1':
            // do thing 1
            $sql = '...';
            break;
        case 'some text 2':
            // do thing 2
            $sql = '...';
            break;
        case 'some text 3':
            // do thing 3
            $sql = '...';
            break;
        default:
            $sql = FALSE;
    } // switch
} // while
if (FALSE!==$sql) {
    // now go on with the $sql statement and execute it using `mysqli` or `PDO`...
} // if

说明:您检查读取的每一行的第一列($fileop[0]),并决定触发哪个操作。

<小时 />

情况 2:您必须为每个文件触发不同的操作:

$handle = fopen($file_tmp,'r');
$action = NULL;
while(FALSE!==($fileop = fgetcsv($handle,','))) {
    if (is_null($action)) {
        switch($fileop[0]) {
            case 'some text 1': $action = 1; break;
            case 'some text 2': $action = 2; break;
            case 'some text 3': $action = 3; break;
            default:            $action = FALSE;
        } // switch
    } // if
    switch ($action) {
        case 1:
            $sql = '...';
            // prepare statement, use $fileop as array of attributes for the statement
            break;
        case 2:
            $sql = '...';
            // prepare statement, use $fileop as array of attributes for the statement
            break;
        case 2:
            $sql = '...';
            // prepare statement, use $fileop as array of attributes for the statement
            break;
    } // switch
} // while

说明:您检查您阅读的第一行的第一列($fileop[0])。仅当$action仍处于NULL 状态时,您才执行此操作。一旦设置$action,你就离开它。现在,您在处理步骤开始时$action定义一次,并且可以对读取的每一行做出反应。

<小时 />一般来说,在这种情况下

使用异常来处理意外的第一列值可能会付出代价,这样在这种情况下你可以跳过阅读整个文件......

为什么不直接加载到mysql?

load data local infile 'filename.csv' into table tblname
fields terminated by ','
enclosed by '"'
lines terminated by ''n'
(field1, field1, field1,...);