PHP MySQL,检查两个表中的重复项

PHP MySQL,检查两个表中的重复项,php,mysql,database,Php,Mysql,Database,我正在做一个项目,允许学生注册入学考试。如果我们在student_data表中有学生ID,我们会将信息添加到存储测试日期和时间信息的additional_data表中。如果学生不在student\u数据表中,他们将被添加到存储测试日期和时间信息的untracked\u表中 以下是我需要做的:如果学生在附加数据或未跟踪的乘务员中,并且考试日期与新条目相同,我不希望座位增加 几天来,我一直在寻找答案,并尝试了不同的查询变体,但都没有成功。以下是我从中得到的一些提示: 下面的$query1检查

我正在做一个项目,允许学生注册入学考试。如果我们在
student_data
表中有学生ID,我们会将信息添加到存储测试日期和时间信息的
additional_data
表中。如果学生不在
student\u数据
表中,他们将被添加到存储测试日期和时间信息的
untracked\u表中

以下是我需要做的:如果学生在
附加数据
未跟踪的乘务员
中,并且考试日期与新条目相同,我不希望座位增加

几天来,我一直在寻找答案,并尝试了不同的查询变体,但都没有成功。以下是我从中得到的一些提示:

下面的
$query1
检查学生是否已经存在于
附加_数据表中,但我还需要检查安置日期是否相同。如果是,那么我不想增加座位的数量。这里的
子句对我不起作用。没有
,就可以了

