INSERT Email address, but ignore (if exists || if suffix = existing suffix && sender name =existingsender name) MYSQL
I'm trying to insert a query into mysql using php, but to have restrictio开发者_运维百科ns.
$sql_insert = "
INSERT IGNORE
into `email`
(`message`,`useremail`,`senderemail`)
VALUES
('$emailmessage','$email_address','$fromaddress')
WHERE
";
$fromaddress is the value I want to make the constraints.
Here's an example
"Zazzle" <sender-1234@zazzle.com>
Don't insert if sender-1234@zazzle.com
is in the senderemail field
Don't insert if "Zazzle"
is in the field and if @zazzle.com
is also in the field.
you can pull the email apart and then check the database to see if it exists in the database using preg_match. If using the example:
--"Zazzle" <sender-1234@zazzle.com>
$atIndex = strrpos($fromaddress, "@");
$nameIndex = strrpos($fromaddress, "<");
$domain = substr($fromaddress, $atIndex+1); # returns zazzle.com
$local = substr($fromaddress, $nameIndex+1, $atIndex); # returns sender-123
$name = substring($fromaddress, 0, $nameIndex-1); # returns "Zazzle"
from there all you have to do is do a select/where query and if no results then to insert.
$query = "SELECT * FROM email WHERE senderemail like '".$domain."'";
$result=mysql_query($query);
$num=mysql_num_rows($result);
OR
$query = "SELECT * FROM email";
$result=mysql_query($query);
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
if(preg_match($domain, $row['senderemail'])) { $isPresent = true; }
}
Preg match will look for the strings in the individual returned value and if present set a value to true. Then you can compare that value to be true/false and then to update your database.
精彩评论