繁体   English   中英

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

[英]PHP MySQL, check for duplicates in two tables

我正在开发一个程序,该程序允许学生报名参加分级测试。 如果在student_data表中有学生ID,则将信息添加到存储测试日期和时间信息的additional_data表中。 如果student不在student_data表中,则会将其添加到存储测试日期和时间信息的untracked_attendants中。

以下是我需要做的:如果学生在additional_datauntracked_attendants和测试日期是一样的新条目,我不想增加而采取的席位。

我已经寻找了几天的答案,并且尝试了不同的查询变体,但均未成功。 以下是我从中获得提示的答案:

$query1如果学生存在于下面的检查additional_data表已经但是我还需要检查放置日期是相同的。 如果是的话,那么我不想增加席位。 这里的AND子句对我不起作用。 如果没有AND ,那就很好。

$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;

好的,所以您有一个Student_id或Attendant_id。 在其他数据中,您可以使用ID和类型将这些数据引用到其中之一。

如果现在要为其他数据输入新记录,则只需执行重复检查。

如果未跟踪的服务员在某处有自己的其他数据,则其工作原理应相同。

测试日期是相同的原则。 我认为您应该检查数据库/表方案以及用于关系的键。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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