php - Codeigniter update method not updating MYSQL records

I'm just learning how to program in codeigniter,I'm following a tutorial, my problem is that i want to update a MYSQL database with a method that calls a model and the model updates the database. But it doesn't i've tried different approaches to the model i can't seem to identify where the problem is:
1.i tried to code the model this way:
`public function updateRecords($id, $name, $email, $password, $mobile)
{
$query=$this->db->query("update users SET
name='$name',email='$email',mobile='$mobile' where user_id='".$id."'");
}`
but got this error: Call to a member function result() on boolean in CodeIgniter, SQL
2.i tried this way:
public function updateRecords($id, $name, $email, $password, $mobile)
{
$data = array(
'id' => $id,
'name' => $name,
'email' => $email,
'passwords' => $password,
'mobile' => $mobile
);
$this->db->where('id', $id);
$this->db->update('Users', $data, array('id' => $id));
}
this above works but does not update records.
Here are the methods model and view of my current setup:
My current Controller:
public function updateData()
{
$this->load->model('Database');
$id = $this->input->get('id');
$result['data'] = $this->Database->displayRecordsById($id);
$this->load->view('pages/edit', $result);
if ($this->input->post('update')) {
$name = $this->input->post('name');
$email = $this->input->post('email');
$password = $this->input->post('password');
$mobile = $this->input->post('mobile');
$this->Database->updateRecords($id, $name, $email, $password, $mobile);
redirect('page/displaydata');
}
}
My current Model:
public function updateRecords($id, $name, $email, $password, $mobile)
{
$data = array(
'id' => $id,
'name' => $name,
'email' => $email,
'passwords' => $password,
'mobile' => $mobile
);
$this->db->where('id', $id);
$this->db->update('Users', $data, array('id' => $id));
enter code here
}
public function displayRecordsById($id)
{
$query = $this->db->query("SELECT * FROM Users WHERE id='" . $id . "'");
return $query->result();
}
My current View:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<style>
* {
font-family: Roboto, sans-serif;
}
th {
width: 200px;
background-color: darkslateblue;
color: snow;
border-collapse: separate;
margin: 0;
}
td {
text-align: left;
border-collapse: separate;
}
</style>
<h3>Update User Information</h3>
<?php echo form_open(); ?>
<?php
$i = 1;
foreach ($data as $row) {
echo form_input($data = '', $id = $row->id) . '<br/>';
echo form_label('Name', 'user name') . '<br/>';
echo form_input($data = '', $name = $row->name) . '<br/>';
echo form_label('Email', 'email') . '<br/>';
echo form_input($data = '', $email = $row->email) . '<br/>';
echo form_label('Password', 'password') . '<br/>';
echo form_input($data = '', $password = $row->passwords) . '<br/>';
echo form_label('Mobile', 'user name') . '<br/>';
echo form_input($data = '', $mobile = $row->mobile) . '<br/>';
echo form_submit('update', 'Update');
} ?>
<?php echo form_close(); ?>
</body>
</html>
```
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: unable to determine current zabbix database version: the table "dbversion" was not found.
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.