So I’m trying to learn how to use CI and the person who wants me to learn it said its easier to use it without a model just with controlers and views. I’m trying to edit a row from my DB . Here is my controler code
public function index() {
$alimente = $this->db->query('select alimente.id, alimente.name FROM alimente order by name asc')->result();
$totalQuery = $this->db->query("select count(id) as total from alimente")->result();
$nrcrt = 1;
foreach ($alimente as $a) {
$a->nrcrt = $nrcrt++;
}
$content = $this->parser->parse('alimente/list_alimente', array("ALIMENTE" => $alimente, "COUNT" => $totalQuery), true);
$TITLE = " Lista Alimente";
$array = array('TITLE' => $TITLE, 'CONTENT' => $content);
$this->parser->parse('TEST', $array, false);
}
public function add() {
$add_aliment = $this->parser->parse('alimente/add_aliment', array(), true);
$TITLE = "Adauga alimente";
$array = array('TITLE' => $TITLE, 'CONTENT' => $add_aliment);
$this->parser->parse('TEST', $array, false);
}
public function add_done() {
$name = $this->input->post('name');
$data = array(
'name' => $name,
);
$this->db->insert('alimente', $data);
redirect("alimente");
}
public function edit($id) {
$alimente = $this->db->query('select alimente.id, alimente.name FROM alimente WHERE id = "' . $id . '" order by name desc')->result();
$content = $this->parser->parse('alimente/edit_alimente', array("ALIMENTE" => $alimente), true);
$TITLE = "Modifica";
$array = array('TITLE' => $TITLE, 'CONTENT' => $content);
$this->parser->parse('TEST', $array, false);
}
public function edit_done() {
$name = $this->imput->post("name");
$id = $this->imput->post("id");
$query = $this->db->prepare("update alimente set name = '".$name."' where id = '".$id."'")->result();
$query->execute($name, $id);
redirect("alimente");
}
And here is my views file
<main role="main" class="col-md-9 ml-sm-auto col-lg-10 px-4">
<div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
<h1 class="h2">Modifica alimente</h1>
</div>
<form method="post" action="{SITE_URL}/alimente/edit_done">
<div class="form-group">
{ALIMENTE}
<label> Id aliment</label>
<input type="text" name="id" value= " {id} " class="form-control" />
<label> Nume aliment</label>
<input type="text" name="name" value= " {name} " class="form-control" />
<br>
{/ALIMENTE}
</div>
<input type="submit" value="Modifica" name="name" class="btn btn-primary" />
</form>
Any help would be great, and I’m sorry if the code is horrible, I just started learning programing for 3 months
EDIT : I forgot to put the error I’m getting
A PHP Error was encountered Severity: Notice
Message: Undefined property: Alimente::$imput
Filename: controllers/Alimente.php
Line Number: 49
Backtrace:
File: C:\xampp\htdocs\CodeIgnite\application\controllers\Alimente.php
Line: 49 Function: _error_handlerFile: C:\xampp\htdocs\CodeIgnite\index.php Line: 315 Function:
require_onceAn uncaught Exception was encountered Type: Error
Message: Call to a member function post() on null
Filename:
C:\xampp\htdocs\CodeIgnite\application\controllers\Alimente.phpLine Number: 49
Backtrace:
File: C:\xampp\htdocs\CodeIgnite\index.php Line: 315 Function:
require_once
First of all CI is a MVC framework so all the database related things should go to models.
if you try to access the database from controller or from view. This is a voilation of archetecture and its not a good practise to access the database from controller. TRY creating seperate file for example
employee.php (html) where all the html will go in applciaiton/view folder
Employee.php (controller )where all the url method will hit and from there it will call appropiate method from model . will go in applciaiton/controller folder
Employee_model (model) where all the functions to insert update and delete into database will be fine in model.
I will recommend you to watch some tutorials to understand the concept of MVC
Answer:
Change imput into input then try
public function edit_done() {
$name = $this->input->post("name");
$id = $this->input->post("id");
$query = $this->db->prepare("update alimente set name = '".$name."' where id = '".$id."'")->result();
$query->execute($name, $id);
redirect("alimente");
}