CodeIgniter - 将数据输入到文本框中,并在提交时检查数据库中的匹配记录

时间:2014-10-18 11:09:09

标签: php html mysql codeigniter

我有一个CodeIgniter应用程序,我需要在名为meal_add.php的视图页面中的表单中添加一些信息。这是表格:

 <div id="content" class="box">
                <form action="<?php echo base_url(); ?>index.php/admin_logins/meal2" method="post">
                    <fieldset>
                        <legend id="add_employee_legend">
                            Add Meal Information
                        </legend>
                        <div>
                            <label id= "emp_id_add_label">Employee ID:</label>
                            <input type="text" name="emp_id" id = "employee_id_add" placeholder="Employee ID" required="1"/>
                        </div>
                        <br>
                        <div>
                            <label id= "is_guest_add_label">Guests?</label>
                            <input type="checkbox" name ="is_guest_checkbox" class ="guestcheck" value="1" onchange="valueChanged()"/>
                        </div>
                        <br>
                        <div id = "guestnum" hidden = "true">
                            <label id= "num_of_guest_add_label">No. of Guests:</label>
                            <input type="text" name="num_of_guest" id = "num_of_guest_add" placeholder='0'/>
                        </div>
                        <div>
                            <label id= "remarks_add_label">Remarks:</label>
                            <textarea rows="1" cols="20" style="margin-left: 35px"></textarea>
                        </div>
                        <input type="submit" name="submit" id = "meal_info_submit" value="Save Meal Information"/>
                        <button id = "cancel_button" onclick="location.href='<?php echo base_url(); ?>index.php/admin_logins/meal'">
                            Cancel
                        </button>
                    </fieldset>
                </form>

我的控制器添加方法 -

function meal2()
{
    if($_POST)
    {
        date_default_timezone_set('Asia/Dacca');
        $mdata['emp_id'] = $this->input->post('emp_id');
        $mdata['entry_date'] = date('Y-m-d');
        $mdata['is_guest'] = $this->input->post('is_guest_checkbox');
        $mdata['num_of_guest'] = $this->input->post('num_of_guest');

        $mdata['remarks'] = $this->input->post('remarks');
        $res = $this->meal_model->insert_meal($mdata);
        if($res)
        {   
            $this->session->set_flashdata('message','Meal information added successfully');
            redirect("admin_logins/meal");
        }
    }
    else
    {
        $this->load->view('admin_logins/meal_add');
    }

}

我的模型方法 -

public function insert_meal($data)
{
    return $this->db->insert('meal', $data);    
}

我有一个名为employee的表,结构如下:

    column              data type
      id                 int PK
    emp_id             varchar(15)
   emp_name            varchar(50)
 emp_mobile_no         varchar(15)

我有另一个名为meal的表,结构如下:

    column              data type
      id                 int PK
    emp_id             varchar(15)
   entry_date             date
   is_guest               int
 num_of_guest             int
   remarks                text

我需要做的是:每当我在表单meal_add.php页面的员工ID文本框中输入ID时,系统将确定我输入的id文本是否与employee.emp_id的值匹配。如果找到匹配项,则数据将成功保存在meal表中meal.emp_id,并且将保存的值为相应的employee.id(主键列中的值) employee表)。如果未找到匹配项,将显示一条php flash消息,指出我的meal_add.php视图页面顶部不存在ID。我知道需要一个JOIN查询,但文本匹配部分对我来说似乎很难,我无法弄清楚如何组织我的代码以及放置它们的位置。请帮帮我。

我的数据库是MySQL。

编辑 - 1:

我在我的控制器方法中完成了这个 -

    $temp = $this->input->post('emp_id');
    $sql = "SELECT e.id FROM employee AS e WHERE e.emp_id = ?";
    $mdata['emp_id'] = $this->db->query($sql, $temp)->emp_id;


    if($mdata['emp_id'] == '')
    {
        $this->session->set_flashdata('message','Employee ID does\'nt exist');
        redirect("admin_logins/meal");
    }

无论我在$temp中提供什么价值,我总是收到flash消息并重定向。

我也是这样做的 -

    echo "<pre>";
    print_r($mdata);
    die();

在完成echoprint_r()之后,我得到了这个 -

  A PHP Error was encountered

  Severity: Notice

  Message: Undefined property: CI_DB_mysql_result::$emp_id

  Filename: controllers/admin_logins.php

  Line Number: 150

  Array
  (
      [emp_id] => 
  )

2 个答案:

答案 0 :(得分:1)

我认为你所追求的是$this->db->insert_id()

文档:https://ellislab.com/codeigniter/user-guide/database/helpers.html

另请查看正确的form validation for codeigniter

<强>更新

抱歉,我在看这个问题时误读了。请查看generating query results

$q = $this->db->query($sql, $temp);
if ($q->num_rows() > 0) {
    // having a match means the emp_id is valid
    $row = $q->row();

    // you can now use $row->emp_id to insert to other table
    $ID = $row->emp_id;
} else {
    $this->session->set_flashdata('message','Employee ID does\'nt exist');
    redirect("admin_logins/meal");
}

为什么emp_id餐桌上的varchar?如果它是外键,那么如果类型匹配并且匹配(加入表)时int快得多,则会有所帮助。

答案 1 :(得分:0)

做了一些重大改变:

控制器 -

function meal2()
{
    if($_POST)
    {
        date_default_timezone_set('Asia/Dacca');
        $temp = $this->input->post('emp_id');

        $query = $this->db->query("SELECT id FROM employee WHERE emp_id = '$temp' ")->result_array();

        $mdata['emp_id'] = $query[0]['id'];

        if($mdata['emp_id'] == '')
        {
            $this->session->set_flashdata('message','Employee ID doesn\'t exist');
            redirect("admin_logins/meal");
        }


        $mdata['entry_date'] = date('Y-m-d');
        $mdata['is_guest'] = $this->input->post('is_guest_checkbox');
        $mdata['num_of_guest'] = $this->input->post('num_of_guest');

        $mdata['remarks'] = $this->input->post('remarks');

        $res = $this->meal_model->insert_meal($mdata);
        if($res)
        {   
            $this->session->set_flashdata('message','Meal information added successfully');
            redirect("admin_logins/meal");
        }
    }
    else
    {
        $this->load->view('admin_logins/meal_add');
    }

}

另外在控制器中名为meal()的另一种方法 -

    $data['myQuery'] = $special_query = $this->db->query("SELECT m.id, e.emp_id, m.entry_Date, m.is_guest, m.num_of_guest, m.remarks FROM meal m LEFT JOIN employee e ON e.id = m.emp_id")->result_array();

必须使用 LEFT JOIN (我最喜欢的)来检索相关信息。