0

我在 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 函数?我怎样才能摆脱我的问题?

4

3 回答 3

1

Be careful - think about what you really want to get:

Look at this example:

$arr1 = array("a", "b", "c", "d");
$arr2 = array("c", "d", "e", "f");

$diff1 = array_diff($arr1, $arr2);
$diff2 = array_diff($arr2, $arr1);

$diff1 contains "a" and "b" because it will return what is in the $arr1 and not in $arr2

$diff2 contains "e" and "f" because it will return what is in the $arr2 and not in $arr1

If you want to get full diff you have to merge $diff1 and $diff2 together:

$f_diff = array_merge($diff1, $diff2);

Then:

if (count($f_diff)) {
    //insert difference
}
else {
    //insert all
}
于 2013-09-17T07:19:00.977 回答
0
$diff = array_diff($nos, $id);
if (count($diff)) {
     // insert diff
} else {
     // insert full array
}

http://php.net/manual/en/function.array-diff.php

于 2013-09-17T06:14:13.943 回答
0

只需像这样结合这两个条件?:

INSERT INTO exam_sections
VALUES (SELECT * 
        FROM exam_sections
        WHERE sec_year = '$year' AND sec_course = '$course' AND sec_block = '$block'
        AND  stud_subject_id != '$subject_id'
        )

INSERT 的列和值需要正确排列!

于 2013-09-17T07:28:19.383 回答