【问题标题】:PHP using get_where clause in Codeigniter not passing dataPHP在Codeigniter中使用get_where子句不传递数据
【发布时间】:2013-02-14 14:23:35
【问题描述】:

大家好,我是 Codeigniter 的新手,我正在尝试获取与从前一页传递的 account_id 相关的所有数据。

我传递了 account_id,但没有传递与 account_id 关联的名称字段。名称字段为空白。

我收到一个错误:

这是我的控制器代码:

function input($account_id = '', $name = ''){
   if((int)$account_id > 0){
      $query = $this->db->select('name', $name);
      $query = $this->db->get_where('db_accounts', array('account_id' => $account_id));

      $data['account'] = $query;
      $data['fname']['value']   = $name;
      $data['faccount_id']['value'] = $account_id;
      $data['name']        = '';
      $data['account_id']  = '';
  }

  $this->load->view('manage/input',$data);      
}

这是我的输入视图表单:

<?php

   $data = array(
          'name'  => $fname['value'],
          'account_id' => $faccount_id['value']
        );

echo '<form action="/manage/edit" method="post" accept-charset="utf-8">';
echo form_hidden($data);

echo $account_id .' Account ID'.
    form_input($faccount_id); 
echo $name .' Name'.
    form_input($fname); 

$data = array('name' => 'submit', 'value' => 'Update Account', 'class' => 'submit');
echo form_submit($data);

  ?>
 <?php echo form_close(); ?>

【问题讨论】:

  • 你得到哪个错误?
  • 我没有收到错误消息。在表单中,变量“name”为空,但“account_id”正在传递值。

标签: php codeigniter activerecord where-clause


【解决方案1】:

我相信 get_where 只是准备好您的查询

$query-&gt;row_array() 应该将结果作为数组返回

$query = $this->db->get_where('db_accounts', array('account_id' => $account_id));
$result = $query->row_array();

对于您问题的第二部分,看起来有很多事情要做。 $name 在您的 input 函数中的值是多少?您实际上是否将值传递给input?确保在 input 函数中设置了该名称,否则它将只是一个空字符串。

【讨论】:

  • +1,您似乎缺少运行该查询的上述代码。
  • 我如何将 account_id 传递到下一页而不是其他字段?
  • 因为您已经拥有帐户 ID - 已传递到函数中。我编辑了上面的答案并带有注释以突出显示。
  • $name 设置在输入函数 function input($account_id = '', $name = '') 见上面我的控制器信息。
【解决方案2】:

来自文档:

$this->db->select() 接受可选的第二个参数。如果你设置 它为 FALSE,CodeIgniter 不会尝试保护您的字段或表 带有反引号的名称。如果您需要复合选择,这很有用 声明。

所以替换

$query = $this->db->select('name', $name);

$this->db->select('name', $name); // No need to assign it to a variable

然后$this-&gt;db-&gt;get_where(); 执行查询并返回您需要从中获取结果的整个查询对象。

$query = $this->db->get_where('db_accounts', array('account_id' => $account_id));
$result = $query->row_array(); //For single row
$result = $query->result_array(); //For more than one row

【讨论】:

    【解决方案3】:

    在 djjjuk 的帮助下找到了解决方案。

     $query = $this->db->get_where('db_accounts', array('account_id' => $account_id));
       if ($query->num_rows() > 0)
       {
         $row = $query->row_array();
    
       } 
    
      $data['account'] = $row;
    
      $data['fname']['value']   = $row['name'];
      $data['faccount_id']['value'] = $account_id;
      $data['name']        = '';
      $data['account_id']  = '';
    }
    
      $this->load->view('manage/input',$data);      
    
     }
    

    【讨论】:

      猜你喜欢
      • 2019-09-02
      • 1970-01-01
      • 1970-01-01
      • 2023-03-05
      • 2015-07-07
      • 2016-10-22
      • 2014-07-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多