我在查询数据库时需要帮助

时间:2016-02-17 17:24:09

标签: php mysql for-loop mysqli

我在这里遇到查询问题。需要在我的数据库中插入电子邮件,但不会重复。电子邮件的输入以逗号或分号分隔。我留下目前的代码:

$separador = $_POST['separador'];
$listemails= explode($separador, $_POST['emails']);
$count = count($listemails);
for($i=0; $i < $count; $i++){
    $check = $connection -> query("SELECT * FROM emails WHERE email_endereco='$listemails[$i]'") or die($connection -> error);
    if($res = $check -> fetch_array(MYSQLI_ASSOC)){
        ...
    }
    $result = $connection -> query("INSERT INTO emails VALUES(NULL, '".$listemails[$i]."')") or die($connection -> error);
    $lastid = $connection -> insert_id;
    $connection -> query("INSERT INTO emails_lists VALUES(NULL,'$lastid','$l')") or die($connetion -> error);
} 

我想念省略号的地方?

1 个答案:

答案 0 :(得分:0)

已经有了:D! 我更改了db for primary key中的email字段,然后更改了代码以使其成为:

for($i=0; $i < $count; $i++){
    $str .= "(NULL,'".$listemails[$i]."')".($i < $count - 1 ? "," : "");
}
$result = $connection -> query("INSERT IGNORE INTO emails VALUES ".$str) or die($connection -> error);

如果我可以帮助其他人,我会留下答案!