如果lecID在数据库中出现两次,我想要限制,它将无法用户将数据插入数据库。但是代码有一些问题,即使lecID在我的数据库中出现两次以上,它仍会插入到db中。我能知道问题所在吗?以下是我的代码:
<?php
require ("config1.php");
if(!empty($_POST)){
//UPDATED
$query="SELECT lecID, COUNT(*) FROM proposal GROUP BY
lecID=:lecID HAVING COUNT(*)>2 ";
$query_params= array(
':lecID'=>$_POST['lecID']
);
try {
$stmt = $db->prepare($query);
$result = $stmt->execute($query_params);
}catch (PDOException $ex) {
$response["success"] = 0;
$response["message"] = $ex->getMessage();
die(json_encode($response));
}
$row= $stmt->fetch();
if($row){
$response["success"] = 0;
$response["message"] = "This supervisor has reached maximum students.";
die(json_encode($response));
}
$query="SELECT 1 FROM proposal WHERE stuName= :stuName ";
$query_params= array(
':stuName'=>$_POST['stuName']
);
try {
$stmt = $db->prepare($query);
$result = $stmt->execute($query_params);
}catch (PDOException $ex) {
$response["success"] = 0;
$response["message"] = "Database Error!";
die(json_encode($response));
}
$row= $stmt->fetch();
if($row){
$response["success"] = 0;
$response["message"] = "You have already choose supervisor.";
die(json_encode($response));
}
?>
真的很感激,如果有人可以指出问题。
答案 0 :(得分:0)
首先,为了解决您的问题,IMO至少应该尝试将代码封装在几个可以为每个步骤调用的函数中。
学生课程提案的商业规则:
讲师只能接受两个学生提案(每个提案都是不同的学生)。
问题 此处的规则含糊不清,因此我假设提案表可能有两个以上的提案可以分配给同一个讲师,但每个提案必须是一个不同的stuID。我假设你有一个名为&#39; is_proposal_accepted&#39;在该表中,当讲师接受提案时设置为true。
学生在创建新提案时,如果该讲师已经接受了#34;那么他就不能选择讲师。两个提案或学生在之前的提案中选择了该讲师。
第一步 在数据库中实施业务规则的数据完整性要求,而不是在代码中:
ALTER TABLE `proposal` ADD UNIQUE `unique_index`(`stuID`, `lecID`);
使用命令行或Heidi SQL等工具在数据库中执行上述操作。
第二步封装您的代码(一些非常粗略的伪代码可以帮助您完成结构):
class Proposal
{
private $dataArr = [];
private $response = [];
public function doit($postArrayData)
{
//Clean up data if there are issues reject
if (!$this->clean_up_data($postArrayData)) return $this->get_response();
/**
* Before even trying to do any data inserts check the business rules
* Check if the lecturers
*/
if (!$this->check_if_lecturer_is_open()) return $this->get_response();
if (!$this->check_if_student_has_available_proposals()) return $this->get_response();
/**
*
If you have reached here in the method your Application based Business rules have been achieved
NOW save the reocord, if the data violates the
DB Rules your response will be set to an error,
otherwise it will show a success message
*/
$this->add_proposal();
return $this->get_response();
}
public function clean_up_data($postArrayData)
{
/**
* Apply any validation checks for data quality here then assign to array
EXAMPLE: Set each field parameter like so:
*/
$this->dataArr['stuID'] = $postArrayData['stuID'];
if (DataHasIssues) {
$this->set_response(0, "Crappy Data try again");
return false;
}
return true;
}
//Keep this method private, we want our POST data to be processed first and the in
private function add_proposal()
{
/**
*
*
*
* Your DB calls here.....
*
* $query = "INSERT INTO proposal (lecName,lecID,stuName,stuID,proposalTitle,proposalObjective,proposalDesc)
* VALUES (:proposalID,:lecName,:lecID,:stuName,:stuID,:proposalTitle,:proposalObjective,:proposalDesc)";
*/
$query_params = $this->dataArr;
/**
* Execute query with error control here.....
* if(ExecuteQuery){
* * return true; //Student proposal has been added
*}
* set errors here
*/
$this->set_response(0, DB_errors_msg);
return false;
}
private function set_response($success, $message)
{
$this->response["success"] = $success;
$this->response["message"] = $message;
}
public function get_response()
{
return $this->response;
}
public function check_if_lecturer_is_open()
{
/**
*
* $countOfAcceptedProposals ... Select SQL code here to count if the lecID has had two proposals flagged as accepted
* */
if (CountOfAccepptedProsalsByLecID < 2) {
return true;
}
$this->set_response(0, "This Lecturer is full and is not available");
return false;
}
public function check_if_student_has_available_proposals()
{
//USE the dataArr to get the StuID you want to check
$CountOfProsalsByStuID = returned_count;// select count from where stuID = $this->dataArr['stuID'];
/**
*
* $countOfStudentProposals ... Select SQL code here to count if the lecID has had two proposals flagged as accepted
* */
if ($CountOfProsalsByStuID < 2) {
return true;
}
$this->set_response(0, "Student has reached the maximum number of proposals allowed");
return false;
}
}
现在,您可以致电Proposal课程,让他们努力检查您的业务规则并正确保存您的学生建议。
$proposal = new Proposal();
echo json_encode($proposal->doit($_POST)); //This returns your response object