2012-03-11 82 views
0

好了,所以雖然我一直在我的PHP和MySQL技能我是新來將數據插入多個表中一氣呵成。我已經做了一些關於這個主題的閱讀,我很欣賞規範化數據的基礎知識和重要性,因此需要將這些信息放入各種表格中。插入用mysql插入ID陣列數據和if語句

我有一些我以前的工作,以及在http://www.desilva.biz/mysql/insertid.html提供的教程拼湊下面的代碼在一起。我目前面臨的問題是,我學習了用於輸入到各種表中的代碼的教程不是基於一系列數據,雖然我幾乎可以使用它,但我無法使用我的fuelrecords_id,因爲我必須在我的目前的代碼尚未定義。因此,爲了使我的代碼在目前工作,我只需要使用列的逗號。

最後,我想完善一種方法,使if語句與數組數據正常工作,所以如果作爲數組的一部分提交一個0或空白,一個新行不會插入,只有0在我的數據庫表中數據的各自的行

<?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); 

$con = mysql_connect("ip","username","password"); 
if (!$con) 
{ 
die('Could not connect: ' . mysql_error()); 
} 

mysql_select_db("jbsrint", $con); 

//New Code 

$row_data = array(); 
foreach($_POST['VEH_LIST_REG'] as $row=>$VEH_LIST_REG) { 
$WEDATE=$wed_refor; 
$VEH_LIST_REG=mysql_real_escape_string($VEH_LIST_REG); 
$FR_DIE_L=mysql_real_escape_string($_POST['FR_DIE_L'][$row]); 
$FR_DIE_C=mysql_real_escape_string($_POST['FR_DIE_C'][$row]); 
$FR_PET_L=mysql_real_escape_string($_POST['FR_PET_L'][$row]); 
$FR_PET_C=mysql_real_escape_string($_POST['FR_PET_C'][$row]); 
$FR_OIL_L=mysql_real_escape_string($_POST['FR_OIL_L'][$row]); 
$FR_OIL_C=mysql_real_escape_string($_POST['FR_OIL_C'][$row]); 
$row_data[] = "(',', '$VEH_LIST_REG', '$WEDATE')"; 
$row_data1[] = "(',','$FR_DIE_L', '$FR_DIE_C', ',')"; 
$row_data2[] = "(',', '$FR_PET_L', '$FR_PET_C', ',')"; 
$row_data3[] = "(',', '$FR_OIL_L', '$FR_OIL_C', '$FR_OIL_C')"; 
} 
if (!empty($row_data)) { 
$query = 'INSERT INTO fuelrecords(FR_ID, VEH_LIST_REG, FR_WE) VALUES '.implode(',', $row_data); 
#$result = mysql_query($query); 

# get fuelrecord id 
$fuelrecords_ID = mysql_insert_id(); 

# if the user submitted diesel information 
if(isset($FR_DIE_L)) 
{ 

# and insert the diesel details 
$sql = "INSERT INTO fuelrecords_die(FRD_ID,FR_DIE_L,FR_DIE_C,fuelrecords_ID) VALUES ".implode(',', $row_data1); 
$result = mysql_query($sql); 
} 

# if the user submitted petrol information 
if(isset($FR_PET_L)) 
{ 

# and insert the diesel details 
$sql = "INSERT INTO fuelrecords_pet(FRP_ID,FR_PET_L,FR_PET_C,fuelrecords_ID) VALUES ".implode(',', $row_data2); 
$result = mysql_query($sql); 
} 

# if the user submitted oil information 
if(isset($FR_OIL_L)) 
{ 

# and insert the oil details 
$sql = "INSERT INTO fuelrecords_oil(FRO_ID,FR_OIL_L,FR_OIL_C,fuelrecords_ID) VALUES ".implode(',', $row_data3); 
$result = mysql_query($sql); 
} 

if (mysql_query($query)) 
    echo '<font color=\"FFFFFF\" size=\"3px\">Successful inserted</font>'; 
else 
    echo '<font color=\"FFFFFF\" size=\"3px\">Insert failed</font>'; 
} 

?> 

<?php 
mysql_close($con) 
?> 

表如下:

fuelrecords 
FR_ID (Auto increment) 
VEH_LIST_REG 
FR_WE 

fuelrecords_die 
FRD_ID (AUTO INCREMENT) 
FR_DIE_L 
FR_DIE_C 
fuelrecords_ID (foreign ID from fuelrecords) 

fuelrecords_pet 
FRP_ID (AUTO INCREMENT) 
FR_PET_L 
FR_PET_C 
fuelrecords_ID (foreign ID from fuelrecords) 

fuelrecords_oil 
FRO_ID (AUTO INCREMENT) 
FR_OIL_L 
FR_OIL_C 
fuelrecords_ID (foreign ID from fuelrecords) 

基本上目的是記錄車輛的燃料使用和成本。由於那裏不會總是汽油,柴油和機油的數據,因此單獨的表格只需要記錄數據。 希望這澄清

一個總是所有幫助和援助深表感謝。

+2

什麼都你做什麼,從來沒有發佈與用戶ID和口令代碼(在這裏連接mysql) – Roger 2012-03-11 11:13:07

回答

1

如果我正確理解您的代碼,您有4個表格:fuelrecords,fuelrecords_die,fuelrecords_pet,fuelrecords_oil

的3個表fuelrecords_diefuelrecords_petfuelrecords_oil各有一個外鍵fuelrecords_idfuelrecords.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插入到其他3個表的每個元組中。如果$i是輸入數據$_POST['FR_DIE_L'][$i](開始於$i==0)的指數,fuelrecords_idmysql_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); 
    } 
} 

?> 
+0

增加了一個長的例子。代碼沒有經過測試,所以要小心錯誤。 ;-) – Basti 2012-03-11 12:12:57

+0

代碼很好,謝謝你把燃料記錄ID放到相應的其他表格中。 我的問題是停止添加空白條目。此外,我剛剛意識到,如果沒有記錄添加到任何其他表格中,幾個星期的車輛記錄可能不需要寫入燃料記錄表中。我猜測最好的方法是讓代碼可靠地不輸入0到表格中,然後如果沒有任何其他表格的數據,則用條目加入燃料記錄來解決問題。 – Burdie87 2012-03-11 13:12:38

+0

你可以在插入前檢查'empty($ _ POST ['FR_DIE_L'] [$ row])'等。 – Basti 2012-03-11 14:11:05