我在 php 中遇到了数组问题;具体来说,我想比较数据库中的两个数组并得到它们的区别。并且差异将存储在数据库中。我是 php 新手。
我的代码看起来像:
这是我得到我的数组的地方:
$year = $_POST['year'];
$course = $_POST['Course'];
$block = $_POST['block'];
$cheetah = mysql_query("SELECT stud_valid_nos FROM exam_students WHERE stud_subject_id = '$subject_id'")or die(mysql_error());
while($validnos = mysql_fetch_array($cheetah)){
$nos = $validnos['stud_valid_nos'];
}
$tiger = mysql_query("SELECT sec_id_num FROM exam_sections WHERE sec_year = '$year' AND sec_course = '$course' AND sec_block = '$block'")or die(mysql_error());
while($idnum = mysql_fetch_array($tiger)){
$id = $idnum['stud_valid_nos'];
}
if($nos ???? $id){ ///this is where the arrays being compared.
inserts only the difference
}
else{
inserts the full array in the database
}
更新
我决定不再使用数组,也许你们中的一些人有一点,所以我重新组织了我的代码,但它仍然不起作用。如何在我的数据库中添加没有重复的学生编号?
这是我的新代码:
$cheetah = mysql_query("SELECT stud_valid_nos FROM exam_students WHERE stud_subject_id = '$subject_id'")or die(mysql_error());
if(mysql_num_rows($cheetah)>0){
while($validnos = mysql_fetch_array($cheetah)){
$nos = $validnos['stud_valid_nos'];
$tiger = mysql_query("SELECT sec_id_num,sec_email FROM exam_sections WHERE sec_id_num <> '$nos' AND sec_year = '$year' AND sec_course = '$course' AND sec_block = '$block'")or die(mysql_error());
while($idnum = mysql_fetch_assoc($tiger)){
$id = $idnum['sec_id_num'];
$email = $idnum['sec_email'];
$insert = mysql_query("INSERT INTO exam_students (stud_valid_nos, stud_email, stud_subject_id, stud_group_id ) VALUES ('$id','$email','$subject_id','$examinergroupid')")or die(mysql_error());
$last = mysql_insert_id();
$password = genRandomString();
$rawr = mysql_query("INSERT INTO exam_passwords (pass_user_id,pass_password,pass_subject_id,pass_exam_id) VALUES ('$last','$password', '$subject_id', 0)")or die(mysql_error());
}
}
也许我应该在 mysql_query 中使用 NOT IN 或 IS NULL 函数?我怎样才能摆脱我的问题?