我有4个表,tblstaff,tblgrade,tblperformance和tblcategory。在tblperformance中,有3个键。 perID是主键。 staffNo和catID是复合键。
我在tblcategory和staffNo中使用catID作为自动增量,我自己填写tblstaff。我运行代码时,我的sql语句没有问题。
如何从tblstaff和tblcategory中检索staffNo和catID以使用sql中的insert语句插入tblperformance?
<?php
$staffNo=$_POST['staffNo'];
$staffName=$_POST['staffName'];
$grade=$_POST['grade'];
$gradePosition=$_POST['gradePosition'];
$gradeDepartment=$_POST['gradeDepartment'];
$catTechnical=$_POST['catTechnical'];
$catOtherTechnical=$_POST['catOtherTechnical'];
$catTechnicalDescription=$_POST['catTechnicalDescription'];
$catOtherTechnicalDescription=$_POST['catOtherTechnicalDescription'];
$catWeightage=$_POST['catWeightage'];
$perReqScore=$_POST['perReqScore'];
$perActScore=$_POST['perActScore'];
$perAction=$_POST['perAction'];
$perOtherAction=$_POST['perOtherAction'];
$perTrainingIlsas=$_POST['perTrainingIlsas'];
$perTrainingPublic=$_POST['perTrainingPublic'];
$sql1="INSERT INTO tblstaff(staffNo, staffName)VALUES('$staffNo', '$staffName')";
$result1=mysql_query($sql1);
$sql2="INSERT INTO tblgrade(grade, gradePosition, gradeDepartment)VALUES('$grade', '$gradePosition', '$gradeDepartment')";
$result2=mysql_query($sql2);
$sql3="INSERT INTO tblcategory(catTechnical, catOtherTechnical, catTechnicalDescription, catOtherTechnicalDescription,catWeightage)
VALUES('$catTechnical', '$catOtherTechnical', '$catTechnicalDescription', '$catOtherTechnicalDescription', '$catWeightage')";
$result3=mysql_query($sql3);
$sql4="INSERT INTO tblperformance(perReqScore, perActScore, perAction, perOtherAction, perTrainingIlsas, perTrainingPublic)
VALUES('$perReqScore','$perActScore', '$perAction', '$perOtherAction', '$perTrainingIlsas', '$perTrainingPublic')";
$result4=mysql_query($sql4);
if(($result1 || $result2) || ($result3 || $result4) == TRUE)
{
echo "<script>alert('Data inserted successfully')</script>";
}
else
{
echo "ERROR";
}
?>
答案 0 :(得分:0)
根据您的情况检索数据:
$query = "SELECT * FROM tblcategory ORDER BY catID DESC LIMIT 1";
$result = mysql_query($query);
while ($row = mysql_fetch_array($result)) {
$latestCatID = $row["0"];
}
SELECT * FROM tblcategory ORDER BY catID DESC LIMIT 1
为您提供插入的最后一条记录。
使用staffNo,您可以使用$staffNo
最后,您对tblperformance的插入将如下所示:
$sql4="INSERT INTO tblperformance(catID, staffNo, perReqScore, perActScore, perAction, perOtherAction, perTrainingIlsas, perTrainingPublic)
VALUES('$latestCatID', '$staffNo', '$perReqScore','$perActScore', '$perAction', '$perOtherAction', '$perTrainingIlsas', '$perTrainingPublic')";
$result4=mysql_query($sql4);
谨慎使用'$latestCatID'
和'$staffNo'
,如果您使用的是整数数据类型,则不需要单引号''
。