douzhi2760 2012-11-20 02:01
浏览 13

单击保存按钮时如何保存在多个表中?

I need to save the values from my dynamic textbox in different tables at the same time. Can someone help me do this? I have 4 tables that needs to be filled. This is my tables and its fields:

table1 - desk_id - desk_user - desk_report - desk_action

table2 - print_id - print_brand - print_model - print_report - print_action

table3 - tel_id - tel_local - tel_user - tel_report - tel_action

table4 - remarks_id - remarks

My PHP code:

   <?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.';

                                    }

}

}

?>
  • 写回答

2条回答 默认 最新

  • dousi4148 2012-11-20 02:08
    关注

    Do four INSERT as a loop?

    $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.';
          }
      }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 有人能看一下我宿舍管理系统的报修功能该怎么改啊?链表那里总是越界
  • ¥15 cs loadimage运行不了,easyx也下了,没有用
  • ¥15 r包runway详细安装教程
  • ¥15 Html中读取Json文件中数据并制作表格
  • ¥15 谁有RH342练习环境
  • ¥15 STM32F407 DMA中断问题
  • ¥15 uniapp连接阿里云无法发布消息和订阅
  • ¥25 麦当劳点餐系统代码纠错
  • ¥15 轮班监督委员会问题。
  • ¥20 关于变压器的具体案例分析