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字符串的方式SQL injection

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

10-16 22:57