$query1 = $db->prepare("SELECT AES_DECRYPT(student_data_id, '".KEY."')
    FROM additional_data
    WHERE AES_DECRYPT(student_data_id, '".KEY."') = :studentDataId
    AND placement_date = :placementDate");
$query1->bindParam(":studentDataId", $studentDataId);
$query1->execute();
$query2
根本不起作用。如果我能看到id存在,它总是返回0事件

$query2 = $db->prepare("SELECT AES_DECRYPT(stu_id, '".KEY."')
    FROM untracked_attendants
    WHERE AES_DECRYPT(stu_id, '".KEY."') = :stuId
    AND test_date = :placementDate");
$query2->bindParam(":stuId", $stuId);
$query2->execute();
这是我文档中的全部代码。(无论发生什么情况,
***
的两行之间的代码都是我继承程序时更改的代码,当程序增加时:
//Return test dates as JSON
if($_POST['action'] == "getDates") {
    $query = $db->prepare("SELECT * FROM test_dates WHERE active = 1 ORDER BY date ASC");
    $query->execute();
    echo json_encode($query->fetchAll());
}

//Checks if the test date is still activated and returns an appropriate response
if($_POST['action'] == "checkDate") {
    $id = strip_tags($_POST['id']);
    $query = $db->prepare("SELECT id FROM test_dates WHERE id = :id AND active = 1");
    $query->bindParam(":id", $id);
    $query->execute();
    if($query->rowCount() > 0) {
        echo "open";
    }
    else {
        echo "closed";
    }
}

//Return test types as JSON
if($_POST['action'] == "getTests") {
    $query = $db->prepare("SELECT * FROM test_types");
    $query->execute();
    echo json_encode($query->fetchAll());
}

//Save the placement test registration
if($_POST['action'] == "save") {
    $uid = filter_var(strip_tags(trim($_POST['uid'])), FILTER_SANITIZE_STRING);
    $id = filter_var(strip_tags(trim($_POST['id'])), FILTER_SANITIZE_NUMBER_INT);
    $fname = filter_var(strip_tags(trim($_POST['firstName'])), FILTER_SANITIZE_STRING);
    $lname = filter_var(strip_tags(trim($_POST['lastName'])), FILTER_SANITIZE_STRING);
    $stuId = filter_var(strip_tags(trim($_POST['stuId'])), FILTER_SANITIZE_NUMBER_INT);
    $email = filter_var(strip_tags(trim($_POST['emailAddress'])), FILTER_SANITIZE_EMAIL);
    $retake = filter_var(strip_tags(trim($_POST['retake'])), FILTER_SANITIZE_NUMBER_INT);
    $location = filter_var(strip_tags(trim($_POST['location'])), FILTER_SANITIZE_STRING);
    $testDate = filter_var(strip_tags(trim($_POST['testDate'])), FILTER_SANITIZE_NUMBER_INT);
    if(isset($_POST['testTypes'])) {
        $testTypes = strip_tags(trim($_POST['testTypes']));
        $testTypes = str_replace("|", " ", $testTypes);
    }
    else {
        $testTypes = "";
    }

    //If the student already exists then add data relating to that record
    $query = $db->prepare("SELECT id,
            AES_DECRYPT(student_id, '".KEY."') AS student_id
            FROM student_data
            WHERE AES_DECRYPT(student_id, '".KEY."') = :student_id");
    $query->bindParam(":student_id", $stuId);
    $query->execute();

    if($query->rowCount() > 0) {
        $row = $query->fetch();
        $studentDataId = $row['id'];

        $query = $db->prepare("SELECT AES_DECRYPT(student_data_id, '".KEY."')
                FROM additional_data
                WHERE AES_DECRYPT(student_data_id, '".KEY."') = :studentDataId");
        $query->bindParam(":studentDataId", $studentDataId);
        $query->execute();

        //If there is already additional data then update it
        if($query->rowCount() > 0) {
            $query = $db->prepare("UPDATE additional_data
                    SET uid = :uid,
                    placement_date = :placementDate,
                    placement_room = :placementRoom,
                    placement_time = :placementTime,
                    tests_needed = :testsNeeded
                    WHERE AES_DECRYPT(student_data_id, '".KEY."') = :studentDataId");
            $query->bindParam(":uid", $uid);
            $query->bindParam(":studentDataId", $studentDataId);
            $query->bindParam(":placementDate", date("Y-m-d", $testDate));
            $query->bindParam(":placementRoom", $location);
            $query->bindParam(":placementTime", date("H:i:s", $testDate));
            $query->bindParam(":testsNeeded", $testTypes);
            $query->execute();
        }

        //If not insert a new record
        else {
            $query = $db->prepare("INSERT INTO additional_data
            (uid,
            student_data_id,
            placement_date,
            placement_room,
            placement_time,
            tests_needed)
            VALUES
            (:uid,
            AES_ENCRYPT(:studentDataId, '".KEY."'),
            :placementDate,
            :placementRoom,
            :placementTime,
            :testsNeeded)");
            $query->bindParam(":uid", $uid);
            $query->bindParam(":studentDataId", $studentDataId);
            $query->bindParam(":placementDate", date("Y-m-d", $testDate));
            $query->bindParam(":placementRoom", $location);
            $query->bindParam(":placementTime", date("H:i:s", $testDate));
            $query->bindParam(":testsNeeded", $testTypes);
            $query->execute();
        }
    }

    //If the student does not exist in then add it into an untracked attendants table
    else {
        $query = $db->prepare("INSERT INTO untracked_attendants
                VALUES(null,
                :uid,
                AES_ENCRYPT(:fname, '".KEY."'),
                AES_ENCRYPT(:lname, '".KEY."'),
                AES_ENCRYPT(:stuId, '".KEY."'),
                AES_ENCRYPT(:email, '".KEY."'),
                :testDate,
                :location,
                :testTime,
                :retake,
                :testsNeeded)");
        $query->bindParam(":uid", $uid);
        $query->bindParam(":fname", $fname);
        $query->bindParam(":lname", $lname);
        $query->bindParam(":stuId", $stuId);
        $query->bindParam(":email", $email);
        $query->bindParam(":testDate", date("Y-m-d", $testDate));
        $query->bindParam(":location", $location);
        $query->bindParam(":testTime", date("H:i:s", $testDate));
        $query->bindParam(":retake", $retake);
        $query->bindParam(":testsNeeded", $testTypes);
        $query->execute();
    }

    /*************************************************************************
    *************************************************************************/
    $query1 = $db->prepare("SELECT AES_DECRYPT(student_data_id, '".KEY."')
                FROM additional_data
                WHERE AES_DECRYPT(student_data_id, '".KEY."') = :studentDataId
                AND placement_date = :placementDate");
    $query1->bindParam(":studentDataId", $studentDataId);
    $query1->execute();

    $query2 = $db->prepare("SELECT AES_DECRYPT(stu_id, '".KEY."')
                FROM untracked_attendants
                WHERE AES_DECRYPT(stu_id, '".KEY."') = :stuId
                AND test_date = :placementDate");
    $query2->bindParam(":stuId", $stuId);
    $query2->execute();

    // if the test date on the additional_data and untracked_attandants
    // table for the current student is different from the date on the 
    // current submission, increment seats filled
    if($query2->rowCount() == 0){
        //Increment the number of seats filled for the test in question
        $query = $db->prepare("UPDATE test_dates SET seats_filled = seats_filled + 1 WHERE id = :id");
        $query->bindParam(":id", $id);
        $query->execute();

        //Check if the date should be closed off
        $query = $db->prepare("SELECT date, location, seats_max, seats_filled FROM test_dates WHERE id = :id");
        $query->bindParam(":id", $id);
        $query->execute();
        $row = $query->fetch();
        if($row['seats_max'] == $row['seats_filled']) {
            $query = $db->prepare("UPDATE test_dates SET active = 0 WHERE id = :id");
            $query->bindParam(":id", $id);
            $query->execute();

            //Check if there's another room with the same date that should be opened automatically
            //If this is true, activate the room
            $query = $db->prepare("UPDATE test_dates SET active = 1 WHERE id != :id AND date = :date AND seats_max != seats_filled");
            $query->bindParam(":id", $id);
            $query->bindParam(":date", $row['date']);
            $query->execute();
        }
    }
    /*********************************************************************/
    /*************************************************************************/
}
}
编辑:这是我的模式

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";

CREATE TABLE additional_data (
  id int(11) NOT NULL AUTO_INCREMENT,
  uid char(23) NOT NULL,
  student_data_id blob NOT NULL,
  placement_code int(2) NOT NULL,
  sent_wr_to_english tinyint(1) NOT NULL,
  mail_date date NOT NULL,
  tests_needed varchar(20) NOT NULL,
  placement_date date NOT NULL,
  placement_room varchar(30) NOT NULL,
  placement_time time NOT NULL,
  orientation_date date NOT NULL,
  orientation_group varchar(20) NOT NULL,
  confirm_test tinyint(1) NOT NULL,
  attended_test tinyint(1) NOT NULL,
  rsvp_orientation tinyint(1) NOT NULL,
  attended_orientation tinyint(1) NOT NULL,
  tech_prep_complete tinyint(1) NOT NULL,
  student_accounts tinyint(1) NOT NULL,
  it_letters tinyint(1) NOT NULL,
  sent_email_reminder tinyint(1) NOT NULL,
  sent_august_mail tinyint(1) NOT NULL,
  no_schedule tinyint(1) NOT NULL,
  change_test date NOT NULL,
  notes text NOT NULL,
  honors tinyint(1) NOT NULL,
  PRIMARY KEY (id)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1;

CREATE TABLE student_data (
  id int(11) NOT NULL AUTO_INCREMENT,
  student_id blob NOT NULL,
  last_name blob NOT NULL,
  first_name blob NOT NULL,
  address1 blob NOT NULL,
  address2 blob NOT NULL,
  city blob NOT NULL,
  state blob NOT NULL,
  zip blob NOT NULL,
  major blob NOT NULL,
  major2 blob NOT NULL,
  sex blob NOT NULL,
  student_type blob NOT NULL,
  phone blob NOT NULL,
  satr blob NOT NULL,
  satm blob NOT NULL,
  attdent_type blob NOT NULL,
  gpa blob NOT NULL,
  ptma blob NOT NULL,
  ptwr blob NOT NULL,
  ptre blob NOT NULL,
  ptlg blob NOT NULL,
  pths blob NOT NULL,
  waiver blob NOT NULL,
  tbrdepo_term_code blob NOT NULL,
  goremal_email_address blob NOT NULL,
  gobtpac_external_user blob NOT NULL,
  download_date date NOT NULL,
  PRIMARY KEY (id),
  UNIQUE KEY student_id (student_id(16))
) ENGINE=InnoDB  DEFAULT CHARSET=latin1;

CREATE TABLE test_dates (
  id int(11) NOT NULL AUTO_INCREMENT,
  `date` datetime NOT NULL,
  location varchar(30) NOT NULL,
  seats_max tinyint(3) NOT NULL,
  seats_filled tinyint(3) NOT NULL DEFAULT '0',
  MA tinyint(1) NOT NULL,
  RE tinyint(1) NOT NULL,
  WR tinyint(1) NOT NULL,
  LG tinyint(1) NOT NULL,
  active tinyint(1) NOT NULL DEFAULT '1',
  PRIMARY KEY (id)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1;

CREATE TABLE test_types (
  id int(11) NOT NULL AUTO_INCREMENT,
  test_type varchar(10) NOT NULL,
  active tinyint(1) NOT NULL DEFAULT '1',
  PRIMARY KEY (id)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1;

CREATE TABLE untracked_attendants (
  id int(11) NOT NULL AUTO_INCREMENT,
  uid char(23) NOT NULL,
  fname blob NOT NULL,
  lname blob NOT NULL,
  stu_id blob NOT NULL,
  email blob NOT NULL,
  test_date date NOT NULL,
  location varchar(30) NOT NULL,
  test_time time NOT NULL,
  retake tinyint(1) NOT NULL,
  tests_needed varchar(20) NOT NULL,
  PRIMARY KEY (id)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1;

CREATE TABLE uploaded_exports (
  id int(11) NOT NULL AUTO_INCREMENT,
  filename varchar(255) NOT NULL,
  uploaded timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (id)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1;

CREATE TABLE users (
  id int(11) NOT NULL AUTO_INCREMENT,
  username varchar(50) NOT NULL,
  email varchar(100) NOT NULL,
  `password` char(128) NOT NULL,
  PRIMARY KEY (id),
  KEY login (username,`password`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1;

CREATE TABLE writing_test_data (
  id int(11) NOT NULL AUTO_INCREMENT,
  uid char(23) NOT NULL,
  student_id blob NOT NULL,
  fname blob NOT NULL,
  mi blob NOT NULL,
  lname blob NOT NULL,
  email blob NOT NULL,
  PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

好的,您有一个学生id或助理id。在其他数据中,您将这些数据引用到其中一个,并带有其id和可能的类型

如果您现在想在其他数据中输入新记录,只需执行重复检查

如果未经跟踪的乘务员在某处有自己的附加数据,则其工作方式也应相同


测试日期也是一样。我想你应该检查你的数据库/表方案以及你用来建立关系的键。

谢谢你的回答。我只是不确定我是否键入了错误的查询。我想在and子句的某个地方我出了问题。你是说我的查询应该按现在的方式工作吗?也许你是说应该张贴表格模式,表格测试日期中的“座位已填满”不应增加,对吗?在未跟踪的服务员中,只有id是唯一的…是什么使“人”成为唯一的有唯一的?哪些字段组合?学生id对每个人来说都是唯一的。但是他们可能会报名参加多个考试。因此,我不想只在学生id和考试日期与表中的学生提交的内容相同的情况下增加座位数。我想在学生报名时只添加一个复选框上面写着“我想重新安排”或类似的话。只是不确定是否有我没有想到的漏洞会让我的数据丢失。