【问题标题】:How to save in multiple table when I click save button?单击保存按钮时如何保存在多个表中?
【发布时间】:2012-11-20 13:49:15
【问题描述】:

我需要将动态文本框中的值同时保存在不同的表格中。有人可以帮我这样做吗?我有 4 张桌子需要填写。这是我的表格及其字段:

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

我的 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.';

                                    }

}

}

?>

【问题讨论】:

  • 您是否尝试向您的 SQL 服务器发出 4 个INSERT INTO 语句?
  • 是的,我现在试试,但似乎只捕获了备注。其他 3 张桌子没有价值。
  • 请帮帮我.. :(

标签: php mysql


【解决方案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() 函数——当它需要一个数组时,您正在向它传递一个布尔表达式。总的来说,我建议您再看看您的代码是如何运作的。

【讨论】:

  • 抱歉,我上传的代码现已更新。请查看我最近编辑的代码。只有备注表有值,其他三个表是空的,没有得到我输入的值。
【解决方案2】:

将四个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.';
      }
  }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-04
    • 1970-01-01
    • 2011-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多