我正在尝试创建一个脚本,该脚本从 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