插入多对多关系设置

j8yoct9x  于 2021-06-24  发布在  Mysql
关注(0)|答案(2)|浏览(340)

很好的一天。我是新手。想寻求帮助,如果这将如何工作?我想要的是避免在参考表(students,teachments)中插入相同的名称,而只是在连接表(student\u teachments)中插入现有的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>

学生讲座
学生
讲座

kcugc4gi

kcugc4gi1#

您可以创建一个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');");

你真的应该考虑使用@obsidiange提到的准备好的语句。
例如,使用准备好的语句调用:

$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();
smdnsysy

smdnsysy2#

我设法使它起作用了。以下是新代码:

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')");

}

谢谢你的回复。我设法想出了一些主意。特别是给@maddintribled。在这里很好。

相关问题