올바르게 코드를 이해하면 fuelrecords
, fuelrecords_die
, fuelrecords_pet
, fuelrecords_oil
의 4 개의 테이블이 있습니다.
3 개 테이블 fuelrecords_die
, fuelrecords_pet
, fuelrecords_oil
각 외래 키 fuelrecords_id
fuelrecords.fr_id
에 있습니다.
추가 튜플을 fuelrecords
에 삽입하고 추가 데이터가 제공되면 다른 튜플을 다른 3 개의 테이블에 삽입하려고합니다. fuelrecords.fr_id
열이 자동 증가 기본 키라고 가정합니다.
여러 개의 튜플을 연료 기록기에 삽입하고 각각에 새로운 fr_id가 포함되도록하려면 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
을 삽입합니다. $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
을 mysql_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 인터프리터에는 영향을주지 않습니다. 코드를 다른 사람들이 읽을 수있게 만드는 것은 일반적인 표기법입니다. 거기에는 많은 스타일 가이드가 the one from 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);
}
}
?>
이제까지 당신이 결코 사용자 ID 및 암호와 코드 (여기 MySQL의 연결) – Roger