gpt4 book ai didi

php - 无法使用 Codeigniter 更新数据库中的数据

转载 作者:行者123 更新时间:2023-11-30 23:48:46 25 4
gpt4 key购买 nike

我无法在 CodeIgniter 中更新记录中的数据。它不是更新,而是在数据库表中添加新记录。'hotelres_id' 是我的表的主键。

我已经发布了下面的代码:-

更新的 Controller 代码:-

function edit($id){
$this->load->model('hotel_reservation','mb');
$data['message'] = '';
$data['object'] = $this->mb->find_by_id($id);

if($data['object']){
$this->form_validation->set_rules('roomno', 'Room Number', 'required|is_unique[hotel_reservation.roomno]');
$this->form_validation->set_rules('checkin', 'Check In', 'required|is_unique[hotel_reservation.checkin]');
$this->form_validation->set_rules('checkout', 'Check Out', 'required|is_unique[hotel_reservation.checkout]');

if ($this->form_validation->run() == TRUE){
$this->mb->eventreg_id = $_POST['hotelres_id'];
$this->mb->eventreg_id = $_POST['eventreg_id'];
$this->mb->eventhotel_id = $_POST['eventhotel_id'];
$this->mb->roomno = $_POST['roomno'];
$this->mb->checkin = $_POST['checkin'];
$this->mb->checkout = $_POST['checkout'];
$this->mb->comment = $_POST['comment'];
$this->mb->update();
$data['message'] = 'Details updated successfully';
$data['object'] = $this->mb;
}
$this->load->view('core/hotel_reservation/edit',$data);
}
else{
$data['message'] = 'No details available!! Fill It!!';
$this->load->view('core/hotel_reservation/save',$data);
}
}

查看代码:-

<html>
<head>
<title>Hotel Reservation</title>
</head>
<body>
<h2>Hotel Reservation</h2>
<?php if(isset($message)&&$message!='') echo "<span class=\"message\">{$message}</span>"; ?>
<form action="<?php echo site_url('core/hotel_re/edit/'.@$object->hotelres_id); ?>" method="POST" >
<table class="formtable">

<tr><td>hotelres_id</td><td><input type="text" name="hotelres_id" id="hotelres_id" class="textbox" value="<?php echo @$object->hotelres_id; ?>" readonly></td></tr>
<tr><td>eventreg_id</td><td><input type="text" name="eventreg_id" class="textbox" value="<?php echo @$object->eventreg_id; ?>" readonly></td></tr>
<tr><td>eventhotel_id</td><td><input type="text" name="eventhotel_id" class="textbox" value="<?php echo @$object->eventhotel_id; ?>" readonly></td></tr>


<tr><td>Room Number</td><td><input type="text" name="roomno" value="<?php echo @$object->roomno; ?>" class="textbox" ></td></tr>
<tr><td>Check In</td><td><input type="text" name="checkin" value="<?php echo @$object->checkin; ?>" class="textbox"></td></tr>

<tr><td>Check Out</td><td><input type="text" name="checkout" value="<?php echo @$object->checkout; ?>" class="textbox"></td></tr>

<tr><td>Comment</td><td><textarea type="text" name="comment" value="<?php echo @$object->comment; ?>" class="textarea" ></textarea></td></tr>
<tr><td>&nbsp;</td><td><input type="submit" name="submit" value="Update" class="submitbutton"></td></tr>
</table>
</form>
<span class="validation-errors"><?php echo validation_errors(); ?></span>
</body>
</html>

模型代码:-

<?php
class hotel_reservation extends CI_Model{
var $hotelres_id;
var $eventreg_id;
var $eventhotel_id;
var $roomno;
var $checkin;
var $checkout;
var $comment;

static $tablename = 'hotel_reservation';
static $tableid = 'hotelres_id';
function find_by_id($id)
{
$tableid = self::$tableid;
$resultset = $this->db->get_where(self::$tablename,array($tableid=>$id),1);
if($resultset->num_rows()==1)
return array_shift($resultset->result(get_class($this)));
return false;
}

function find_all()
{
$resultset = $this->db->get(self::$tablename);
return $resultset->result(get_class($this));
}

function save()
{
$tableid = self::$tableid;
if(isset($this->$tableid)&&$this->$tableid!=''&&$this->$tableid!=0)
$this->update();
else
$this->insert();
}

private function insert()
{
$this->db->insert(self::$tablename,$this);
}

function update()
{
$tableid = self::$tableid;
$this->db->where($tableid,$this->$tableid);
$this->db->update(self::$tablename,$this);
}

function delete()
{
$tableid = self::$tableid;
$this->db->where($tableid,$this->$tableid);
$this->db->delete(self::$tablename);
}
}

最佳答案

错误就在这里!!

在你的 Controller 中,你已经完成了这个......

        $this->mb->eventreg_id = $_POST['hotelres_id'];
$this->mb->eventreg_id = $_POST['eventreg_id'];

应该是这个

        $this->mb->hotelres_id = $_POST['hotelres_id'];
$this->mb->eventreg_id = $_POST['eventreg_id'];

就像我说的,更新术语应该这样写......

$this->db->where($tableid,$this->hotelres_id);

解决了吗?

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

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