0

First of all I know php mysql_* functions are depreciated, but my supervisor wants me to use the regardless.

In this piece of code everything is running fine, except the line: $insert =

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

The data is printed out correctly using the line:

      echo  "<script>alert('$tutor_id, $student_id')</script>";

However the data still won't go into the table "match". I can't figure this out

Any help would be greatly appreciated.

    mysql_query($insert);

        $run_6  =   mysql_query("select tutors.tutor_id, students.student_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')");

while($row = mysql_fetch_row($run_6)) {
    $tutor_id = $row[0];
    $student_id = $row[1];

     echo  "<script>alert('$tutor_id, $student_id')</script>";

    $insert = "insert into match (t_id, s_id) values ('$tutor_id', '$student_id')";

    mysql_query($insert);
}
bano590
  • 39
  • 1
  • 8

1 Answers1

1

match is a reserved keyword. That means if you are going to name a table that you must wrap it in ticks:

insert into `match` (t_id, s_id) values ('$tutor_id', '$student_id')

This would have been brought to your attention if you had error checking and handling in your code. At the very least during development check mysql_error().

John Conde
  • 217,595
  • 99
  • 455
  • 496
  • Thank you, you legend. I should have put in these checks earlier, it would have saved me a lot of bother – bano590 Mar 26 '14 at 18:48
  • I cannot agree more with @JohnConde comments on adding appropriate error handling to your code. This should be done ALWAYS, in ALL CASES when working with databases. Check for and handle errors on server connection; check for and handle errors on DB selection; check for and handle errors on query execution; etc. – Mike Brant Mar 26 '14 at 18:48
  • thanks guys, harsh lesson learned. for anyone checking this out in the future the 'match' didn't work, so I just changed the table name to "matches" – bano590 Mar 26 '14 at 18:57