作为查询结果在数据库中获得0

时间:2013-05-25 15:05:30

标签: php codeigniter

嗨所有新手在这里:)不知道这是否是正确的标题,如果不是抱歉...

我正在尝试将数据从一个表输入到另一个表中,以供正确的用户使用

控制器:

function Admin(){
    $user = $this->input->post('username'); //there is one text field in view page were you input username
    $this->user->getEmail($user);
    if($this->user->getUser($user)){
        $this->user->Input_Admin();
        $this->Success();
    }
    $this->Fail();
}

模型:

function Input_Admin(){
    $k = array(
        'Name' => $this->input->post('username'),
        'Email' => $this->input->post('Email'),
    );  
    $a = $this->db->insert('admin', $k);

    return $a;
}

function getEmail($user){
    $this->db->select('Email');
    $this->db->where('username', $user);
    $q = $this->db->get('users');
    if($q->num_rows > 0){
        return $q->result();
    }
    return FALSE;
}

管理员表ID,姓名,电子邮件中有3个字段...名称的功能就像魅力一样,但电子邮件总是输入0 ....所以我做错了什么?

这里的req是视图:

<form name="info" method="post" action="<?php echo base_url(); ?>index.php/user_controler/admin">
    <table width="500" border="0" cellspacing="1" cellpadding="1" align="center" bgcolor="#FFCC99">
            <tr> 
                <td width="25%">Username:</td>
                <td><input type="text" name="username" size="30"></td>

            </tr>
            <tr> 
                <td colspan="6"> 
                    <div align="center"> 
                        <input type="submit" name="Confirm" value="Confirm">
                        <input type="reset" name="Cancel" value="CANCEL">
                    </div>
                </td>
            </tr>
    </table>
    </form>

1 个答案:

答案 0 :(得分:0)

哪里是<input type="text" name="Email" size="30">

你没有在html中,所以你得到0值

将其放在html中,因此发布请求也会向您的控制器发送电子邮件;)

只是为了说清楚:

$k = array(
        'Name' => $this->input->post('username'), //needs <input name="username"/> in html
        'Email' => $this->input->post('Email'), //needs <input name="Email"/> in html
    );  

另外一个提示使用此:

<?php echo site_url('user_controller/admin'); ?>

而不是

<?php echo base_url(); ?>index.php/user_controler/admin

更舒适,易读;)

如果您想要来自db:

的用户电子邮件,则可以尝试此操作
function Admin(){
    $user = $this->input->post('username'); //there is one text field in view page were you input username
    $results = $this->user->getEmail($user);
    if($this->user->getUser($user)){
        $this->user->Input_Admin($results);
        $this->Success();
    }
    $this->Fail();
}
function Input_Admin($data){
    $k = array(
        'Name' => $this->input->post('username'),
        'Email' => $data->email,
    );  
    $a = $this->db->insert('admin', $k);

    return $a;
}

function getEmail($user){
    $this->db->select('Email');
    $this->db->where('username', $user);
    $q = $this->db->get('users');
    if($q->num_rows > 0){
        return $q->row();
    }
    return FALSE;
}