如何将当前记录移动到另一个表并删除当前记录

时间:2012-08-31 12:24:59

标签: php mysql

我目前正在使用PHP在本地创建一个系统,以便用户将数据插入位于一个页面的14个表中,然后结束一天中该表中的所有数据应该移动到数据库中的一个新表中,自动命名为dashboard(将存储每日数据)但最近我通过手动点击使用一个按钮来执行操作,同时上面14个表中的所有数据将被自动删除。很容易让用户在第二天插入新数据,因为今天的数据已被移动到另一个表中,当前表的数据也是空的。我在所有网站上都在搜索这个问题,但没有得到任何解决方案。希望你们所有人都可以帮助我解决这个问题。

我使用 INSERT INTO 但不能正常工作。我应该在哪里使用 FROM 标记?因为有8个表需要传输,例如:sr1,sr2,sr3等。

我的代码如下:

    if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  $insertSQL = sprintf("INSERT INTO sr1_full (date, total_pending, appt_today, percent_appt_today, percent_complete, partial_complete, full_complete, return_technical, return_customer, return_cancel, cancel, reappt, focus, total_tepi, total_bawah) SELECT date, total_pending, appt_today, percent_appt_today, percent_complete, partial_complete, full_complete, return_technical, return_customer, return_cancel, cancel, reappt, focus, total_tepi, total_bawah FROM sr1 TRUNCATE TABLE sr1",
                           GetSQLValueString($_POST['date'], "date"),
                           GetSQLValueString($_POST['total_pending'], "int"),
                           GetSQLValueString($_POST['appt_today'], "int"),
                           GetSQLValueString($_POST['percent_appt_today'], "int"),
                           GetSQLValueString($_POST['percent_complete'], "int"),
                           GetSQLValueString($_POST['partial_complete'], "int"),
                           GetSQLValueString($_POST['full_complete'], "int"),
                           GetSQLValueString($_POST['return_technical'], "int"),
                           GetSQLValueString($_POST['return_customer'], "int"),
                           GetSQLValueString($_POST['return_cancel'], "int"),
                           GetSQLValueString($_POST['cancel'], "int"),
                           GetSQLValueString($_POST['reappt'], "int"),
                           GetSQLValueString($_POST['focus'], "int"),
                           GetSQLValueString($_POST['total_tepi'], "int"),
                           GetSQLValueString($_POST['total_bawah'], "int"));

      mysql_select_db($database_pods, $pods);
      $Result1 = mysql_query($insertSQL, $pods) or die(mysql_error());

      $insertGoTo = "main.php";
      if (isset($_SERVER['QUERY_STRING'])) {
        $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
        $insertGoTo .= $_SERVER['QUERY_STRING'];
      }
      header(sprintf("Location: %s", $insertGoTo));
    }

    mysql_select_db($database_pods, $pods);
    $query_sr1 = "SELECT * FROM sr1";
    $sr1 = mysql_query($query_sr1, $pods) or die(mysql_error());
    $row_sr1 = mysql_fetch_assoc($sr1);
    $totalRows_sr1 = mysql_num_rows($sr1);

    mysql_select_db($database_pods, $pods);
    $query_sr2 = "SELECT * FROM sr2";
    $sr2 = mysql_query($query_sr2, $pods) or die(mysql_error());
    $row_sr2 = mysql_fetch_assoc($sr2);
    $totalRows_sr2 = mysql_num_rows($sr2);

    mysql_select_db($database_pods, $pods);
    $query_sr3 = "SELECT * FROM sr3";
    $sr3 = mysql_query($query_sr3, $pods) or die(mysql_error());
    $row_sr3 = mysql_fetch_assoc($sr3);
    $totalRows_sr3 = mysql_num_rows($sr3);

    mysql_select_db($database_pods, $pods);
    $query_sr4 = "SELECT * FROM sr4";
    $sr4 = mysql_query($query_sr4, $pods) or die(mysql_error());
    $row_sr4 = mysql_fetch_assoc($sr4);
    $totalRows_sr4 = mysql_num_rows($sr4);

    mysql_select_db($database_pods, $pods);
    $query_sr5 = "SELECT * FROM sr5";
    $sr5 = mysql_query($query_sr5, $pods) or die(mysql_error());
    $row_sr5 = mysql_fetch_assoc($sr5);
    $totalRows_sr5 = mysql_num_rows($sr5);

    mysql_select_db($database_pods, $pods);
    $query_stx = "SELECT * FROM stx";
    $stx = mysql_query($query_stx, $pods) or die(mysql_error());
    $row_stx = mysql_fetch_assoc($stx);
    $totalRows_stx = mysql_num_rows($stx);

    mysql_select_db($database_pods, $pods);
    $query_kl = "SELECT * FROM kl";
    $kl = mysql_query($query_kl, $pods) or die(mysql_error());
    $row_kl = mysql_fetch_assoc($kl);
    $totalRows_kl = mysql_num_rows($kl);

    mysql_select_db($database_pods, $pods);
    $query_msc = "SELECT * FROM msc";
    $msc = mysql_query($query_msc, $pods) or die(mysql_error());
    $row_msc = mysql_fetch_assoc($msc);
    $totalRows_msc = mysql_num_rows($msc);

    mysql_select_db($database_pods, $pods);
    $query_total = "SELECT * FROM total_bawah";
    $total = mysql_query($query_total, $pods) or die(mysql_error());
    $row_total = mysql_fetch_assoc($total);
    $totalRows_total = mysql_num_rows($total);

3 个答案:

答案 0 :(得分:1)

您很难找到有关如何执行此操作的信息,这是正确的方法。

您不应该复制表的结构来存储“今天”的数据和前几天的数据。您应该做的是将所有项目存储在一个仪表板表中,并根据日期列过滤以确定哪些项目被视为今天的项目。您尝试采用的方法是将行从当前表移动到存档表,这是错误的。这不是数据库的工作方式。

所有这一切,您可以使用select into table将所有数据从today_dashboard移至all_dashboard,然后delete from today_dashboard将其清除,从而实现此目的。 不要这样做。重新思考您的架构,将它们存储在一个表中。

答案 1 :(得分:1)

$current_date = date("m.d.y"); 
$query = "INSERT INTO table_name (date,column1, column2, column3,...)
          VALUES ($current_date,value1, value2, value3,...)";
$result=mysql_query($query);
  • 这样您就必须使用当前日期将所有dats插入到表格中。
  • 将记录保存到数据库后,您必须检查当前日期是否与任何记录匹配。如果发现任何记录,则必须使用删除查询删除此记录。这样您就可以删除该表中的所有现有记录

    希望这对你有用......

答案 2 :(得分:1)

使用此SQL复制数据。

insert into `CopyTable` (`colum1`,`colum2`) select `colum1`,`colum2` from `Sourcetable`

空表

TRUNCATE TABLE  `Sourcetable`

示例:

$insertSQL = "INSERT INTO sr1_full (`date`, `total_pending`, `appt_today`, `percent_appt_today`, `percent_complete`, `partial_complete`, `full_complete`, `return_technical`, `return_customer`, `return_cancel`, `cancel`, `reappt`, `focus`, `total_tepi`, `total_bawah`) SELECT `date`, `total_pending`, `appt_today`, `percent_appt_today`, `percent_complete`, `partial_complete`, `full_complete`, `return_technical`, `return_customer`, `return_cancel`, `cancel`, `reappt`, `focus`, `total_tepi`, `total_bawah` FROM `sr1`";

mysql_query($insertSQL); // data copied.

$removeSQL = "TRUNCATE TABLE sr1";

mysql_query($removeSQL); // deleted all data from sr1

//data moved