2017-10-14 115 views
1

我想限制lecID在数据库中出现两次,它将无法将数据插入到数据库中。但是代码有一些问题,即使lecID在我的数据库中出现两次以上,它仍然会插入到数据库中。我能知道什么是问题。以下是我的代码:检查数据库中的值是否存在两次以上

 <?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

您正在检查是否存在超过两次的任何lecId,我认为您必须添加一个条件来检查您要插入的特定lecid。 查询结果应该是 select count(*),lecid from lecid = $ _POST ['lecID'] group by lecid count count(*)> 2 –

+0

您需要检查计数的返回值,而不是整行。 – Shadow

+0

不工作:(@HariramanRadhakrishnan – Lawrence

回答

0

首先,为了解决您的问题,IMO至少应该尝试将代码封装在几个函数中,您可以为每一步调用它。

为学生选课建议业务规则:

  1. 被允许创建两个提案的学生。 学生无法为两个提案(DB规则)选择相同的讲师。
  2. 讲师只能接受两个学生提议(每个提议将是不同的学生)。这里

    发行你的规则有歧义,所以我提出一个假设,该提案表可以有更多的提案超过两个可分配到相同的讲师,但是每一个提案一定是不同的stuID。我假设你在该表中有一个名为'is_proposal_accepted'的列,当讲师接受一个提议时,它被设置为true。

  3. 学生在创建新建议时,如果该讲师已经“接受”了两个建议或者学生选择了以前的建议中的讲师,他就不能选择讲师。

第一步 强制数据库中的业务规则的数据完整性要求不能在代码:

ALTER TABLE `proposal` ADD UNIQUE `unique_index`(`stuID`, `lecID`); 

执行上述使用命令行或工具,数据库作为海蒂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 = new Proposal(); 
    echo json_encode($proposal->doit($_POST)); //This returns your response object 
+0

什么是'DataHasIssues'?它应该是'$ DataHasIssues'吗? – Script47

+0

DataHasIssues只是伪代码(假代码,让开发人员知道该怎么办)....在这种情况下,我正在测试以查看学生输入的所有数据都符合我的规则。如果数据有问题,例如“缺少数据”,我会拒绝它,请将var $ DataHasIssues设置为false。 您听起来很热衷于学习代码,我建议您查阅https://www.freecodecamp.org/进行一些免费培训。 –

+0

如果您是一位新开发人员,但没有多少时间了解详细信息,但希望创建一个运行良好的数据驱动应用程序,我强烈建议您观看此视频。使用这里显示的工具是在Laravel为noob开始的好方法。 :-) https://vimeo.com/154415433 –