我正在更新记录,但它只在数据库中插入Null值,它没有取我插入表格的值。请检查下面的代码。我是codeigniter的新手。请帮助我。
控制器
public function update1()
{
$id = $this->uri->segment(3);
$this->load->model("model_add_user");
$data['posts'] = $this->model_add_user->update1($id);
$data['user'] = $this->model_add_user->select();
$this->load->view('index',$data);
}
模型
public function update1($userId)
{
$data = array(
'date'=>$this->input->post('date'),
'firtsname'=>$this->input->post('firtsname'),
'middlename'=>$this->input->post('middlename'),
'lastname'=>$this->input->post('lastname'),
'mobileno'=>$this->input->post('mobileno'),
'landline'=>$this->input->post('landline'),
'address'=>$this->input->post('address'),
'city'=>$this->input->post('city'),
'locality'=>$this->input->post('locality'),
'email'=>$this->input->post('email'),
);
$this->db->where('userId',$userId);
$this->db->update('add_user',$data);
}
查看
<?php
foreach ($posts as $post)
{
?>
<input type="text" name="userId" placeholder="USER ID" style="width:500px" value='<?php echo $post->userId;?>'>
<input type="date" name="date" style="width:500px" value='<?php echo $post->date;?>'>
<input type="text" name="firtsname" placeholder="FIRST NAME" style="width:330px" value='<?php echo $post->firtsname;?>'>
<input type="text" name="middlename" placeholder="MIDDLE NAME" style="width:330px" value='<?php echo $post->middlename;?>'>
<input type="text" name="lastname" placeholder="LAST NAME" style="width:330px" value='<?php echo $post->lastname;?>'>
<input type="text" name="mobileno" placeholder="ENTER MOBILE NO." style="width:500px" value='<?php echo $post->mobileno;?>'>
<input type="text" name="landline" placeholder="LANDLINE No." style="width:500px" value='<?php echo $post->landline;?>'>
<textarea name="address" placeholder="ENTER ADDRESS" ><?php echo $post->address;?></textarea>
<input type="text" name="city" placeholder="CITY" style="width:500px" value='<?php echo $post->city;?>'>
<input type="text" name="locality" placeholder="LOCALITY" style="width:500px" value='<?php echo $post->locality;?>'>
<input type="text" name="email" placeholder="ENTER EMAIL" style="width:1010px" value='<?php echo $post->email;?>'></br>
<a href="<?php echo site_url('Welcome/update1/'.$post->userId)?>"><input type="submit" class="submit" name="UPDATE" value="UPDATE" ></a>
<a href="<?php echo base_url('')?>" role="button" ><input type="submit" class="submit" name="UPDATE" value="Cancel" ></a>
<?php }
?>
答案 0 :(得分:1)
您好,我可以提供: 视图:
<?php
foreach ($posts as $post)
{
?>
<?php echo form_Open('welcome/update_process');?>
<input type="text" name="userId" placeholder="USER ID" style="width:50px" value='<?php echo $post->userId;?>'>
<input type="date" name="date" style="width:500px" value='<?php echo $post->date;?>'>
<input type="text" name="firtsname" placeholder="FIRST NAME" style="width:330px" value='<?php echo $post->firtsname;?>'>
<input type="text" name="middlename" placeholder="MIDDLE NAME" style="width:330px" value='<?php echo $post->middlename;?>'>
<input type="text" name="lastname" placeholder="LAST NAME" style="width:330px" value='<?php echo $post->lastname;?>'>
<input type="text" name="mobileno" placeholder="ENTER MOBILE NO." style="width:500px" value='<?php echo $post->mobileno;?>'>
<input type="text" name="landline" placeholder="LANDLINE No." style="width:500px" value='<?php echo $post->landline;?>'>
<textarea name="address" placeholder="ENTER ADDRESS" ><?php echo $post->address;?></textarea>
<input type="text" name="city" placeholder="CITY" style="width:500px" value='<?php echo $post->city;?>'>
<input type="text" name="locality" placeholder="LOCALITY" style="width:500px" value='<?php echo $post->locality;?>'>
<input type="text" name="email" placeholder="ENTER EMAIL" style="width:1010px" value='<?php echo $post->email;?>'></br>
<input type="submit" class="submit" name="UPDATE" value="UPDATE" >
<a href="<?php echo base_url('')?>" role="button" class="submit">Cancel</a>
<?php echo form_close();?>
<?php }
?>
<?php endif ?>
控制器:
public function update_process()
{
$this->load->model("model_add_user");
$id = $this->input->post('userId');
$data = array(
'date'=>$this->input->post('date'),
'firtsname'=>$this->input->post('firtsname'),
'middlename'=>$this->input->post('middlename'),
'lastname'=>$this->input->post('lastname'),
'mobileno'=>$this->input->post('mobileno'),
'landline'=>$this->input->post('landline'),
'address'=>$this->input->post('address'),
'city'=>$this->input->post('city'),
'locality'=>$this->input->post('locality'),
'email'=>$this->input->post('email'),
);
$result = $this->model_add_user->update_data($id,$data);
if ($result == "true") {
redirect('welcome/update_retrive/'.$id);
} else {
echo "cannot update";
}
}
型号:
function update_data($id,$data)
{
$query = $this->db->where('userId',$id)
->update('add_user',$data);
if ($query) {
return "true";
} else {
return "false";
}
}