美好的一天。新手在这里。想要求帮助,如果这有用吗?我想要的是避免在参考表(学生,讲座)上插入相同的名称,而只是在联结表(student_lecture)中插入现有的ID。
这是php:
if($_SERVER['REQUEST_METHOD'] == 'POST'){
$student = $_POST['student'];
$lecture = $_POST['lecture'];
$addStudent = mysqli_query($con,"INSERT IGNORE INTO students (student) VALUES ('$student')");
$studentID = mysqli_insert_id($con);
$addLecture = mysqli_query($con,"INSERT IGNORE INTO lectures (lecture) VALUES ('$lecture')");
$lectureID = mysqli_insert_id($con);
$addClass = mysqli_query($con,"INSERT INTO student_lecture (student_id,lecture_id) VALUES ('$studentID','$lectureID')");
}
这是html:
<html>
<title>Add Class</title>
<body>
<form name="Add Class" method="post" action="<?php echo
htmlspecialchars($_SERVER["PHP_SELF"]);?>">
Student: <input type="text" name="student">
</br></br>
Lecture: <input type="text" name="lecture">
</br></br>
<input type="submit" value="Add Class">
</form>
</body>
</html>
答案 0 :(得分:0)
你可以创建一个为你插入的sql函数:
DELIMITER $$
CREATE FUNCTION INSERT_SUDENT_LECTURE(student_title TEXT, lecture_name TEXT)
BEGIN
SET @m_student_id = -1;
SET @m_lecture_id = -1;
/* get student id if exist */
SELECT s.student_id
INTO @m_student_id
FROM student s
WHERE s.student = student_title;
/* if not insert */
IF @m_student_id < 0 THEN
INSERT IGNORE INTO students (student) VALUES (student_title);
SELECT LAST_INSERT_ID()
INTO @m_student_id;
END IF;
/* get lecture id if exist */
SELECT l.lecture_id
INTO @m_lecture_id
FROM lecture l
WHERE l.lecture = lecture_name;
/* if not insert */
IF @m_lecture_id < 0 THEN
INSERT IGNORE INTO lectures (lecture) VALUES (lecture_name);
SELECT LAST_INSERT_ID()
INTO @m_lecture_id;
END IF;
/* check if all went ok */
IF @m_lecture_id > 0 && @m_student_id > 0 THEN
/* insert new values in student_lecture */
INSERT INTO student_lecture (student_id,lecture_id) VALUES (@m_student_id,@m_lecture_id)
END IF;
END $$
DELIMITER ;
然后调用这样的函数:
mysqli_query($con, "INSERT_SUDENT_LECTURE ('$studentID','$lectureID');");
你应该考虑使用prepared statements作为@ObsidianAge提及。
例如,这是带有准备好的声明的电话:
$SQL_CONN = mysqli ("host",
"user",
"password",
"dbname",
"port");
$stm = $SQL_CONN->prepare("INSERT_SUDENT_LECTURE (?,?);");
$stm->bind_param ("ss", $studentID, $lectureID);
$sqlStm->execute();
$stm->free_result();
$stm->close();
答案 1 :(得分:0)
我设法让它发挥作用。这是新代码:
if($_SERVER['REQUEST_METHOD'] == 'POST'){
$student = $_POST['student'];
$lecture = $_POST['lecture'];
/* first I have selected the entries from students and lectures table */
$students = mysqli_query($con,"SELECT * FROM students WHERE student='$student'");
$lectures = mysqli_query($con,"SELECT * FROM lectures WHERE lecture='$lecture'");
/* then fetch every rows */
$student_row = mysqli_fetch_array($students);
$lecture_row = mysqli_fetch_array($lectures);
/* here we check if student name exists */
if($student == $student_row['student']) {
/* if it exists then just update it */
$updateStudent = mysqli_query($con,"UPDATE students SET student='$student' WHERE student='$student'");
}else{
/* if it doesn't exist then add as new entry */
$addStudent = mysqli_query($con,"INSERT IGNORE INTO students (student) VALUES ('$student')");
};
/* here we check if student id exists */
if($student == $student_row['student']) {
/* if it exists then just use existing id */
$studentID = $student_row['student_id'];
}else{
/* if it doesn't exist then use a new id */
$studentID = mysqli_insert_id($con);
};
/* here we check if lecture title exists */
if($lecture == $lecture_row['lecture']) {
/* if it exists then just update it */
$updateLecture = mysqli_query($con,"UPDATE lectures SET lecture='$lecture' WHERE lecture='$lecture'");
}else{
/* if it doesn't exist then add as new entry */
$addLecture = mysqli_query($con,"INSERT IGNORE INTO lectures (lecture) VALUES ('$lecture')");
};
/* here we check if lecture id exists */
if($lecture == $lecture_row['lecture']) {
/* if it exists then just use existing id */
$lectureID = $lecture_row['lecture_id'];
}else{
/* if it doesn't exist then use a new id */
$lectureID = mysqli_insert_id($con);
};
/* then we execute on adding entries on junction table */
$addClass = mysqli_query($con,"INSERT INTO student_lecture (student_id,lecture_id) VALUES ('$studentID','$lectureID')");
}
感谢您的回复。我设法得到了一些想法。特别是@MadddinTribleD。它在StackOverFlow上很有用。