【发布时间】:2015-03-07 00:14:08
【问题描述】:
我正在开发一个允许学生报名参加分班考试的计划。如果我们在student_data 表中有学生ID,我们将信息添加到存储测试日期和时间信息的additional_data 表中。如果学生不在student_data 表中,他们将被添加到存储测试日期和时间信息的untracked_attendants。
这是我需要做的: 如果学生在additional_data 或untracked_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;
【问题讨论】: