0

我正在尝试创建一个脚本,该脚本从 csv 文件上传电子邮件地址,但还在插入之前检查该地址是否已存在于 2 个不同的表中。我有这个完美的查询:

INSERT INTO list_email (fname, lname, email_addr)
SELECT ins.* FROM ( SELECT 'bob' AS fname,
'schmoe' AS lname,
'bogus@bogus.com' AS email_addr
FROM dual UNION ALL
SELECT 'mary', 'lamb', 'hoe@me.com'
FROM dual ) AS ins WHERE NOT EXISTS
( SELECT 1
FROM list_email AS e
WHERE e.email_addr = ins.email_addr
) AND NOT EXISTS
( SELECT 1
FROM list_no_email AS ne
WHERE ne.email_addr = ins.email_addr );

但由于这是一个文件上传脚本,出于显而易见的原因 bogus@bogus.com 是行不通的。我需要比较“每个”入站记录,而不仅仅是一个。所以它必须是 $email_addr 绑定到 list_email.email_addr 列(如果它不存在)我似乎无法访问 $_FILES['userfile'] 键来获取诸如 $email_addr 之类的数据来执行比较。$_FILES 的 var_dump() 是否以“userfile”为键显示 test.csv,但我无法进入其中。$_POST 有我的 row_name 并且工作正常。

  <form enctype="multipart/form-data" method="POST">
        <p> Give a unique name for your campaign </p>
        <input name="row_name" type="text">
        <br/>
        <input name="userfile" type="file">
        <br/>
        <input type="submit" value="Upload">

  </form>
<?php
$dsn = "mysql:host=$host;port=$port;dbname=$dbname"; //Data Source Name = Mysql
$db = new PDO($dsn, $db_username, $db_password); //Connect to DB
//var_dump($_FILES);
//var_dump($_POST);
if (isset($_POST['userfile']) && isset($_POST['row_name'])){
        $insert = array(NULL, $data[0]." ".$data[1], $data[0],$data[1],$data[2]);
        var_dump($insert);
        $do = $db->prepare(
                "INSERT INTO list_email(list_name, fname, lname, email_addr)
                SELECT ins.*
                FROM(
                 SELECT $data[1] AS fname,
                 $lname AS lname,
                 $data[3] AS email_addr
                 FROM dual
                 UNION ALL
                 SELECT $data[1], $data[2], $data[3]
                 FROM dual
                ) AS ins
                WHERE NOT EXISTS(
                 SELECT 1
                 FROM list_email AS e
                 WHERE e.email_addr = ins.email_addr
                )
                AND NOT EXISTS(
                 SELECT 1
                 FROM list_no_email AS ne
                 WHERE ne.email_addr = ins.email_addr
                )"
                );
        $csv_file = $_FILES['userfile']['tmp_name'];
        // exit if file fail
        if ( ! is_file( $csv_file ) )
                exit('File not found.');
        if (($handle = fopen($csv_file, "r")) !== FALSE) {
        while (($data = fgetcsv($handle)) !== FALSE) {
                //var_dump($insert);
                $do->execute($insert);
            }
            fclose($handle);
        }
}
exit( "Complete!" );
?>
</body>
</html>

** * ** * ** * ** * ** * var_dump() * ** * ** * ** * ** * ** * ****

    Notice: Undefined offset: 0 in /home/wemail1/www/pages/campaign-build.inc.php on line 35

Notice: Undefined offset: 1 in /home/wemail1/www/pages/campaign-build.inc.php on line 35

NULL array(1) { ["userfile"]=> array(5) { ["name"]=> string(9) "test1.csv"
+["type"]=> string(24) "application/vnd.ms-excel" ["tmp_name"]=> string(14)
+"/tmp/php5k0ppx" ["error"]=> int(0) ["size"]=> int(76) } }
> no values ^^
> var_dump($_FILES);

** * ** * ** * ** * ** 桌子 ** * ** * ** * ** * ** * **

mysql> desc list_email;
+------------+--------------+------+-----+---------+----------------+
| Field      | Type         | Null | Key | Default | Extra          |
+------------+--------------+------+-----+---------+----------------+
| id         | int(11)      | NO   | PRI | NULL    | auto_increment |
| list_name  | varchar(55)  | YES  |     | NULL    |                |
| fname      | char(50)     | YES  |     | NULL    |                |
| lname      | char(50)     | YES  |     | NULL    |                |
| email_addr | varchar(150) | YES  |     | NULL    |                |
+------------+--------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)

mysql> desc list_no_email;
+------------+--------------+------+-----+-------------------+-----------------------------+
| Field      | Type         | Null | Key | Default           | Extra                       |
+------------+--------------+------+-----+-------------------+-----------------------------+
| id         | int(11)      | NO   | PRI | NULL              | auto_increment              |
| date_in    | timestamp    | NO   |     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
| email_addr | varchar(150) | YES  |     | NULL              |                             |
+------------+--------------+------+-----+-------------------+-----------------------------+
3 rows in set (0.00 sec)




http://stackoverflow.com/questions/18584541/mysql-insert-into-with-dual-condition-for-if-not-exist
4

1 回答 1

0

所以我清理了你的代码。您提到的错误是由于您调用$_FILES[0],$_FILES[1]$_FILES[2]. 这里没有值,因此它为空。

此外,您的代码有一堆根本不需要的东西。

另外:查看准备好的语句以及如何在那里填充变量!

让我解释:

<?php
$dsn = "mysql:host=$host;port=$port;dbname=$dbname"; //Data Source Name = Mysql
$db = new PDO($dsn, $db_username, $db_password); //Connect to DB
if (isset($_POST['userfile']) && isset($_POST('row_name']))){
    //$rowname = $_POST['row_name']; (do you need this? You never call this anywhere below)

    //Dont know why this is here
    //$fname = $_FILES[0]; 
    //$lname = $_FILES[1]; 
    //$post_addr = $_FILES[2];

    //PDO does not work with $var, instead use :var and fill the var before executing the query
    $do = $db->prepare(
            "INSERT INTO list_email(list_name, fname, lname, email_addr)
            SELECT ins.*
            FROM(
             SELECT :fname AS fname,
             :lname AS lname,
             :post_addr AS email_addr
             FROM dual
             UNION ALL
             SELECT :fname, :lname, :post_addr
             FROM dual
            ) AS ins
            WHERE NOT EXISTS(
             SELECT 1
             FROM list_email AS e
             WHERE e.email_addr = ins.email_addr
            )
            AND NOT EXISTS(
             SELECT 1
             FROM list_no_email AS ne
             WHERE ne.email_addr = ins.email_addr
            )"
            );

            $do->bindParam(':fname', $fname);
            $do->bindParam(':lname', $lname);
            $do->bindParam(':post_addr', $post_addr);

    $csv_file = $_FILES['userfile']['tmp_name'];
    // exit if file fail
    if ( ! is_file( $csv_file ) )
            exit('File not found.'); //exit = bad practise, use proper error handling
    if (($handle = fopen($csv_file, "r")) !== FALSE) {
    while (($data = fgetcsv($handle)) !== FALSE) 
    {
            //$insert = array(NULL, $data[0]." ".$data[1], $data[0],$data[1],$data[2]); (what is this? what does it do?)

            //Fill the variables needed for the query
            $fname = "your desired value";
            $lname = "your desired value";
            $post_addr = "your desired value";
            //Execute the query
            $do->execute();
    }
        fclose($handle);
    }
}
else {
 print "Something went wrong while trying IsSet";
}
?>

让我们来看看这个:

$fname = "your desired value";
$lname = "your desired value";
$post_addr = "your desired value";

显然,您在这里需要您的价值观,这些将来自您阅读的那一行。因此,例如,这将是:

$fname = $data[0];
$lname = $data[1];
$post_addr = $data[2];

除此之外,您的 SQL 查询也会失败。您想将数据插入到list_email请求的4列中,但只传递3 个变量。

我也不明白为什么你会选择你两次传递的变量dual

编辑

正如我所说,您的 SQL 查询是错误的。

我已经更新了一点。 list_name 来自哪里?

我现在在更新的查询中添加了MISSING_VALUE :

INSERT INTO list_email(list_name, fname, lname, email_addr)
            SELECT ins.*
            FROM(

SELECT ins.*
FROM
(
SELECT 
    'MISSING VALUE' as MISSING_VALUE
    :fname AS fname,
    :lname AS lname,
    :post_addr AS email_addr
FROM dual
) as ins 
WHERE 
NOT EXISTS(
    SELECT 1 
    FROM list_email AS e 
    WHERE e.email_addr = ins.email_addr)
AND 
NOT EXISTS(
    SELECT 1
    FROM list_no_email AS ne
    WHERE ne.email_addr = ins.email_addr))
于 2013-09-03T08:56:33.023 回答