2012-11-20 50 views
1

我需要將我的動態文本框中的值同時保存在不同的表中。有人可以幫我做這個嗎?我有4個表格需要填寫。這是我的表和字段:當我點擊保存按鈕時如何保存在多個表中?

表1 - desk_id - desk_user - desk_report - desk_action

表2 - print_id - print_brand - print_model - print_report - print_action

表3 - tel_id - tel_local - tel_user - tel_report - tel_action

表4 - remarks_id - remarks

我的PHP代碼:

<?php 

$con = mysql_connect ("localhost","root","nasi") or die 
('cannot connect to database error: '.mysql_error()); 


if (isset($_POST['desk_user']) && 
isset($_POST['desk_report']) && 
isset($_POST['desk_action']) && 
isset($_POST['print_brand']) && 
isset($_POST['print_model']) && 
isset($_POST['print_report']) && 
isset($_POST['print_action']) && 
isset($_POST['tel_local']) && 
isset($_POST['tel_user']) && 
isset($_POST['tel_report']) && 
isset($_POST['tel_action']) && 
isset($_POST['remarks'])) 
{ 

$desk_user = $_POST['desk_user']; 
$desk_report = $_POST['desk_report']; 
$desk_action = $_POST['desk_action']; 
$print_brand = $_POST['print_brand']; 
$print_model = $_POST['print_model']; 
$print_report = $_POST['print_report']; 
$print_action = $_POST['print_action']; 
$tel_local = $_POST['tel_local']; 
$tel_user = $_POST['tel_user']; 
$tel_report = $_POST['tel_report']; 
$tel_action = $_POST['tel_action']; 
$remarks = $_POST['remarks']; 

if (!empty($desk_user)&& !empty($desk_report)&& !empty($desk_action) && !empty($print_brand) && !empty($print_model) && !empty($print_report) && !empty($print_action) && !empty($tel_local) && !empty($tel_user) && !empty($tel_report) && !empty($tel_action) && !empty($remarks)) { 

mysql_select_db("csr", $con); 
$queries = array(); 
for($i=0; $i<count($desk_user || $print_brand || $tel_local || $remarks); $i++) 
{ 
    $queries [] = "('" .$desk_user [$i ] . "', '" .$desk_report [$i ] . "', '" .$desk_action [$i ] . "')" ; 

    $queries1 [] = "('" .$print_brand [$i ] . "', '" .$print_model [$i ] . "', '" .$print_report [$i ] . "', '" .$print_action [$i ] . "')" ; 

    $queries2 [] = "('" .$tel_local [$i ] . "', '" .$tel_user [$i ] . "', '" .$tel_report [$i ] . "', '" .$tel_action [$i ] . "')" ; 

    $queries3 [] = "('" .$remarks [$i ] . "')" ; 
} 

if(count($queries) == 0) 
{ 
    # Nothing passed 
    # exit 
} 

$query = "insert into desktoplaptop (desk_user, desk_report, desk_action tel_local) values " . implode(", ", $queries) ; 

$query1 = "insert into printer (print_brand, print_model, print_report, print_action) values " . implode(", ", $queries1) ; 

$query2 = "insert into tel (tel_user, tel_report, tel_action) values " . implode(", ", $queries2) ; 

$query3 = "insert into remarks (remarks) values " . implode(", ", $queries3) ; 

if ($sql_run = mysql_query($query) || $sql_run = mysql_query($query1) || $sql_run = mysql_query($query2) || $sql_run = mysql_query($query3)) { 
          echo 'ok.'; 
           } 
         else { 
            echo '*Sorry, we couldn\'t register you at this time. Try again later.'; 

            } 

} 

} 

?> 
+1

您是否嘗試向您的SQL服務器發出4條'INSERT INTO'語句? – Nemoden

+0

是的,我現在嘗試它,但似乎只有評論被捕獲。其他3張桌子沒有價值。 – user1831375

+0

幫助我的人請.. :( – user1831375

回答

0

做四個INSERT作爲循環?

$query[0] = "INSERT INTO TABLE1 (...) VALUES (...)"; 
$query[1] = "INSERT INTO TABLE2 (...) VALUES (...)"; 
//etc... 

foreach ($query as $x) 
    { 
     if ($sql_run = mysql_query($x)) { 
     echo 'ok.'; 
     } else { 
     echo '*Sorry, we couldn\'t register you at this time. Try again later.'; 
     } 
    } 
1

如果有四張桌子,有需要爲每一個獨特的INSERT語句。您提供的代碼,你只命名一個表:desktoplaptop

如果實際上有四種獨特的表按上面的列表作爲建議,你需要寫它是指每個表的模式獨特的INSERT語句。

例如:

$queries = array(); 
if(!empty($desk_user)) { 
    $queries[] = "INSERT into desktop (desk_user, desk_report, desk_action) VALUES ('" . $desk_user . "', '" .$desk_report . "', '" . $desk_action . "')'"; 
} 

重複其他3個表

foreach($queries as $query) { 
    if ($sql_run = mysql_query($query)) { 
     echo 'ok.'; 
    } else { 
     echo '*Sorry, we couldn\'t register you at this time. Try again later.'; 
    } 
} 

請注意,如果您是從Web表單以輸入,你也想mysql_escape_string()每個$ _ POST變量防止注射。另外,你似乎錯誤地使用了count()函數 - 當它期望一個數組時,你將它傳遞給一個布爾表達式。總的來說,我會建議再仔細看看你的代碼如何運作。

+0

對不起,我上傳的代碼現在已更新。請參閱我最近編輯的代碼。只有備註表得到的值,而其他三個表是空的,並沒有得到我輸入的值。 – user1831375

相關問題