Reputation: 121
i'm trying to update my records which are stored in the database, but unfortunately i'm getting errors like (given below) this please can any one help me in this it would be help full for me
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");
}
}
}
Model
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);
}
View
<!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>
Upvotes: 1
Views: 953
Reputation: 9717
Hope this will help you :
Arguments mismatch with updaterecords
method in controller with model
Should be like this :
Call updaterecords
method in your controller like this :
$this->Contact_model->updaterecords($id, $data);
Your model method should be like this :
function updaterecords($id,$data,)
{
$this->db->where('id', $id);
$this->db->update('about_us1', $data);
}
UPDATE :
your controller code should be like this :
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");
}
Upvotes: 1