0

Hi all newbie here :) Don't know if this is correct title, so sorry if not...

I'm trying to input data from one table to another for right user

controller:

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();
}

model:

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;
}

There is 3 fields in admin table id, name, email... the function for name is working like a charm but for email always input 0.... so what i'm doing wrong?

As req here is view:

<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>
4

1 回答 1

0

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

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

把它放在 html 中,所以 post 请求也会向你的控制器发送电子邮件;)

只是为了说清楚:

$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;
}
于 2013-05-25T16:29:32.993 回答