您好我在使用codeIgniter并且我设法简单地将ID号码和电话号码发布到名为“offers”的表中,两个字段都是INT,但是当我尝试更新对应于特定ID的电话号码时,我看到没有数据库中的更改。我在下面列出了我的控制器,型号和视图
newOffer控制器
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
//insert data into db using offer_model model
// other option update submit
class newOffer extends CI_Controller {
function addOffer() {
//if the form is submitted
$this->load->view("check");
$this->load->model("offer_model");
if ($this->input->post('mysubmit')) {
$this->offer_model->entry_insert();
}
}
function updateOffer (){
$this->load->view("check2");
$this->load->model("offer_model");
if ($this->input->post('mysubmit')) {
$this->offer_model->upddata();
}
}
}
?>
offer_model
class offer_model extends CI_Model{
public function entry_insert(){
$data = array(
'idNum' => $this->input->post('idNum'),
'phneNum' => $this->input->post('phneNum'),
);
$this->db->insert('offers',$data);
}
public function upddata($data) {
$this->db->where('idNum', $idNum);
$this->db->update('data' ,$data);
//extract($data);
//$data['OfferName']
//$this->db->where('OfferName' , $data['OfferName']);
//$this->db->update($Offers, array('OfferName' => $OfferName));
return true;
}
}
?>
更新值的视图
<?form _open(base_url()."index.php/newOffer/updateOffer")?>
<div class="form">
// <?php echo form_open('newOffer/addOffer'); ?>
<legend>Please enter details for your new offer</legend>
<label for="ID Number">ID Number: <span class="required">*</span></label>
<input type="text" name="idNum" id="idNum" placeholder="Please enter ID Number/>
<label for="phone Number">Phone Number:</label>
<input type="text" name="phneNum" id="phneNum " placeholder="Please enter phone Number"/>
<fieldset class="submit_field">
<?php echo form_submit('mysubmit', 'Submit Form'); ?>
</fieldset>
</div><!-- end of form div -->
?>
答案 0 :(得分:1)
您没有在此处向模型传递任何数据
$this->offer_model->upddata();
您需要添加类似
的内容$this->offer_model->upddata($this->input->post());
同样在您的模型代码中$idNum
未定义,您也需要提供。
e.g:
public function upddata($data) {
$idNum = $data['idNum'];
unset($data['idNum']);
$this->db->where('idNum', $idNum);
$this->db->update('offers' ,$data);
return true;
}
//etc
答案 1 :(得分:0)
确保您从插入或更新数据的同一个表中进行竞争。
public function upload($id_akun, $data)
{
$table = "foto";
$this->db->where("id_akun", $data['id_akun']);
$count = $this->db->count_all_results($table);
if ($count < 1) {
$this->db->insert($table, $data);
} else {
$this->db->where('id_akun', $data['id_akun']);
$this->db->update($table, $data);
}
}
祝你好运:)
答案 2 :(得分:0)
使用构造在控制器中加载模型并始终将后期数据传递给模型函数,然后只有表格将数据发布到模型函数中。
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
function __construct()
{
parent::__construct();
$this->load->model("offer_model");
}
class newOffer extends CI_Controller {
function addOffer() {
//if the form is submitted
$this->load->view("check");
$this->load->model("offer_model");
if ($this->input->post('mysubmit')) {
$CI_Post = $$this->input->post();
$this->offer_model->entry_insert($CI_Post);
}
}
function updateOffer (){
$this->load->view("check2");
if ($this->input->post('mysubmit')) {
$CI_Post = $$this->input->post();
$this->offer_model->upddata($CI_Post);
}
}
}
?>
这是在数组中获取表单数据的模型。
<?php
class offer_model extends CI_Model{
public function entry_insert($param=""){
$data = array(
'idNum' => $param['idNum'],
'phneNum' => $param['phneNum'],
);
$this->db->insert('offers',$data);
return $this->db->insert_id();
}
public function upddata($param="") {
$data = array(
'idNum' => $param['idNum'],
'phneNum' => $param['phneNum'],
);
$this->db->where('idNum', $idNum);
$this->db->update('data' ,$data);
return $this->db->affected_rows();
}
}
?>
答案 3 :(得分:-1)
执行类似
的操作public function upddata() {
$data=array();
$data=$this->input->post(); //get all post value to data array
$idNum=$data['idNum'];
unset($data['idNum']); // unset unnecessary values
$this->db->where('idNum', $idNum)->update('offers' ,$data);
return true;
}
这是程序。如果您有100个输入字段,则无法将每个值添加到数组中。如果您遇到任何问题,请告诉我。
<强>更新强>
更改
<?php echo form_open('newOffer/addOffer'); ?>
到
<?php echo form_open('newOffer/updateOffer'); ?>
在视图中更新值