搜索结果是数据库中的所有数据,而不是特定数据

时间:2018-01-13 06:57:03

标签: php codeigniter

当我转到搜索框并输入特定的卷号时,它会显示我数据库中的所有数据,而不是我通过卷号输入的数据。我是php和codeigniter的新手。

这是模特:

<?php
  class Stud_Model extends CI_Model {

  function __construct() {
     parent::__construct();
  }

  public function insert($data) {
     if ($this->db->insert("stud", $data)) {
        return true;
     }
  }

  public function delete($roll_no) {
     if ($this->db->delete("stud", "roll_no = ".$roll_no)) {
        return true;
     }
  }

  public function update($data,$old_roll_no) {
     $this->db->set($data);
     $this->db->where("roll_no", $old_roll_no);
     $this->db->update("stud", $data);
  }

  public function get_results($search_term='default')
{
    // Use the Active Record class for safer queries.
    $this->db->select('*');
    $this->db->from('stud');
    $this->db->like('roll_no',$search_term);

    // Execute the query.
    $query = $this->db->get();

    // Return the results.
    return $query->result_array();
}
}
?>

控制器:

<?php
  class Stud_controller extends CI_Controller {

  function __construct() {
     parent::__construct();
     $this->load->helper('url');
     $this->load->helper('form');
     $this->load->database();    //manual connection to the database
  }

  public function index() {
     $query = $this->db->get("stud");
     $data['records'] = $query->result();

     $this->load->helper('form');
     $this->load->view('Stud_view',$data);
  }

  public function add_student_view() {
     $this->load->helper('form');
     $this->load->view('Stud_add');
  }

  public function add_student() {
     $this->load->model('Stud_Model');

     $data = array(
        'roll_no' => $this->input->post('roll_no'),
        'name' => $this->input->post('name'), 
        'surname' => $this->input->post('surname'),
        'mname' => $this->input->post('mname'),
        'mobileno' => $this->input->post('mobileno'),
        'email' => $this->input->post('email'),
        'homeadd' => $this->input->post('homeadd')
     );

     $this->Stud_Model->insert($data);

     $query = $this->db->get("stud");
     $data['records'] = $query->result();
     $this->load->view('Stud_view',$data);
  }

  public function update_student_view() {
     $this->load->helper('form');
     $roll_no = $this->uri->segment('3');
     $query = $this->db->get_where("stud",array("roll_no"=>$roll_no));
     $data['records'] = $query->result();
     $data['old_roll_no'] = $roll_no;
     $this->load->view('Stud_edit',$data);
  }

  public function update_student(){
     $this->load->model('Stud_Model');

     $data = array(
        'roll_no' => $this->input->post('roll_no'),
        'name' => $this->input->post('name'),
        'surname' => $this->input->post('surname'),
        'mname' => $this->input->post('mname'),
        'mobileno' => $this->input->post('mobileno'),
        'email' => $this->input->post('email'),
        'homeadd' => $this->input->post('homeadd')
     );

     $old_roll_no = $this->input->post('old_roll_no');
     $this->Stud_Model->update($data,$old_roll_no);

     $query = $this->db->get("stud");
     $data['records'] = $query->result();
     $this->load->view('Stud_view',$data);
  }

  public function delete_student() {
     $this->load->model('Stud_Model');
     $roll_no = $this->uri->segment('3');
     $this->Stud_Model->delete($roll_no);

     $query = $this->db->get("stud");
     $data['records'] = $query->result();
     $this->load->view('Stud_view',$data);
  }

   public function execute_search()
{
  $this->load->model('Stud_Model');
    // Retrieve the posted search term.
    $search_term = $this->input->post('search');

    // Use a model to retrieve the results.
    $data['records'] = $this->Stud_Model->get_results($search_term);

    // Pass the results to the view.
    $this->load->view('result_view',$data);
}


}
?>

观点:

<!DOCTYPE html>
<html lang = "en">

<head>
  <meta charset = "utf-8">
  <title>Students Example</title>
</head>

<body>
  <a href = "<?php echo base_url(); ?>stud/add_view">Add</a>

  <br>

  <?php
   echo form_open('Stud_controller/execute_search');
   echo form_input(array('roll_no'=>'search'));
   echo form_submit('search_submit','Submit');
   ?>

  </br>
  <br>

  <table border = "1">
     <?php

        $i = 1;
        echo "<tr>";
        echo "<td>Sr#</td>";
        echo "<td>Roll No.</td>";
        echo "<td>First Name</td>";
        echo "<td>Surname</td>";
        echo "<td>Middle Name</td>";
        echo "<td>Mobile/Telephone No.</td>";
        echo "<td>Email Address</td>";
        echo "<td>Home Address</td>";
        echo "<td>Edit</td>";
        echo "<td>Delete</td>";
        echo "<tr>";
        ?>

        <?php foreach($records as $r) {

           echo "<tr>";
           echo "<td>".$i++."</td>";
           echo "<td>".$r->roll_no."</td>";
           echo "<td>".$r->name."</td>";
           echo "<td>".$r->surname."</td>";
           echo "<td>".$r->mname."</td>";
           echo "<td>".$r->mobileno."</td>";
           echo "<td>".$r->email."</td>";
           echo "<td>".$r->homeadd."</td>";
           echo "<td><a href = '".base_url()."stud/edit/".$r->roll_no."'>Edit</a></td>";
           echo "<td><a href = '".base_url()."stud/delete/".$r->roll_no."'>Delete</a></td>";
           echo "<tr>";
        }
     ?>
  </table>

  </body>

  </html>

搜索框的结果视图:

<!DOCTYPE html>
<head>
<table border = "1">

<?php

        echo "<tr>";
        echo "<td>Roll No.</td>";
        echo "<td>First Name</td>";
        echo "<td>Surname</td>";
        echo "<td>Middle Name</td>";
        echo "<td>Mobile/Telephone No.</td>";
        echo "<td>Email Address</td>";
        echo "<td>Home Address</td>";
        echo "<tr>";
        ?>


        <?php foreach($records as $r) {

           echo "<tr>";
           echo "<td>".$r['roll_no']."</td>";
           echo "<td>".$r['name']."</td>";
           echo "<td>".$r['surname']."</td>";
           echo "<td>".$r['mname']."</td>";
           echo "<td>".$r['mobileno']."</td>";
           echo "<td>".$r['email']."</td>";
           echo "<td>".$r['homeadd']."</td>";
           echo "<tr>"; 
        }

    ?>
    </table>

    </head>
    </html>

例如,如果我在搜索框中输入1,它将显示如下所有数据:

Sr# Roll No.    First Name  Surname Middle Name Mobile/Telephone No.    Email Address   Home Address
1   1   Charles Xavier  Howlett 1234567891011   xaviercharles@gmail.com 9912 east street
2   2   Nancy   Hopkins Johnson 09108873512 nancehp@gmail.com   3628 north street

而不是仅显示第一卷。

1 个答案:

答案 0 :(得分:0)

输入搜索表单上的参数不正确,如果您将数组作为参数提供,它将生成name => value属性对。

因此,您可以将搜索表单视图更改为:

...
<?php
echo form_open('Stud_controller/execute_search');
echo form_input(array('name'=>'search'));
echo form_submit('search_submit','Submit');
?>
...