我正在查询我的数据库中的大量代码,并且在用户可以将其他代码输入到数据库之前需要进行一些验证。比较从mySql查询返回的字符串
一个例子代码将是这样的:
TD-BR-010212-XXXXXXXX
其中TD代表推广,BR代表的地方,这些数字代表日期,其余都是随机的。
我的问题是,在代码输入到数据库之前,我想检查该代码的日期和地点是否已经存在,因为它们不应该被允许从同一地点和日期输入代码。
我认为这将是一个循环内的东西,因为我已经有了:
$location_part_of_td = $code[2].$code[3];
$date_part_of_td = $code[4].$code[5].$code[6].$code[7].$code[8].$code[9];
$trade_day_result = mysql_query('SELECT * from wp_scloyalty WHERE promotion_type = trade-day') or die(mysql_error()); // Pulls all trade day codes from the database and checks the date part of the code.
// the date part exists with the same area part, user cant redeem.
while($info = mysql_fetch_array($trade_day_result))
{
$code = $info["product"];
}
但我只是不知道检查字符串的最好方式..
如果仅仅是开始地点和日期,你可以创建一个带有前缀的唯一索引。 – 2012-03-16 12:54:58