我有一个字符串,其中包含许多以逗号分隔的电子邮件地址,即
$emails = “111@abc.com, 222@abc.com, 333@abc.com”;
我有正常的PHP代码,工作正常
<?php
function cust_get_oldest_items_all($limit = 1) {
$mSearch = new Search();
$result = $mSearch->dao->query(sprintf('SELECT pk_i_id FROM %st_item ORDER BY dt_pub_date LIMIT 0, %d', DB_TABLE_PREFIX, $limit ));
return $result->result();
}
?>
我想在上面的php代码中使用WHERE子句只包含那些s_contact_email在$ email中的项目?
答案 0 :(得分:1)
以这种方式处理您的电子邮件:
$emails = "111@abc.com, 222@abc.com, 333@abc.com";
$emailsArr = explode(",",$emails);//make an array of emails
//Todo: escape each array item (each email) to prevent SQL injection
//surround each email with single quotes
$emailsArr = array_map(function($e){return "'".trim($e)."'";},$emailsArr);
//join. new string looks like >> ('111@abc.com','222@abc.com','333@abc.com')
$emails = '('. implode(",",$emailsArr) .')';
将您的SELECT
查询更改为:
SELECT pk_i_id FROM %st_item
WHERE s_contact_email IN $emails
ORDER BY dt_pub_date LIMIT 0, %d
安全提示
此修复程序可以使用,但这还不够。您必须正确转义每个电子邮件地址或使用预准备语句来阻止攻击者企图接管您的数据库(SQL注入攻击)