值不使用php codeigniter插入数据库

时间:2015-12-18 06:08:45

标签: php mysql database forms codeigniter

我试图通过php CodeIgniter将数据插入数据库,并在提交按钮期间在同一页面中显示结果。以下是我的代码。
问题是我收到错误消息,因为重复输入' 104'关键' PRIMARY' 。请帮忙。
查看页面

<div class="container col-lg-4">
<?php echo validation_errors(); ?> 
<form action="<?php echo base_url();?>index.php/Welcome/gallery1" method="post">
<div class="form-group has-info">
<?php
foreach ($h->result_array() as $value){      
?>
<input type="hidden" name="id" value="<?php echo $value['offer_id'];?>" >
<?php }?>
<br> 
<label class="control-label col-lg-6" for="inputSuccess">Offer title</label>
<input type="text" class="form-control col-lg-6" name="offered" id="offered" value="<?php if(isset($_POST['offered'])) echo $_POST['offered'];  ?>">
<label class="control-label col-lg-6" for="inputSuccess">Offer Description</label>
<textarea id="description" name="description" class="form-control col-lg-6"  rows="3" value="<?php if(isset($_POST['description'])) echo $_POST['description'];  ?>" ></textarea>
<br/>
<div>
<button type="submit" class="btn btn-primary col-lg-4">    <span>SUBMIT</span>
</button>
</div>
</div>
</form>

控制器页面

public function gallery1()
{
$this->load->helper('url');
$this->load->helper('form');
$this->load->library('form_validation');
$this->load->model('Login_set');
$this->form_validation->set_rules('id','id');
$this->form_validation->set_rules('offered','offered','required');
$this->form_validation->set_rules('description','description','required');
$page_id =$this->uri->segment(3);
$data['h']=$this->Login_set->select();
$this->load->view('App_stay/pages/hotel1_galery.php',$data); 
if($this->form_validation->run() == FALSE)
{   
}
else
{
$data['query']=$this->Login_set->add_offer();
if($data['query']!=NULL)
{
$data['ins_msg']='data inserted';               
}
else
{
$data['ins_msg']='data not inserted';
}
}
}

模型页面

public function add_offer()
{ 
$this->form_validation->set_message('offered','offered','required');
$this->form_validation->set_message('description','description','required');
$this->load->database();
$this->load->helper('url');
$data=array
(
'offer_id'=>$this->input->post('id'),
'hotel_id'=>1,
'offers_name'=>$this->input->post('offered'),
'offers_description'=>$this->input->post('description')
);
$this->db->insert('offer_hotel1',$data);
}    

0 个答案:

没有答案