简体   繁体   中英

inserting data from multiple tables into one separate table

First of all, I know MySQL is a depreciated language, but my supervisor insists I use it.

I have three tables in my database. They are : "students", "tutors", "match" . When I find a run the below query($query2) I am checking the make sure all the criteria in that query is perfectly matched.

When it is perfectly matched I want to insert the following into the match table:

"insert into match (tutor_id, student_id, school_id) values ('$tutor_id','$student_id','$school_id')";

The "match" table contains the columns:

match_id(auto incremented), tutor_id, student_id, school_id

Any help would be greatly appreciated.

if(mysql_query($query2)){
    $check_availability =  "select * 
                            from tutors, students 
                            where tutor_availability = '$student_availability'
                            AND (tutor_subject_1 = '$student_subject_1'
                            OR tutor_subject_1 = '$student_subject_2'
                            OR tutor_subject_2 = '$student_subject_2'
                            OR tutor_subject_2 = '$student_subject_1')";    

    $run_5 = mysql_query($check_availability);

    if(mysql_num_rows($run_5)>0) {

        echo  "<script>alert('we have a match')</script>";
        my_sql_query($query3) =  "insert into match (tutor_id, student_id, school_id) values ('$tutor_id','$student_id','$school_id')";

        mysql_query($query3);
    } else{
        echo  "<script>alert('no match found')</script>";
    }
}

I think this line is wrong:

my_sql_query($query3) =  "insert into match (tutor_id, student_id, school_id) values ('$tutor_id','$student_id','$school_id')";

The function name is spelled wrong and the parameter shouldn't be $query3 but the actual query:

mysql_query("insert into match (tutor_id, student_id, school_id) values ('$tutor_id','$student_id','$school_id')");

Basically you have to insert the output of the previous query into the database.

change

$check_availability =  "select * 
                        from tutors, students 
                        where tutor_availability = '$student_availability'
                        AND (tutor_subject_1 = '$student_subject_1'
                        OR tutor_subject_1 = '$student_subject_2'
                        OR tutor_subject_2 = '$student_subject_2'
                        OR tutor_subject_2 = '$student_subject_1')";  

to

$check_availability =  "insert into match (tutor_id, student_id, school_id) 
                        select tutors.id, students.id, students.school_id 
                        from tutors, students 
                        where tutor_availability = '$student_availability'
                        AND (tutor_subject_1 = '$student_subject_1'
                        OR tutor_subject_1 = '$student_subject_2'
                        OR tutor_subject_2 = '$student_subject_2'
                        OR tutor_subject_2 = '$student_subject_1')";  

You have to loop through the row to get the values to insert:

if(mysql_query($query2)){
    $check_availability =  "select * 
                            from tutors, students 
                            where tutor_availability = '$student_availability'
                            AND (tutor_subject_1 = '$student_subject_1'
                            OR tutor_subject_1 = '$student_subject_2'
                            OR tutor_subject_2 = '$student_subject_2'
                            OR tutor_subject_2 = '$student_subject_1')";    

    $run_5 = mysql_query($check_availability);

    if(mysql_num_rows($run_5)>0) {

        echo  "<script>alert('we have a match')</script>";
        while($row = mysql_fetch_assoc($run_5)) {
            $tutor_id = $row['tutor_id'];
            $student_id = $row['student_id'];
            $school_id = $row['school_id'];
            mysql_query("insert into match (tutor_id, student_id, school_id) values ('$tutor_id','$student_id','$school_id')");
        }

    } else{
        echo  "<script>alert('no match found')</script>";
    }
}

I don't know the exact field names, but it should point you in the right direction.

Maybe try using mysql_fetch_row($run_5) and change the select query to only pull out tutor_id, student_id and school_id.

if(mysql_query($query2)){
$check_availability =  "select t.tutor_id, s.student_id, t.school_id
                        from t.tutors, s.students 
                        where t.tutor_availability = '$student_availability'
                        AND (t.tutor_subject_1 = '$student_subject_1'
                        OR t.tutor_subject_1 = '$student_subject_2'
                        OR t.tutor_subject_2 = '$student_subject_2'
                        OR t.tutor_subject_2 = '$student_subject_1')";    

$run_5 = mysql_query($check_availability);

if(mysql_num_rows($run_5)>0) {

    echo  "<script>alert('we have a match')</script>";
    while($row = mysql_fetch_row($run_5)) {
        $tutor_id = $row[0];
        $student_id = $row[1];
        $school_id = $row[2];
        mysql_query("insert into match (tutor_id, student_id, school_id) values ('$tutor_id','$student_id','$school_id')");
    }

} else{
    echo  "<script>alert('no match found')</script>";
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM