在棘手的情况下,我需要一些帮助。
目前我有一个看起来像这样的数组
Array
(
[0] => Array
(
[Task_ID] => 59
[email] => m@m.com
)
[1] => Array
(
[Task_ID] => 59
[email] => m@m.com
)
[2] => Array
(
[Task_ID] => 59
[email] => z@z.com
)
[3] => Array
(
[Task_ID] => 60
[email] => m@m.com
)
[4] => Array
(
[Task_ID] => 60
[email] => z@z.com
)
[5] => Array
(
[Task_ID] => 61
[email] => z@z.com
)
)
这是我当前的插入声明。
if(is_array($myArr)){
$valuesArr = array();
foreach($myArr as $row){
$sql = "INSERT INTO user_data (Task_ID, email, url) values ";
$TaskID = (int) $row['Task_ID'];
$email = mysql_real_escape_string( $row['email'] );
$valuesArr[] = "('$TaskID', '$email')";
$sql .= "('$TaskID', '$email')";
mysql_query($sql) or exit(mysql_error());
}
}
我想要做的是插入一个不在数组中的新值,即URL值。
对于网址,我想为每封唯一的电子邮件增加一个数字,同一封电子邮件将获得相同的网址值。
SQL语法应该是什么样子的一个例子。
INSERT INTO user_data (TaskID, email, url) values ('60', 'm@m.com','1')
INSERT INTO user_data (TaskID, email, url) values ('80', 'm@m.com','1')
INSERT INTO user_data (TaskID, email, url) values ('82', 'z@z.com','2')
INSERT INTO user_data (TaskID, email, url) values ('62', 'z@z.com','2')
INSERT INTO user_data (TaskID, email, url) values ('42', 'z@z.com','2')
INSERT INTO user_data (TaskID, email, url) values ('61', 'a@a.com','3')
INSERT INTO user_data (TaskID, email, url) values ('81', 'a@a.com','3')
INSERT INTO user_data (TaskID, email, url) values ('92', 't@t.com','4')
INSERT INTO user_data (TaskID, email, url) values ('32', 't@t.com','4')
INSERT INTO user_data (TaskID, email, url) values ('72', 't@t.com','4')
请注意,每次更改电子邮件时网址都会增加1。请注意,TaskID不是PK,电子邮件不符合规定。
非常感谢您的阅读。
答案 0 :(得分:1)
试一试
if (is_array($myArr)) {
$valuesArr = array();
$addedEmails = array();
$cnt = 0;
foreach($myArr as $row){
$sql = "INSERT INTO user_data (Task_ID, email, url) values ";
$TaskID = (int) $row['Task_ID'];
$email = mysql_real_escape_string( $row['email'] );
if (!in_array($email, $addedEmails)) {
$cnt++;
$addedEmails[] = $email;
}
$valuesArr[] = "('$TaskID', '$email', '$cnt')";
$sql .= "('$TaskID', '$email', '$cnt')";
mysql_query($sql) or exit(mysql_error());
}
}