我尝试根据输入值向数据库添加行。 即如果输入" 5",查询将向数据库插入5行。 (这部分工作正常)
现在,我需要bed_number为现有max(bed_number)的+1,但我似乎无法让它工作。
如果现有的max(bed_number)返回5,则查询应该添加" 6,7,8,9,10等等#34;作为5个条目的bed_number。
如果现有的max(bed_number)返回null,则应该添加" 1,2,3,4,5等"
现在,无论最大计数如何,结果总是返回1,2,3,4,5 ...
我现在所拥有的是:
global $conn;
if ($values["number_of_bed"])
{
$add1 = $values["number_of_bed"]+1;
$existingBed = "select Max(bed_number) from bed where bed =" '".$i."'" +1;
for ($i=1;$i<$add1;$i++)
{
$strInsert = "insert into bed (unit_id,bed_number) values ('".$values["unit_id"]."','".$existingBed."')";
db_exec($strInsert,$conn);
}
header("Location: bed_list.php");
// Exit and Redirect to the list page after updating database
exit();
答案 0 :(得分:0)
//echo "Number of customers: " . $data["c"];
global $conn;
if ($values["bed_number"])
{
$add1 = $values["bed_number"]+1;
//for ($i=1;$i<$values["bed_number"];$i++)
for ($i=1;$i<$add1;$i++)
{
$sql = "select max(bed_number) as c from bed where unit_id =" . $values["unit_id"];
$rs = CustomQuery($sql);
$data = db_fetch_array($rs);
$strInsert = "insert into bed (unit_id,bed_number) values ('".$values["unit_id"]."','".$data["c"]."'+1)";
// add more fields from the add page to be inserted into database
db_exec($strInsert,$conn);
}
header("Location: bed_list.php");
// Exit and Redirect to the list page after updating database
exit();
}