gpt4 book ai didi

php - 使用 codeigniter 更新记录时出错

转载 作者:行者123 更新时间:2023-11-29 07:31:02 24 4
gpt4 key购买 nike

我正在尝试更新存储在数据库中的记录,但不幸的是,我遇到了类似(如下所示)这样的错误,请任何人帮助我,这对我来说会很有帮助

A PHP Error was encountered

Severity: Notice

Message: Undefined property: stdClass::$id

Filename: views/display_about_us1.php

Line Number: 44

Backtrace:

File: C:\xampp\htdocs\CodeIgniter_try\application\views\display_about_us1.php Line: 44

Function: _error_handler

File: C:\xampp\htdocs\CodeIgniter_try\application\controllers\Home.php Line: 107 Function: view

File: C:\xampp\htdocs\CodeIgniter_try\index.php Line: 315 Function: require_once

Controller

public function updatedata()
{
$id=$this->input->get('id');
$result['data']=$this->Contact_model->displayrecordsById($id);
$this->load->view('update_about_us1',$result);

if($this->input->post('update'))
{
// Basic validation
$this->form_validation->set_rules('first_content', 'First content', 'required');
$this->form_validation->set_rules('second_content', 'Second content', 'required');
$this->form_validation->set_rules('third_content', 'Third content', 'required');
$this->form_validation->set_rules('fourth_content', 'Fourth content', 'required');

if ($this->form_validation->run() == FALSE)
{
$this->load->view('update_about_us1');
}

else
{
$id = $this->input->post('id');
$data = array();
$data['first_content']=$this->input->post('first_content');
$data['second_content']=$this->input->post('second_content');
$data['third_content']=$this->input->post('third_content');
$data['fourth_content']=$this->input->post('fourth_content');
$this->Contact_model->updaterecords($id, $data);
redirect("Home/About_us_display");
}
}
}

型号

function displayrecordsById($id)
{
$query=$this->db->query("select * from about_us1 where id='".$id."'");
return $query->result();
}

function updaterecords($id,$data)
{
$this->db->where('id', $id);
$this->db->update('about_us1', $data);
}

查看

        <!DOCTYPE html>
<html>
<head>
<title>About_us1 page</title>
</head>

<body>
<?php
$i=1;
foreach($data as $row)
{
?>
<form action="Home/updatedata" method="get">
<table width="600" border="1" cellspacing="5" cellpadding="5">
<tr>
<td width="230">First content </td>
<td width="329"><textarea rows="4" cols="50" name="first_content" value="<?php echo $row->first_content; ?>"/></textarea></td>
<span><?php echo form_error("first_content");?></span>
</tr>
<tr>
<td>Second content </td>
<td><textarea rows="4" cols="50" name="second_content" value="<?php echo $row->second_content; ?>"/></textarea></td>
<span><?php echo form_error("second_content");?></span>
</tr>
<tr>
<td>Third content </td>
<td><textarea rows="4" cols="50" name="third_content" value="<?php echo $row->third_content; ?>"/></textarea></td>
<span><?php echo form_error("third_content");?></span>
</tr>
<tr>
<td>Fourth content </td>
<td><textarea rows="4" cols="50" name="fourth_content" value="<?php echo $row->fourth_content; ?>"/></textarea></td>
<span><?php echo form_error("fourth_content");?></span>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" name="update" value="Update Records"/></td>
</tr>
</table>
</form>
<?php } ?>
</body>
</html>




// display record view page



<!DOCTYPE html>
<html lang="en">
<head>
<title>display</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>

<body>
<br><br>
<div class="container">

<div class="panel panel-primary">
<div class="panel-heading text-center">Contact Us Details</div>
</div>

<div class="table-responsive">
<table class="table">
<thead>
<tr>
<th>ID</th>
<th>First Content</th>
<th>Second Content</th>
<th>Third Content</th>
<th>Fourth Content</th>
<th>Update Content</th>
</tr>
</thead>
<tbody>
<tr>
<?php
$i=1;
foreach($data as $row)
{
echo "<tr>";
echo "<td>".$i."</td>";
echo "<td>".$row->first_content."</td>";
echo "<td>".$row->second_content."</td>";
echo "<td>".$row->third_content."</td>";
echo "<td>".$row->fourth_content."</td>";
echo "<td><a href='updatedata?id=".$row->id."'>Update</a></td>";
echo "</tr>";
$i++;
}
?>
</tr>
</tbody>
</table>
</div><br><br>

<a href="<?php echo site_url("Home/backend"); ?>" style="margin-left: 40%"> <button type="button" class="btn btn-success">Back</button></a>

</div>
</body>
</html>

最佳答案

希望这对您有帮助:

参数与带有模型的 Controller 中的updaterecords 方法不匹配

应该是这样的:

像这样在 Controller 中调用 updaterecords 方法:

$this->Contact_model->updaterecords($id, $data);

你的模型方法应该是这样的:

function updaterecords($id,$data,)
{
$this->db->where('id', $id);
$this->db->update('about_us1', $data);
}

更新:

你的 Controller 代码应该是这样的:

if($this->input->post('update'))
{
$data = array();
$id = $this->input->post('id');
$data['first_content']=$this->input->post('first_content');
$data['second_content']=$this->input->post('second_content');
$data['third_content']=$this->input->post('third_content');
$data['fourth_content']=$this->input->post('fourth_content');
$this->Contact_model->updaterecords($id, $data);
redirect("Home/About_us_display");
}

关于php - 使用 codeigniter 更新记录时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51259543/

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