gpt4 book ai didi

php - 使用 PHP+MySQL 成功插入后立即插入附加值

转载 作者:行者123 更新时间:2023-11-29 06:24:44 25 4
gpt4 key购买 nike

if(isset($_REQUEST['SAVE'])) {
$sql = "SELECT SecName FROM section WHERE SecID='$section'";
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result)){
$SecName = $row["SecName"];
}
$sql = "SELECT SubjName FROM subject WHERE SubjID='$subject'";
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result)){
$SubjName = $row["SubjName"];
}

$check = mysql_query("SELECT SecID FROM service where SubjID='$SubjName'")or die(mysql_error());
$bool = 1;
while($info = mysql_fetch_array( $check ))
if(($info['SecID'] == $SecName)){
$bool = 0;
}
if($bool){

$check = mysql_query("SELECT * FROM service ")or die(mysql_error());
$bool = 1;
while($info = mysql_fetch_array( $check ))
if(($info['Start_date'] == $Start_date) && ($info['Venue'] == $Venue) || ($info['Start_date'] == $Start_date) && ($info['Facilitator'] == $Facilitator)){
$bool = 0;
}
if($bool){

$sql="INSERT INTO service ( ServiceID, Date, Semester, School_year, Start_date, Venue, Facilitator, Stype, SecID, SubjID)VALUES('$RecCode','$Date','$Semester','$School_year','$Start_date','$Venue','$Facilitator','$Stype', '$SecName', '$SubjName')";



if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo '<script type="text/javascript">';
echo 'alert("Save Successfully!");';
echo 'window.location="Admin_RecSchedMapLst.php";';
echo '</script>';
mysql_close($con);
}else
{
echo '<script type="text/javascript">';
echo 'alert("Conflicting schedule for Venue or Facilitator!");';
echo '</script>';
}
}else
{
echo '<script type="text/javascript">';
echo 'alert("The SECTION has been already scheduled!");';
echo '</script>';
}
}

/* $result = mysql_query("SELECT SIDno FROM class WHERE SecID=$SecID AND SubjID=$SubjID");
$row = mysql_fetch_assoc($result);
$SIDno = $row['SIDno'];
$result = mysql_query("SELECT ServiceID FROM service WHERE SecID=$SecID AND SubjID=$SubjID");
$row = mysql_fetch_assoc($result);
$ServiceID = $row['ServiceID'];
$sql="INSERT INTO registered ( ServiceID, IDno, Stype )VALUES('$ServiceID','$SIDno','$Stype')"; */

}

这段代码工作正常。我只是想问我如何才能处理上面评论的代码。我想要做的就是当插入 serviceid 时,我还想立即将 serviceID、idno、类型插入到已注册的另一个表中。

最佳答案

使用 mysql_insert_id 更容易、更好函数来获取刚刚插入的行的 ID,而不是执行另一个 SELECT。例如:

$sql="INSERT INTO service ( ServiceID, Date, Semester, School_year, Start_date, Venue, Facilitator, Stype, SecID, SubjID) VALUES ('$RecCode','$Date','$Semester','$School_year','$Start_date','$Venue','$Facilitator','$Stype', '$SecName', '$SubjName')";

if (mysql_query($sql,$con)) {
$ServiceID = mysql_insert_id();
$sql="INSERT INTO registered ( ServiceID, IDno, Stype )VALUES('$ServiceID','$SIDno','$Stype')";
mysl_query($sql, $con); // check for error here too though
}
else {
echo "OH NO!!!";
}

另外,请注意 SQL injection与您构建 SQL 字符串的方式有关。

关于php - 使用 PHP+MySQL 成功插入后立即插入附加值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/596228/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com