简体   繁体   English

如何将更新数据的ID插入其他表?

[英]How can i insert the id of updated data into different table?

i want to ask.. how can i insert the id of updated data into different table?.. 我想问..如何将更新数据的ID插入不同的表中?

this is the codes that i had made, i'm using $inserted_id= mysql_inserted_id() but it's not working.. the id still not insert into "updatetrail" table, hope u can help me =) : 这是我编写的代码,我正在使用$ inserted_id = mysql_inserted_id(),但是它不起作用.. id仍未插入“ updatetrail”表中,希望您能为我提供帮助=):

    <?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $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']);
}

$count=0;

if ((isset($_POST["MM_update"]))) {
foreach ($_FILES['file']['name'] as $certificate) {

$allowedExts = array("gif", "jpeg", "jpg", "png", "txt", "pdf");
  if ($_FILES['file']['error'][$count]!=4 && $_FILES["file"]["error"][$count] > 0)
    {
        echo "Return Code: " . $_FILES["file"]["error"][$count] . "<br>";
    }
  else
    {
  if ($_FILES['file']['error'][$count]!=4) {
    if (file_exists("documents/" . $_FILES["file"]["name"][$count]))
          {
          print '<script type="text/javascript">'; 
          print 'alert("The certificate '. $_FILES["file"]["name"][$count].' is already exists, try rename the file and try again. =) ")';      print '</script>';

        echo "<script language=\"JavaScript\">{                                                                                         
        location.href=\"update(power).php\";                                                                                               
        }</script>";
          }
        else
          {
          move_uploaded_file($_FILES["file"]["tmp_name"][$count],
          "documents/" . $_FILES["file"]["name"][$count]);
          }

        $file=fopen("documents/". $_FILES["file"]["name"][$count],"r") or exit("Unable to open file!");

        $j=0;
        while(!feof($file))
        {

        $thetoken[$j]=fgets($file, 40);
        $j++;

        }
    }

  $updateSQL = sprintf("UPDATE department SET d_name=%s, d_staff_name=%s, d_position=%s, d_noic_nopassport=%s, d_type_training=%s, d_date_training=%s, d_date_expired=%s, d_sijil=%s WHERE d_no=%s",
                       GetSQLValueString($_POST['d_name'], "text"),
                       GetSQLValueString($_POST['d_staff_name'], "text"),
                       GetSQLValueString($_POST['d_position'], "text"),
                       GetSQLValueString($_POST['d_noic_nopassport'], "int"),
                       GetSQLValueString($_POST['d_type_training'], "text"),
                       GetSQLValueString($_POST['d_date_training'], "text"),
                       GetSQLValueString($_POST['d_date_expired'], "date"),
                       GetSQLValueString(addslashes($_FILES['file']['name'][$count]), "text"),
                       GetSQLValueString($_POST['departmentID'], "int"));

 mysql_select_db($database_dbconn, $dbconn);
$Result1 = mysql_query($updateSQL, $dbconn) or die(mysql_error());      

$inserted_id=$_POST['departmentID'];                       
$inserted_id = mysql_insert_id();

$sql2 = mysql_query("INSERT INTO updatetrail (d_no,d_name, d_position, d_staff_name, d_noic_nopassport, d_type_training, d_date_training, d_date_expired, d_sijil)
VALUES
($inserted_id,'$_POST[d_name]','$_POST[d_position]','$_POST[d_staff_name]','$_POST[d_noic_nopassport]','$_POST[d_type_training]','$_POST[d_date_training]','$_POST[d_date_expired]','".addslashes($_FILES['file']['name'][$count])."')") or die("Error: " . mysql_error());

insertAuditTrail(date('Y-m-d H:i:s'),"Module:Update User Details" ,"User Details successfully edited",$_SESSION['userID'],$inserted_id); 
  $Result1 = mysql_query($updateSQL, $dbconn) or die(mysql_error());

    $count=$count + 1;


    }   
      echo "<script language=\"JavaScript\">{                                                                                         
    location.href=\"update(power).php\";                                                                                               
    }</script>";
    if (isset($_SERVER['QUERY_STRING'])) {
   $updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";
    $updateGoTo .= $_SERVER['QUERY_STRING'];
  }
   header(sprintf("Location: %s", $updateGoTo));    
}

You do understand that your code has unspeakably massive security flaws , right? 您确实知道您的代码具有难以置信的巨大安全漏洞 ,对吗? Also that you're using a deprecated function ? 还要使用不推荐使用的函数吗?

But disregarding those for the moment, your query would work using the built-in MySQL function LAST_INSERT_ID() that can go right into your query and will be interpreted by the server without you needing to fetch it: 但是暂时不考虑这些内容,您的查询将使用内置的MySQL函数 LAST_INSERT_ID() ,该函数可以直接进入您的查询,并由服务器解释,而无需获取它:

$sql2 = mysql_query("INSERT INTO updatetrail (d_no,d_name, d_position, ...
VALUES (LAST_INSERT_ID(),'$_POST[d_name]', ...

Actually... um, wait... in your code... you aren't actually doing an insert prior to this. 实际上...嗯,等等...在您的代码中...在此之前您实际上并没有进行插入操作 There is no mysql_insert_id() in php or LAST_INSERT_ID() available when you didn't do an insert. 当您不执行插入操作时,php或LAST_INSERT_ID()没有可用的mysql_insert_id()

If you want the id from the row you updated (not inserted) in the prior query, you have to SELECT it. 如果要使用先前查询中更新 (未插入)的行中的ID,则必须SELECT它。

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

相关问题 如何使用插入ID插入另一个表? - How can I insert to another table using the insert id? 如何将数据插入表的列(2个表中的一个)中,以使用户ID在两个表中均匹配? - How can i insert data into a column of a table (out of 2 tables) such that user id matches in both of them? 如何在laravel中将具有用户ID的表格数据插入表中? - How do I insert into table form data with user id in laravel? Laravel默认身份验证寄存器如何将数据插入两个不同的表 - Laravel default auth register how can i insert data into two different table 如何将ID数组插入数据库的单个字段中 - how can i insert the array of id into a single field of data base SQL / PHP-使用PDO和表中数据的INSERT语句显示为“资源ID#”。 如何获取它以存储正确的字符串? - SQL/PHP - INSERT statement using PDO and data inside the table appears as 'Resource ID #'. How can I get it to store the correct string? 如何通过id向另一个表插入数据 - How to insert data to another table by id 如何使用同一个控制器将数据插入到不同的表中? - How can I insert data into different tables using the same controller? 如何将 json 数据插入到不同的 mysql 表中? - How can i insert the json data into different mysql tables? 如何从选中的复选框向表中插入数据? - How can I insert data to a table from the selected checkbox?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM