我不明白如何在MySQL表格中准确插入一行。如果我使用我的代码,我会为每个帖子添加一个新行。而不是那样,我只想要一行包含所有值。
以下是代码示例。
HTML:
echo form_open('account/update');
echo "<p><label for='gender'>Gender</label>
<input type='text' name='gender' id='gender' value='".$gender."' />
</p>
<p>
<label for='age'>Age</label>
<input type='text' name='age' id='age' value='".$age."' />
</p>
<p>
<label for='bio'>Biografie</label>
<input type='text' name='bio' id='bio' value='".$bio."' />
</p>
<p>
<label for='skills'>Skills</label>
<input type='text' name='skills' id='skills' value='".$skills."' />
</p>
<p><input type='submit' value='Save' /></p>";
echo form_close();
控制器:
function update()
{
$userid = $this->session->userdata("userid");
$datanew = array(
'userid' => $this->session->userdata("userid"),
'gender' => $this->input->post('gender'),
'age' => $this->input->post('age'),
'bio' => $this->input->post('bio') ,
'skills' => $this->input->post('skills')
);
$this->session->set_userdata($data);
$this->load->model('model_account');
$this->load->model("model_user");
$this->model_account->profile_insert($datanew);
$this->load->view("change_avatar");
redirect("account/show/".$this->session->userdata("userid"));
}
型号:
function profile_insert($datanew)
{
$this->db->insert('profile', $datanew);
}
如果我提交HTML表单,我会获得5行。
答案 0 :(得分:1)
这对我有用: 我将userid设置为unique,并在插入和更新函数之间的控制器ti开关中创建一个if语句。
控制器:
function update()
{
$datanew = array(
'userid' => $this->session->userdata("userid"),
'gender' => $this->input->post('gender'),
'age' => $this->input->post('age'),
'bio' => $this->input->post('bio') ,
'skills' => $this->input->post('skills')
);
$exists = $this->db->select('profile')->where('userid', $this->session->userdata("userid"));
if($exists)
{
$this->session->set_userdata($datanew);
$this->load->model('model_account');
$this->load->model("model_user");
$this->model_account->profile_update($datanew);
$this->load->view("change_avatar");
redirect("account/show/".$this->session->userdata("userid"));
}
else
{
$this->session->set_userdata($datanew);
$this->load->model('model_account');
$this->load->model("model_user");
$this->model_account->profile_insert($datanew);
$this->load->view("change_avatar");
redirect("account/show/".$this->session->userdata("userid"));
}
}
模型:
function profile_insert($datanew)
{
$this->db->insert('profile', $datanew);
}
function profile_update($datanew)
{
$this->db->update('profile', $datanew);
}
感谢您的帮助