codeigniter mysql停止第三次连续插入

时间:2014-03-05 15:16:11

标签: php mysql codeigniter

我有这个codeigniter mysql代码来创建约会:

if ($this->form_validation->run() == TRUE)
{
    $data = array(
        'startTime' => $startTime ,
        'endTime' => $endTime ,
        'day' => $date ,
        'contact_id' =>  $this->input->post('InputPatient') 
    );

    $this->db->insert('rdv', $data);
    echo 'Appointment Created Successfully.';
}

我需要的是 - 在特定时间如晚上8点,我不应该创建超过2个约会。在具有相同开始时间的第三个插入时,我应该给出错误。

感谢。

2 个答案:

答案 0 :(得分:1)

在执行insert之前,您可以执行select查看具有相同开始时间的条目数。如果少于2,那么您可以继续进行插入。

$this->db->select('count(*) as count')
    ->from('rdv')
    ->where('startTime', $startTime)
    ->where('day', $date);
$query = $this->db->get();

$aResult = $query->row_array();

if($aResult['count'] < 2)
{
    //Do your insert
}

答案 1 :(得分:0)

你走了。在插入之前,请检查特定时间和日期是否已有2个约会。

if ($this->form_validation->run() == TRUE)
{
  $data = array(
     'startTime' => $startTime ,
     'endTime' => $endTime ,
     'day' => $date ,
     'contact_id' =>  $this->input->post('InputPatient') 
  );

  $isAlreadyTwo = $this->db->from("rdv")
                           ->where(array("startTime" => $startTime, "day" => $day))
                           ->count_all_results() >= 2;

  if($isAlreadyTwo)
  {
    echo "Error!!";
  }
  else
  {
    $this->db->insert('rdv', $data);
    echo 'Appointment Created Successfully.';
  }
}

希望这会有所帮助:)

相关问题