I am trying to update my database with data from a form, the form gets filled in with data from the daatabase. but when i click submit it gives errors. can anyone help?
my controller:
public function update()
{
$this->load->helper('form');
$this->load->library('form_validation');
$data['title'] = 'Update a product';
$this->form_validation->set_rules('title', 'Title', 'required');
$this->form_validation->set_rules('price', 'Price', 'required');
$this->form_validation->set_rules('description', 'Description', 'required');
$this->form_validation->set_rules('publisher', 'Publisher', 'required');
$this->form_validation->set_rules('engine', 'Engine', 'required');
$this->form_validation->set_rules('release_date', 'Release date', 'required');
$this->form_validation->set_rules('platform', 'Platform', 'required');
$this->form_validation->set_rules('genre', 'Genre', 'required');
$this->form_validation->set_rules('picture', 'Picure', 'required');
if ($this->form_validation->run() === FALSE)
{
$this->load->view('templates/header2', $data);
$this->load->view('admin/update', $data);
$this->load->view('templates/footer2');
}
else
{
$this->admin_model->update_products();
$this->load->view('admin/success');
}
}
function input($id = 0)
{
$this->load->helper('form');
$this->load->helper('html');
if($this->input->post('mysubmit'))
{
$this->admin_model->update_products();
}
else
{
$this->admin_model->set_products();
}
if((int)$id > 0)
{
$query = $this->admin_model->get($id);
$data['id']['value'] = $query['id'];
$data['title']['value'] = $query['title'];
$data['price']['value'] = $query['price'];
$data['description']['value'] = $query['description'];
$data['publisher']['value'] = $query['publisher'];
$data['engine']['value'] = $query['engine'];
$data['release_date']['value'] = $query['release_date'];
$data['platform']['value'] = $query['platform'];
$data['genre']['value'] = $query['genre'];
$data['picture']['value'] = $query['picture'];
}
$this->load->view('admin/update',$data);
}
my model:
function get($id){
$this->load->database();
$query = $this->db->get_where('products',array('id'=>$id));
return $query->row_array();
}
function update_products($data){
$this->load->helper('url');
$data = array(
'id' => $this->input->post('id'),
'title' => $this->input->post('title'),
'price' => $this->input->post('price'),
'description' => $this->input->post('description'),
'publisher' => $this->input->post('publisher'),
'engine' => $this->input->post('engine'),
'release_date' => $this->input->post('release_date'),
'platform' => $this->input->post('platform'),
'genre' => $this->input->post('genre'),
'picture' => $this->input->post('picture')
);
$this->db->where('id',$id);
$this->db->update('products',$data);
}
my view:
<h2>Create a new product</h2>
<?php echo validation_errors(); ?>
<?php echo form_open('admin/update') ?>
<input type="hidden" value=<?php echo '"' . $id['value'] . '"' ?>>
<label for="title">Title</label>
<input type="text" name="title" value=<?php echo '"' . $title['value'] . '"' ?>><br><br>
<label for="price">Price</label>
<input type="number" step="any" name="price" value=<?php echo '"' . $price['value'] . '"' ?>><br><br>
<label for="description">Description</label>
<textarea name="description"><?php echo '"' . $description['value'] . '"' ?></textarea><br><br>
<label for="publisher">Publisher</label>
<input type="text" name="publisher" value=<?php echo '"' . $publisher['value'] . '"' ?>><br><br>
<label for="engine">Engine</label>
<input type="text" name="engine" value=<?php echo '"' . $engine['value'] . '"' ?>><br><br>
<label for="release_date">Release date</label>
<input type="date" name="release_date" value=<?php echo '"' . $release_date['value'] . '"' ?>><br><br>
<label for="platform">Platform</label>
<input type="text" name="platform" value=<?php echo '"' . $platform['value'] . '"' ?>><br><br>
<label for="genre">Genre</label>
<input type="text" name="genre" value=<?php echo '"' . $genre['value'] . '"' ?>><br><br>
<label for="picture">Picture</label>
<input type="text" name="picture" value=<?php echo '"' . $picture['value'] . '"' ?>><br><br>
<input type="submit" name="mysubmit" value="submit">
result after submit:
Create a new product
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: id
Filename: admin/update.php
Line Number: 7
""> Title
A PHP Error was encountered
Severity: Warning
Message: Illegal string offset 'value'
Filename: admin/update.php
Line Number: 10
"U">
Price
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: price
Filename: admin/update.php
Line Number: 13
"">
Description
Publisher
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: publisher
Filename: admin/update.php
Line Number: 19
"">
Engine
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: engine
Filename: admin/update.php
Line Number: 22
"">
Release date
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: release_date
Filename: admin/update.php
Line Number: 25
"">
Platform
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: platform
Filename: admin/update.php
Line Number: 28
"">
Genre
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: genre
Filename: admin/update.php
Line Number: 31
"">
Picture
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: picture
Filename: admin/update.php
Line Number: 34
"">
You have so many problems/errors in your code but I'll try to mark and fix them all. let's start...
In your input($id = 0)
method after following line:
$query = $this->admin_model->get($id);
You have code like this:
$data['id']['value'] = $query['id'];
Change all of them to:
$data['id'] = $query['id'];
$data['title'] = $query['title'];
// more...
Remove ['value']
from all of them. Then in your view
you have variables (array) like:
$id['value']
$title['value']
Remove ['value']
from those and just use $title
and so on for every variable/field. Then in your view again make changes from this:
<?php echo form_open('admin/update') ?>
To this:
<?php echo form_open('admin/update/' . $id) ?>
Also remove <input type="hidden" value=<?php echo '"' . $id['value'] . '"' ?>>
and finally in update_products
method, change it to:
function update_products($id)
{
//...
}
From $data
array in update_products
method; remove this:
'id' => $this->input->post('id'),
Hope this will work but you had many errors so not sure if I could have marked them all.Give it a try.
Update: You are calling update_products
from update
so you should pass the id
to update
method so make changes in, add the $id
parameter:
public function update($Id)
{
//...
}
Then in this method where you are calling the update_products
method pass the $id
with it like this:
$this->admin_model->update_products($id);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.