Php 如何将更新数据的id插入到不同的表中?
我想问问。。如何将更新数据的id插入不同的表 这是我编写的代码,我使用$inserted\u id=mysql\u inserted\u id(),但它不起作用。。id仍然没有插入到“updatetrail”表中,希望您能帮助我=):Php 如何将更新数据的id插入到不同的表中?,php,mysql,Php,Mysql,我想问问。。如何将更新数据的id插入不同的表 这是我编写的代码,我使用$inserted\u id=mysql\u inserted\u id(),但它不起作用。。id仍然没有插入到“updatetrail”表中,希望您能帮助我=): 您确实了解您的代码有,对吗?还有你用的是 但暂时不考虑这些,您的查询将使用内置的MySQL函数LAST\u INSERT\u ID()工作,该函数可以直接进入您的查询,并由服务器进行解释,而无需获取查询: 实际上。。。嗯,等等。。。在你的代码中。。。在此之前,您实
您确实了解您的代码有,对吗?还有你用的是
但暂时不考虑这些,您的查询将使用内置的MySQL函数LAST\u INSERT\u ID()
工作,该函数可以直接进入您的查询,并由服务器进行解释,而无需获取查询:
实际上。。。嗯,等等。。。在你的代码中。。。在此之前,您实际上没有执行插入操作。在php中没有mysql\u insert\u id()
,或者当您没有执行插入操作时,LAST\u insert\u id()
不可用
如果您想从上次查询中更新(未插入)的行中获取id,则必须选择它。$inserted\u id=$\u POST['departmentID']$inserted_id=mysql_insert_id();这是干什么用的?在mate行中更改的相同变量$inserted\u id=mysql\u insert\u id()用于获取更新数据的最后一个id。但它仍然没有得到身份证。。所以我试着把$inserted\u id=$\u POST['departmentID']放进去。。但它不起作用=_=“我尝试使用$sql2=mysql\u查询($INSERT-INTO-updatetrail(d\u-no,d\u-name,d\u-position,…)值(LAST\u-INSERT\u-ID(),“$\u-POST[d\u-name]”,…),但它仍然无法将更新数据的ID保存到“updatetrail”表中=\uu=“傻我,我假设您在尝试获取“INSERT”的ID之前实际上正在执行INSERT
。”“行。你的意思是这样吗?我尝试了,但没有工作..=”)$sql2=mysql\u query(“插入到updatetrail SELECT*FROM department WHERE d_no=$inserted\u id;”)或die(“Error:.mysql\u Error());不,不是那样。如果你想要id(?)从department,然后您需要自己从department中选择id,其中…
您不能在更新查询中或更新查询后执行您想要执行的操作,只能在插入后执行。因此,您必须单独选择它,作为单独的查询。对不起,我不明白,您能帮我看一下代码吗?(n.n)/
<?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));
}
$sql2 = mysql_query("INSERT INTO updatetrail (d_no,d_name, d_position, ...
VALUES (LAST_INSERT_ID(),'$_POST[d_name]', ...