我正在使用CodeIgniter併爲每個字段獲取未定義的變量錯誤,我試圖插入到數據庫中。有什麼我失蹤? E:哦,是的,最重要的是,它將在提交表單後在數據庫的所有字段中插入「NULL」。插入CodeIgniter和未定義的變量
這是我的觀點:
<?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 (04)" 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('[email protected]', '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');
}
}
?>