使用PHP将CSV文件数据插入并更新到MySQL表中

时间:2014-12-31 07:13:14

标签: php mysql csv

我正在将CSV文件中的记录插入到MySQL表中,如果已经存在任何记录更新记录并插入其他记录,如果记录没有匹配则插入所有记录。

这是我的代码

<?php

$connect = mysql_connect('localhost', '', '');
if (!$connect) {
    die('Could not connect to MySQL: ' . mysql_error());
}

$cid = mysql_select_db('test', $connect);


define('CSV_PATH', '/home/ubc/Documents/');

$csv_file = CSV_PATH . "test.csv";
$csvfile  = fopen($csv_file, 'r');
$theData  = fgets($csvfile);

$i = 0;
while (!feof($csvfile)) {

    $csv_data[] = fgets($csvfile, 1024);
    $csv_array  = explode(",", $csv_data[$i]);
    $insert_csv = array();

    $insert_csv['name']  = $csv_array[0];
    $insert_csv['email'] = $csv_array[1];

    $query = mysql_query("select * from test");
    $count = mysql_num_rows($query);

    if ($count == 0) {
        $query = "INSERT INTO test(name,email)VALUES('" . $insert_csv['name'] . "','" . $insert_csv['email'] . "')";

        $n = mysql_query($query, $connect);
        $i++;
    } else {
        $query = mysql_query("select name from test where name='" . $insert_csv['name'] . "'");
        while ($row = mysql_fetch_array($query)) {

            if ($count) {
                $sql = "update test set email='" . $insert_csv['email'] . "'";
                $qu  = mysql_query($sql);

            } else {

                $query = "INSERT INTO test(name,email)VALUES('" . $insert_csv['name'] . "','" . $insert_csv['email'] . "')";

                $n = mysql_query($query, $connect);

                $i++;
            }
        }
    }
}

fclose($csvfile);

echo "File data successfully imported to database!!";
mysql_close($connect);
?>

2 个答案:

答案 0 :(得分:0)

使用以下查询..

INSERT INTO table (primkey, col2, col3) VALUES(1, 2, 3) ON DUPLICATE KEY UPDATE    
col1=VALUES(1), col2=VALUES(2)

它的例子。

这可能对您有所帮助

http://dev.mysql.com/doc/refman/5.0/en/replace.html

答案 1 :(得分:0)

    <?php

        $connect = mysql_connect('localhost', '', '');
        if (!$connect) {
            die('Could not connect to MySQL: ' . mysql_error());
        }

        $cid = mysql_select_db('test', $connect);


        define('CSV_PATH', '/home/ubc/Documents/');

        $csv_file = CSV_PATH . "test.csv";
        $csvfile  = fopen($csv_file, 'r');
        $theData  = fgets($csvfile);

        $i = 0;
        while (!feof($csvfile)) {

            $csv_data[] = fgets($csvfile, 1024);
            $csv_array  = explode(",", $csv_data[$i]);
            $insert_csv = array();

            $insert_csv['name']  = $csv_array[0];
            $insert_csv['email'] = $csv_array[1];

            $query = mysql_query("select name from test where name='" . $insert_csv['name'] . "'");
            $count = mysql_num_rows($query);

            if ($count == 0) {
                $query = "INSERT INTO test(name,email)VALUES('" . $insert_csv['name'] . "','" . $insert_csv['email'] . "')";

                $n = mysql_query($query, $connect);

            } else {
                $sql = "update test set email='" . $insert_csv['email'] . "'";
                        $qu  = mysql_query($sql);
            }
 $i++;
            }
        }

        fclose($csvfile);

        echo "File data successfully imported to database!!";
        mysql_close($connect);
        ?>