我正在尝试将数组数据插入或更新到mysql数据库中。
我的数组看起来像
Array
(
[0] => Array
(
[R_ID] => 32
[email] => a@a.com
[name] => Bob
)
[1] => Array
(
[R_ID] => 32
[email] => b@b.com
[name] => Dan
)
[2] => Array
(
[R_ID] => 32
[email] => c@c.com
[name] => Paul
)
[3] => Array
(
[R_ID] => 35
[email] => d@d.com
[name] => Mike
)
)
我正在使用此代码将数据插入到mysql数据库中。
if(is_array($EMailArr)){
$sql = "INSERT INTO email_list (R_ID, EMAIL, NAME) values ";
$valuesArr = array();
foreach($EMailArr as $row){
$R_ID = (int) $row['R_ID'];
$email = mysql_real_escape_string( $row['email'] );
$name = mysql_real_escape_string( $row['name'] );
$valuesArr[] = "('$R_ID', '$email', '$name')";
}
$sql .= implode(',', $valuesArr);
mysql_query($sql) or exit(mysql_error());
}
但我希望如果数组R_ID值与任何行R_ID值匹配,则查询更新现有值,否则查询插入新行。
我无法理解我该怎么做。