【发布时间】:2015-12-15 02:06:41
【问题描述】:
我正在开发一个印版分配系统。我为变量$quantity 分配了一个值,它会说明要分配多少个盘子。
$plate_prefix和$plate_suffix是要分配的板块数量的起点。
但是有一个例子,有一个定制的盘子会在盘子的数量之间。
示例:我需要 30 个板块,AAA-1001 是开始,但 AAA-1020 已经被占用,所以我需要跳过 AAA-1020 以获取板块 AAA-1001 到 AAA-1032。
$region = $_POST['region'];
$district_office = $_POST['district_office'];
$quantity = $_POST['quantity'];
$plate_prefix = $_POST['plate_prefix'];
$plate_suffix = $_POST['plate_suffix'];
$loop = 0;
while($loop < $quantity)
{
if($plate_suffix <= 9999)
{
$sql1 = mysql_query("INSERT INTO mv_plate (`plate_prefix`, `plate_suffix`, `region`, `district_office`, `status`)
VALUES ('$plate_prefix', '$plate_suffix', '$region', '$district_office', 'Available')");
$plate_suffix = $plate_suffix+1;
$loop = $loop+1;
}
else
{
$plate_prefix = ++$plate_prefix;
$plate_suffix = 1001;
}
}
【问题讨论】:
-
进行选择查询,其中
yourfield= '$yourplate'。然后在插入之前执行if else语句... -
如果数据库设计良好(
UNIQUE约束plate_prefix和plate_suffix)INSERT无论如何都会引发错误,从而防止重复板。请注意,您的代码易受 SQL 注入攻击:使用 PDO。