我正在使用CodeIgniter并为我尝试插入数据库的每个字段获取未定义的变量错误。有什么我想念的吗?
E:哦,是的,最重要的是,在提交表单后,它会在数据库的所有字段中插入“NULL”。
以下是我的观点:
<?php $this->load->helper('form'); ?>
<?php $attributes = array(
'id' => 'add_user_form'
);
?>
<?php echo form_open('users/add_user', $attributes); ?>
<span class="close_form_button_add_user_form">X</span>
<div class="form_row">
<input type="text" name="f_name" placeholder="Etunimi" class="half-width" />
<input type="text" name="l_name" placeholder="Sukunimi" class="half-width" style="float: right; margin-right: 24px;"/>
</div>
<div class="form_row">
<input type="email" name="email" placeholder="Uuden käyttäjän sähköpostiosoite" class="user full-width" />
</div>
<div class="form_row">
<input type="email" name="email_confirm" placeholder="Sähköpostiosoite uudelleen" class="user full-width" />
</div>
<div class="form_row">
<input type="text" name="phone_number" placeholder="Puhelinnumero (0401234567)" class="full-width" />
</div>
<div class="form_row" style="margin-top: 20px;">
<select name="user_type" class="add_user_select" id="select_user_type">
<option value="">Käyttäjätyyppi</option>
<option value="admin">Hallinto</option>
<option value="moderator">Hallinto 2</option>
<option value="enduser">Pääkäyttäjä</option>
</select>
<input type="submit" name="add_user" value="Luo käyttäjä" class="login_submit" id="submit_user" style="margin: 5px 24px 0px 0px;"/>
</div>
<div class="form_row" id="add_user_optional_information">
<select name="school" class="add_user_select half-width">
<option value="">Koulutalo</option>
<?php foreach($schools as $school) :?>
<option value="<?=$school['id']?>"><?=$school['name']?></option>
<?php endforeach; ?>
</select>
<select name="subject" class="add_user_select half-width">
<option value="">Laji</option>
<?php foreach($subjects as $subject) :?>
<option value="<?=$subject['id']?>"><?=$subject['subject_name']?></option>
<?php endforeach; ?>
</select>
</div>
<?php echo form_close(); ?>
<?php echo validation_errors(); ?>
</div>
这是我将用户添加到数据库(称为用户)的模型:
<?php
class User extends CI_Model {
public function add_user() {
$data = array(
'username' => $username,
'name' => $name,
'email' => $email,
'password' => $password,
'user_type' => $user_type,
'phonenumber' => $phonenumber,
'school_id' => $school_id,
'subject_id' => $subject_id
);
$this->db->insert('users', $data);
}
}
?>
我的控制器处理实际插入:(忽略评论)
<?php
class Users extends CI_Controller {
public function add_user()
{
if($this->input->post('add_user'))
{
$this->load->library('form_validation');
$this->form_validation->set_rules('f_name', 'Etunimi', 'required|min_length[2]');
$this->form_validation->set_rules('l_name', 'Sukunimi','required|min_length[3]');
$this->form_validation->set_rules('email', 'Sähköpostiosoite','required|matches[email_confirm]|is_unique[users.email]|valid_email');
$this->form_validation->set_rules('email_confirm', 'Sähköpostiosoite', 'required|valid_email');
$this->form_validation->set_rules('phone_number', 'Puhelinnumero', 'min_length[7]|max_length[10]');
if($this->input->post('user_type') == "moderator")
{
$this->form_validation->set_rules('school', 'Koulutalo', 'required');
$this->form_validation->set_rules('subject', 'Laji', 'required');
}
if($this->form_validation->run() == FALSE)
{
echo "Not working!!";
}
$first_part_username = substr($this->input->post('f_name'), 0, 2);
$second_part_username = substr($this->input->post('l_name'), 0, 3);
$random_number = random_string('numeric', 4);
$username = $first_part_username . $second_part_username . $random_number;
$password = random_string('alnum', 8);
$this->load->model('user');
$data = array (
'name' => strtoupper($this->input->post('f_name')). " ".strtoupper($this->input->post('lname')),
'email' => $this->input->post('email'),
'username' => $username,
'password' => $this->phpass->hash($password),
'user_type' => $this->input->post('user_type'),
'phone_number' => $this->input->post('phone_number'),
'school_id' => $this->input->post('school_id'),
'subject_id' => $this->input->post('subject_id')
);
if($this->user->add_user($data))
{
$this->load->library('email');
$config['newline'] = '\r\n';
$this->email->initialize($config);
$this->email->from('your@example.com', 'Your Name');
$this->email->to($this->input->post('email'));
$this->email->subject('Käyttäjä rekisteröity');
$this->email->message(
"Hei,"
.strtoupper($this->input->post('f_name')). " ".strtoupper($this->input->post('l_name')).".
Tämä on automaattinen viesti kilpailuvastaavan toimesta. \n
Olemme luoneet sinulle palveluumme käyttäjätunnuksen ja salasanan, jonka avulla pääset käsiksi
infrastruktuurijärjestelmään. \n
Pääset kirjautumaan järjestelmään seuraavilla tunnuksilla:"
);
if($this->email->send()) {
base_url().'dashboard';
}
}
}
$this->load->view('header');
$this->load->view('dashboard', $data);
$this->load->view('footer');
}
}
?>
答案 0 :(得分:1)
您正在将$data
从Controller
传递到Model
,但您尚未在parameter
中定义任何Model
。将模型功能更改为 -
public function add_user($data) {
$data = array(
'username' => $data['username'],
'name' => $data['name'],
'email' => $data['email'],
'password' => $data['password'],
'user_type' => $data['user_type'],
'phonenumber' => $data['phone_number'],
'school_id' => $data['school_id'],
'subject_id' => $data['subject_id']
);
$this->db->insert('users', $data);
}
答案 1 :(得分:0)
您正在调用$ this-&gt; user-&gt; add_user($ data)但您的模型函数定义是公共函数add_user(),它应该是公共函数add_user($ data)