gpt4 book ai didi

php - 使用 codeigniter 更新数据库中的记录

转载 作者:可可西里 更新时间:2023-11-01 07:45:06 24 4
gpt4 key购买 nike

您好,我正在使用 codeIgniter,我设法简单地将 ID 号码和电话号码发布到名为“offers”的表中,这两个字段都是 INT,但是当我尝试更新与特定 ID 对应的电话号码时,我看不到数据库中的更改。我在下面列出了我的 Controller 、模型和 View

newOffer Controller

   <?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;
}
}
?>

更新值的 View

<?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 -->
?>

最佳答案

您没有向此处的模型传递任何数据

$this->offer_model->upddata();

你需要添加类似的东西

$this->offer_model->upddata($this->input->post());

同样在您的模型代码中,$idNum 未定义,您也需要提供它。

例如:

public function upddata($data) {
$idNum = $data['idNum'];
unset($data['idNum']);
$this->db->where('idNum', $idNum);
$this->db->update('offers' ,$data);
return true;
}
//etc

关于php - 使用 codeigniter 更新数据库中的记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18985135/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com