简体   繁体   中英

add rows with loop max value +1

I'm trying to add rows to the database based on the input value. ie If input as "5", query will insert 5 rows to database. (this part is working fine)

Now, I need the bed_number to be +1 to the existing max(bed_number) but i can't seems to get it to work.

If existing max(bed_number) returns 5, than the query should add "6,7,8,9,10, etc" as the bed_number for the 5 entries.

If existing max(bed_number) returns null, than it should add "1,2,3,4,5, etc"

Right now, the result always return 1,2,3,4,5... regardless of the max count.

What i have here now is:

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();
//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();

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM