正在搜索$ _POST变量

时间:2013-08-06 13:17:06

标签: php codeigniter search http-post

我在html中有这个代码块用于输入搜索。

<?php echo form_open('bookstore/_searchbook'); ?>
<table>
    <tr>
        <td>
    <label for="searchid">Search:</label>
        </td>
        <td>
    <input type="text" size="15" name="searchid" />
        </td>
    </tr>
    <tr>
        <td>
    <label for="searchtype">Type:</label>
        </td>
        <td>
    <select>
        <option value="book_id">Id</option>
        <option value="book_author">Author</option>
        <option value="book_name">Title</option>
    </select>
        </td>
    </tr>
    <tr>
    <input type="submit" value="Search" />
    </tr>
</table>
<?php echo form_close(); ?>

我的控制器上有这个,

public function booksearch()
{
  if($_POST['submit'])
  {
    $col= $this->input->post('searchtype', TRUE);
    $val= $this->input->post('searchval', TRUE);

    $data['book'] = $this->books_model->showbook($col,$val);
    $this->load->view('showbooks',$data);
  }
}

这将是我的模特

public function showbook($col, $searchid)
{
        $this->db->select()->from('books')->where(array($col=>$searchid));
        $query=$this->db->get();
        return $query->first_row('array');
}

关于我的观点的进一步信息,

我有这个来打印搜索结果。

<table cellpadding='5'>
<th>Book ID</th>
<th>Title</th>
<th>Author</th>
<th>Released Year</th>
<th>ISBN</th>

<?php
if(isset($books)) : foreach($books as $book) :  
?>      
        <tr>
        <td><?php echo $book['book_id'] ?></td>
        <td><?php echo $book['book_name'] ?></td>
        <td><?php echo $book['book_author'] ?></td>
        <td><?php echo $book['book_year'] ?></td>
        <td><?php echo $book['book_isbn'] ?></td>
        </tr>
<?php
    endforeach;
?>

<?php
else :
?>
<h5>No records</h5>
<?php
endif;
?>
</table>

它什么也没有返回,所以我看到的都是没有记录。有人指导我做错了。

3 个答案:

答案 0 :(得分:1)

  • 您的表单的操作(查看)为_searchbook,而您的控制器功能为booksearch
  • 您的提交输入需要name属性
  • 在控制器功能中,您在视图中({fore}循环中)$data['book']将该变量引用为$books
  • 型号:你需要选择至少一些东西;例如$this->db->select('*')->from('books')->where(array($col=>$searchid));代替$this->db->select()
  • 型号:我认为您需要return $query->result_array();而不是$query->first_row('array')

答案 1 :(得分:0)

因为您的提交按钮没有任何name属性。而不是if($_POST['submit'])$this->input->post(null)

答案 2 :(得分:-1)

尝试添加全局$ _POST;到你的功能的顶部。在php中,函数中使用(声明)的所有变量都是私有的,除非你通过将它添加到函数中的全局语句来告诉它。

public function booksearch()
{
      global $_POST;

if($_POST['submit'])
    {
    $col= $this->input->post('searchtype', TRUE);
    $val= $this->input->post('searchval', TRUE);

    return $data['book'] = $this->books_model->showbook($col,$val);
    }
}