简体   繁体   English

以一种形式在不同的表中插入和更新

[英]insert and update in different table in one form

Its possible insert data in table A but in the same time, update data in table B in one submit button?where should i put the insert query? 它可以在表A中插入数据,但同时用一个提交按钮更新表B中的数据?我应该在哪里插入查询? Should i put them in the update query or declare one more query? 我应该将它们放在更新查询中还是声明另一个查询? I am a beginner in writing code. 我是编写代码的初学者。 Please someone help me solving this problem. 请有人帮我解决这个问题。 Below is my page code. 下面是我的页面代码。

<?php
include ("supsrwk_epenyenggaraan.php");
 ?>
<?php

// initialize the session

if (!isset($_SESSION)) {
    session_start();
}

// ** Logout the current user. **

$logoutAction = $_SERVER['PHP_SELF'] . "?doLogout=true";

if ((isset($_SERVER['QUERY_STRING'])) && ($_SERVER['QUERY_STRING'] != "")) {
    $logoutAction.= "&" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_GET['doLogout'])) && ($_GET['doLogout'] == "true")) {

    // to fully log out a visitor we need to clear the session varialbles

    $_SESSION['MM_Username'] = NULL;
    $_SESSION['MM_UserGroup'] = NULL;
    $_SESSION['PrevUrl'] = NULL;
    unset($_SESSION['MM_Username']);
    unset($_SESSION['MM_UserGroup']);
    unset($_SESSION['PrevUrl']);
    $logoutGoTo = "/supsrwk_epenyenggaraan/stafflogin.php";
    if ($logoutGoTo) {
        header("Location: $logoutGoTo");
        exit;
    }
}

?>

<?php
include ("supsrwk_epenyenggaraan.php");
 ?>
<?php

// initialize the session

if (!isset($_SESSION)) {
    session_start();
}

if (!function_exists("GetSQLValueString")) {
    function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
    {
        if (PHP_VERSION < 6) {
            $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
        }

        $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
        switch ($theType) {
        case "text":
            $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
            break;

        case "long":
        case "int":
            $theValue = ($theValue != "") ? intval($theValue) : "NULL";
            break;

        case "double":
            $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
            break;

        case "date":
            $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
            break;

        case "defined":
            $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
            break;
        }

        return $theValue;
    }
}

$editFormAction = $_SERVER['PHP_SELF'];

if (isset($_SERVER['QUERY_STRING'])) {
    $editFormAction.= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) {
    $updateSQL = sprintf("UPDATE aduan SET statusAduan=%s WHERE aduanID=%s", GetSQLValueString($_POST['statusAduan'], "text") , GetSQLValueString($_POST['aduanID'], "int"));
    mysql_select_db($database_supsrwk_epenyenggaraan, $supsrwk_epenyenggaraan);
    $Result1 = mysql_query($updateSQL, $supsrwk_epenyenggaraan) or die(mysql_error());
    $updateGoTo = "/supsrwk_epenyenggaraan/rekodDalamProses.php?aduanID=" . $row_aduan['aduanID'] . "";
    if (isset($_SERVER['QUERY_STRING'])) {
        $updateGoTo.= (strpos($updateGoTo, '?')) ? "&" : "?";
        $updateGoTo.= $_SERVER['QUERY_STRING'];
    }

    header(sprintf("Location: %s", $updateGoTo));
}

$colname_staff = "-1";

if (isset($_SESSION['MM_Username'])) {
    $colname_staff = $_SESSION['MM_Username'];
}

mysql_select_db($database_supsrwk_epenyenggaraan, $supsrwk_epenyenggaraan);
$query_staff = sprintf("SELECT * FROM staff WHERE No_KP = %s", GetSQLValueString($colname_staff, "text"));
$staff = mysql_query($query_staff, $supsrwk_epenyenggaraan) or die(mysql_error());
$row_staff = mysql_fetch_assoc($staff);
$totalRows_staff = "-1";

if (isset($_SESSION['MM_Username'])) {
    $totalRows_staff = $_SESSION['MM_Username'];
}

mysql_select_db($database_supsrwk_epenyenggaraan, $supsrwk_epenyenggaraan);
$query_staff = sprintf("SELECT * FROM staff WHERE No_KP = %s", GetSQLValueString($colname_staff, "text"));
$staff = mysql_query($query_staff, $supsrwk_epenyenggaraan) or die(mysql_error());
$row_staff = mysql_fetch_assoc($staff);
$totalRows_staff = mysql_num_rows($staff);
$maxRows_aduan = 10;
$pageNum_aduan = 0;

if (isset($_GET['pageNum_aduan'])) {
    $pageNum_aduan = $_GET['pageNum_aduan'];
}

$startRow_aduan = $pageNum_aduan * $maxRows_aduan;
mysql_select_db($database_supsrwk_epenyenggaraan, $supsrwk_epenyenggaraan);
$query_aduan = "SELECT * FROM aduan WHERE statusAduan = 'PERMOHONAN'";
$query_limit_aduan = sprintf("%s LIMIT %d, %d", $query_aduan, $startRow_aduan, $maxRows_aduan);
$aduan = mysql_query($query_limit_aduan, $supsrwk_epenyenggaraan) or die(mysql_error());
$row_aduan = mysql_fetch_assoc($aduan);

if (isset($_GET['totalRows_aduan'])) {
    $totalRows_aduan = $_GET['totalRows_aduan'];
}
else {
    $all_aduan = mysql_query($query_aduan);
    $totalRows_aduan = mysql_num_rows($all_aduan);
}

$totalPages_aduan = ceil($totalRows_aduan / $maxRows_aduan) - 1;

Why don't you try using two sql statements? 为什么不尝试使用两个sql语句?

SQL1->"INSERT INTO table1 (col1, col2, col3) VALUES (val1, val2, val3)";
SQL2->"UPDATE table2 SET ....";

Both statements get executed after the button is pressed... 按下按钮后,两个语句都将执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在不同的行中按顺序更新并插入一个新的? - How to update sequentially in order in different rows and insert a new one? 提交后,将form7数据插入到不同的数据库表中 - after submission contact form7 data insert into different database table 仅使用一个函数来更新数据库中表的不同列 - Using only one function to update different columns of a table in database 在表单提交之前将使用AJAX更新数据库,从而允许我将新编辑的数据插入到新的mysql表中 - Will using AJAX to update database before form submit allowing me to insert newly edited data into new mysql table 将具有不同colspan的行插入表中 - Insert row with different colspan into table 有没有办法在表的一个字段中插入多个数据,并且一个特定数据在其字段上具有不同的值? 代码点火器 - Is there any way to insert multiple data in a field of table and one specific data has different value on its field? Codeigniter Laravel多次插入一个表 - Laravel multiple insert into one table 一种形式的两种不同动作 - Two different actions in one form 将不同的表单选项卡处理为一个表单 - Handle different form tabs as one form 如何在Bootstrap表中插入和更新数据? - How to insert and update data in Bootstrap table?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM