如果我正确理解您的代码,您有 4 个表:fuelrecords
, fuelrecords_die
, fuelrecords_pet
, fuelrecords_oil
.
3 个表fuelrecords_die
,每个都有一个fuelrecords_pet
外键。fuelrecords_oil
fuelrecords_id
fuelrecords.fr_id
您现在想要将多个元fuelrecords
组插入到其他 3 个表中,如果提供了其他数据,则将多个元组插入到其他 3 个表中。我假设该fuelrecords.fr_id
列是一个自动递增的主键。
要将多个元组插入到燃料记录中并让它们每个都有一个新的 fr_id,您只需不为 column 传递值fr_id
。这相当于NULL
作为值传递。MySQL 会自动为每个元组插入唯一的连续数字。
之后,您可以调用mysql_insert_id()
以获取第一个插入的 id。使用mysql_affected_rows()
您可以获得插入的元组的数量。这足以获取所有最后插入的元组的 id。第一个是mysql_insert_id()+0
第二个是mysql_insert_id()+1
……,最后一个是mysql_insert_id()+(mysql_affected_rows()-1)
。
在下一步中,您再次遍历输入数据,并fuelrecords_id
使用上述方法将 插入到其他 3 个表的每个元组中。如果$i
是输入数据的索引$_POST['FR_DIE_L'][$i]
(从 开始$i==0
),fuelrecords_id
则将是mysql_insert_id()+$i
。您只能迭代到mysql_insert_id()+mysql_affected_rows()-1
,但无论如何您可能会有相同数量的 POST 数据。
一种更简单但效率稍低的方法是只做一个insert into fuelrecords
,然后为每个单个 POST 数据对象将一个插入到其他 3 个表中。您不必计算fuelrecords_id
asmysql_insert_id()
将在每次插入后为您提供正确的 id。
<?php
$wedrf=trim($_POST['FR_WE']);
list($d, $m, $y) = explode('/', $wedrf);
$mk=mktime(0, 0, 0, $m, $d, $y);
$wed_refor=strftime('%Y-%m-%d',$mk);
$row_data = array();
// shorthand for mysql_real_escape_string
function esc($value) {
return mysql_real_escape_string($value);
}
// all tuples for fuelrecords to be inserted
foreach($_POST['VEH_LIST_REG'] as $row => $VEH_LIST_REG) {
$row_data[] = "(NULL, '".esc($VEH_LIST_REG)."', '".esc($wed_refor)."')";
}
if (!empty($row_data)) {
$query = 'INSERT INTO fuelrecords(FR_ID, VEH_LIST_REG, FR_WE) VALUES '.implode(',', $row_data);
$result = mysql_query($query);
# get first fuelrecord id
$first_fuelrecords_id = mysql_insert_id();
// all tuples for the other 3 tables. insert only if data is givin.
$die_data = array();
$pet_data = array();
$oil_data = array();
foreach($_POST['VEH_LIST_REG'] as $row => $VEH_LIST_REG) {
// calculate the right fuelrecords_id for this tuple
$fuelrecords_id = (int)($first_fuelrecords_id + $row);
// insert for fuelrecords_die
if (isset($_POST['FR_DIE_L'][$row]))
{
$die_data[] = "(NULL, ".$fuelrecords_id.", '".esc($_POST['FR_DIE_L'][$row])."', '".esc($_POST['FR_DIE_C'][$row])."')";
}
// insert for fuelrecords_pet
if (isset($_POST['FR_PET_L'][$row]))
{
$pet_data[] = "(NULL, ".$fuelrecords_id.", '".esc($_POST['FR_PET_L'][$row])."', '".esc($_POST['FR_PET_C'][$row])."')";
}
// insert for fuelrecords_oil
if (isset($_POST['FR_OIL_L'][$row]))
{
$oil_data[] = "(NULL, ".$fuelrecords_id.", '".esc($_POST['FR_OIL_L'][$row])."', '".esc($_POST['FR_OIL_C'][$row])."')";
}
}
// insert the tuples into fuelrecords_die
if (!empty($die_data))
{
$sql = "INSERT INTO fuelrecords_die(FRD_ID, fuelrecords_ID, FR_DIE_L, FR_DIE_C) VALUES ".implode(',', $die_data);
$result = mysql_query( $sql);
}
// insert the tuples into fuelrecords_pet
if (!empty($pet_data))
{
$sql = "INSERT INTO fuelrecords_pet(FRP_ID, fuelrecords_ID, FR_PET_L, FR_PET_C) VALUES ".implode(',', $pet_data);
$result = mysql_query( $sql);
}
// insert the tuples into fuelrecords_oil
if (!empty($oil_data))
{
$sql = "INSERT INTO fuelrecords_oil(FRO_ID, fuelrecords_ID, FR_OIL_L, FR_OIL_C) VALUES ".implode(',', $oil_data);
$result = mysql_query( $sql);
}
}
?>
一个小的题外话:尽量不要使用大写的变量名。通常为常量保留大写标识符:
define("MY_SHORT_PI", 3.14159265);
define("MY_CONST", "foobar");
$my_variable = "bat";
echo "I am a constant ".MY_SHORT_PI;
echo "Me too ".MY_CONST;
echo "I am a variable ".$my_variable;
这不会对 PHP 解释器产生任何影响。这只是使您的代码对其他人可读的常用符号。那里有许多风格指南,例如PEAR 中的指南。
第二个例子(见评论)
<?php
$wedrf=trim($_POST['FR_WE']);
list($d, $m, $y) = explode('/', $wedrf);
$mk=mktime(0, 0, 0, $m, $d, $y);
$wed_refor=strftime('%Y-%m-%d',$mk);
// VALUES strings for fuelrecords
$row_data = array();
// temporary storage for just _L and _C values
$die_data_tmp = array();
$pet_data_tmp = array();
$oil_data_tmp = array();
// VALUES strings for the three tables
$die_data = array();
$pet_data = array();
$oil_data = array();
// shorthand for mysql_real_escape_string
function esc($value) {
return mysql_real_escape_string($value);
}
// all tuples for fuelrecords to be inserted
foreach($_POST['VEH_LIST_REG'] as $row => $VEH_LIST_REG) {
// check if diesel values are greater than 0
if (0 < (int)$_POST['FR_DIE_L'][$row] && 0 < (int)$_POST['FR_DIE_C'][$row])
$die_data_tmp[$row] = array($_POST['FR_DIE_L'][$row], $_POST['FR_DIE_C'][$row]);
// check if petrolium values are greater than 0
if (0 < (int)$_POST['FR_PET_L'][$row] && 0 < (int)$_POST['FR_PET_C'][$row])
$pet_data_tmp[$row] = array($_POST['FR_PET_L'][$row], $_POST['FR_PET_C'][$row]);
// check if oil values are greater than 0
if (0 < (int)$_POST['FR_OIL_L'][$row] && 0 < (int)$_POST['FR_OIL_C'][$row])
$oil_data_tmp[$row] = array($_POST['FR_OIL_L'][$row], $_POST['FR_OIL_C'][$row]);
// check if at least one of the 3 tables will get tuples. if not just continue
// with the next and don't assign this fuelrecord tuple to $row_data
if (! isset($die_data_tmp[$row]) && ! isset($pet_data_tmp[$row]) && ! isset($oil_data_tmp[$row]))
continue;
// all values are at least 1, so add this tuple to our inserts
$row_data[$row] = "(NULL, '".esc($VEH_LIST_REG)."', '".esc($wed_refor)."')";
}
if (!empty($row_data)) {
$query = 'INSERT INTO fuelrecords(FR_ID, VEH_LIST_REG, FR_WE) VALUES '.implode(',', $row_data);
$result = mysql_query($query);
# get first fuelrecord id
$current_fuelrecords_id = mysql_insert_id();
// all tuples for the other 3 tables. insert only if data is givin.
foreach($row_data as $row => $VEH_LIST_REG) {
// insert for fuelrecords_die
if (isset($die_data_tmp[$row]))
{
$die_data[] = "(NULL, ".$current_fuelrecords_id.", '".esc($die_data_tmp[$row][0])."', '".esc($die_data_tmp[$row][1])."')";
}
// insert for fuelrecords_pet
if (isset($pet_data_tmp[$row]))
{
$pet_data[] = "(NULL, ".$current_fuelrecords_id.", '".esc($pet_data_tmp[$row][0])."', '".esc($pet_data_tmp[$row][1])."')";
}
// insert for fuelrecords_oil
if (isset($oil_data_tmp[$row]))
{
$oil_data[] = "(NULL, ".$current_fuelrecords_id.", '".esc($oil_data_tmp[$row][0])."', '".esc($oil_data_tmp[$row][1])."')";
}
// increment the fuelrecords_id for the next tuple.
++$current_fuelrecords_id;
}
// insert the tuples into fuelrecords_die
if (!empty($die_data))
{
$sql = "INSERT INTO fuelrecords_die(FRD_ID, fuelrecords_ID, FR_DIE_L, FR_DIE_C) VALUES ".implode(',', $die_data);
$result = mysql_query( $sql);
}
// insert the tuples into fuelrecords_pet
if (!empty($pet_data))
{
$sql = "INSERT INTO fuelrecords_pet(FRP_ID, fuelrecords_ID, FR_PET_L, FR_PET_C) VALUES ".implode(',', $pet_data);
$result = mysql_query( $sql);
}
// insert the tuples into fuelrecords_oil
if (!empty($oil_data))
{
$sql = "INSERT INTO fuelrecords_oil(FRO_ID, fuelrecords_ID, FR_OIL_L, FR_OIL_C) VALUES ".implode(',', $oil_data);
$result = mysql_query( $sql);
}
}
?>