I am trying to update a mySQL table while using CodeIgniter.
The Controller
I have the redirect() commented out to use print_r(). When it returns the array, it returns the correct updated values. But if i uncomment the redirect, then it will redirect me to the page with the table and the values will not get updated. I also check on phpMyAdmin to make sure values arent getting updated and not just displaying, but they arent updating on their either. This is confusing me because the print_r() is returning the correct values.
<?php
class update_ctrl extends CI_Controller {
function __construct(){
parent::__construct();
$this->load->model('update_model');
}
public function updateGame($id){
$this->load->model('update_model');
$data['games'] = $this->update_model->getGame($id);
$this->load->view('update_view', $data);
$this->load->view('footer');
}
public function update(){
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<span class="error">', '</span>');
$this->form_validation->set_rules('name', 'Name', 'trim|required');
$this->form_validation->set_rules('genre', 'Genre', 'trim|required');
$this->form_validation->set_rules('developer', 'Developer', 'trim|required');
$this->form_validation->set_rules('year', 'YearReleased', 'trim|required|numeric');
$this->form_validation->set_rules('price', 'Price', 'trim|required|numeric');
$id = $this->input->post('ID');
$data = array(
'Name' => $this->input->post('name'),
'Genre' => $this->input->post('genre'),
'Developer' => $this->input->post('developer'),
'YearReleased' => $this->input->post('year'),
'Price' => $this->input->post('price')
);
$this->load->model('update_model');
$this->update_model->update($id, $data);
$this->session->set_flashdata('msg', 'Game Updated!');
print_r($data);
//redirect('');
}
}
?>
The Model
<?php
class update_model extends CI_Model {
public function getGame($id) {
$this->db->select('*');
$this->db->from('games');
$this->db->where('ID', $id);
$query = $this->db->get();
if ($query->num_rows() > 0){
return $query->result();
} else {
return $query->result();
}
}
public function update($id, $data){
$this->db->where('ID', $id);
$this->db->update('games', $data);
}
}
?>
The only thing I can think of is that your query is silently failing. Go into database.php and change db_debug to TRUE and run everything again.
I would also like to add that you seem to do a good job making sure all your fields are validated. I'd also add a validation for id just to make sure you are getting it - in the case the user does something funky then they won't get some weird query error because the id wasn't set.
Comment out your all $this->form_validation->set_rules and try again, I think the numeric validation get errors
public function update(){
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
//$this->form_validation->set_error_delimiters('<span class="error">', '</span>');
//$this->form_validation->set_rules('name', 'Name', 'trim|required');
//$this->form_validation->set_rules('genre', 'Genre', 'trim|required');
//$this->form_validation->set_rules('developer', 'Developer', 'trim|required');
//$this->form_validation->set_rules('year', 'YearReleased', 'trim|required|numeric');
//$this->form_validation->set_rules('price', 'Price', 'trim|required|numeric');
$id = $this->input->post('ID');
$data = array(
'Name' => $this->input->post('name'),
'Genre' => $this->input->post('genre'),
'Developer' => $this->input->post('developer'),
'YearReleased' => $this->input->post('year'),
'Price' => $this->input->post('price')
);
$this->load->model('update_model');
$this->update_model->update($id, $data);
$this->session->set_flashdata('msg', 'Game Updated!');
print_r($data);
//redirect('');
}
}
Related
Hi i am new here and i dont know how to use codeigniter and now im confused. So i am currently trying to add user data to the database using codeigniter 3.1.10 . When i click the " save " button there's nothing to display. The page was refresh
Can you help me please?
Models:
function add_user($data) {
$this->db->set("username",$data["username"]);
$this->db->set("password",$data["password"]);
$this->db->set("indirizzo",$data["indirizzo"]);
$this->db->set("citta",$data["citta"]);
$this->db->set("cap",$data["cap"]);
$this->db->insert("user");
$ins_id =$this->db->insert_id();
return $ins_id;
}
Controllers:
function add() {
$this->load->library('form_validation');
$this->form_validation->set_rules('save', '', 'trim|required|number');
if ($this->form_validation->run()) :
$data = array(
"username"=>$this->input->post("username"),
"password"=>$this->input->post("password"),
"indirizzo"=>$this->input->post("indirizzo"),
"citta"=>$this->input->post("citta"),
"cap"=>$this->input->post("cap"),
);
$user_id= $this->user_model->add_user($data);
$this->log_model->scrivi_log($user_id,"user","add");
$this->session->set_flashdata('feedback', 'User added.');
redirect("user/pageuser/".$user_id);
else :
$content = $this->view->load("content");
$content->load("clienti_form","user/add");
$this->view->render();
endif;
}
Your doing a lot wrong, starting from the fact that your doing stuff from the model in your controller, and you should divide it, otherwise your not using the concept of MVC.
Try something like this, being hard to help you, without seeing the whole code:
Model
function add_user()
{
$data = array(
'username' => $this->input->post('username'),
'password' => $this->input->post('password'),
'indirizzo' => $this->input->post('indirizzo'),
'citta' => $this->input->post('citta'),
'cap' => $this->input->post('cap')
);
return $this->db->insert('user', $data);
}
Controller
function add() {
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username', 'required');
$this->form_validation->set_rules('password', 'Password', 'required');
$this->form_validation->set_rules('indirizzo', 'Indirizzo', 'required');
$this->form_validation->set_rules('citta', 'Citta', 'required');
$this->form_validation->set_rules('cap', 'Cap', 'required');
$errore = true;
if ($this->form_validation->run() === FALSE){ // if doesnt work load your view
$this->load->view('your view');
}
else {
$this->user_model->add_user();
$this->log_model->scrivi_log($user_id,"user","add");
$this->session->set_flashdata('feedback', 'User added.');
redirect("user/pageuser/".$user_id);
$content = $this->view->load("content");
$content->load("clienti_form","user/add");
$this->view->render();
}
}
You really should try and search more about it, and learn!
I could learn a lot of the basics of CodeIgniter, watching this channel that has great content, and explains every detail: https://www.youtube.com/playlist?list=PLillGF-RfqbaP_71rOyChhjeK1swokUIS
function add_user($data) {
$this->db->insert("user",$data);
$ins_id =$this->db->insert_id();
return $ins_id;
}
use this in model..
and in controller set rules for each like this
$this->form_validation->set_rules('username', 'Username', 'required');
$this->form_validation->set_rules('password', 'Password', 'required');
// for all other
I have a controller with:
if($_POST) {
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$val = $this->form_validation;
$val->set_rules('content[title]', 'Title', 'trim|required');
$val->set_rules('content[subtitle]', 'Subtitle', 'trim');
$val->set_rules('content[description]', 'description', 'trim');
if ($val->run() AND $this->db->insert('content', $content)) {
// query done
}
}
When I post form I am getting this error:
Fatal error: Call to undefined method stdClass::load() in ...\libraries\Form_validation.php on line 450
Line of 450 of Form_validation.php
// Load the language file containing error messages
$this->CI->lang->load('form_validation');
Please help me fix this....
replace your input names with title, subtitle, description and try this code
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
if ($this->input->post()) {
$this->form_validation->set_rules('title', 'Title', 'trim|required');
$this->form_validation->set_rules('subtitle', 'Subtitle', 'trim');
$this->form_validation->set_rules('title', 'Description', 'required');
$data['content'] = array(
'db_field_name' => $this->input->post('title'),
'db_field_name' => $this->input->post('subtitle'),
'db_field_name' => $this->input->post('description'),
);
if ($this->form_validation->run()){
$this->db->insert('content', $data['content']);
}
}
If you are using form validation and all for multiple instance you can define the helper and library in the config/autoload.php. Also refer the input_field name for validation rule.
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
if ($this->input->post()) {
$this->form_validation->set_rules('title', 'Title', 'trim|required');
$this->form_validation->set_rules('subtitle', 'Subtitle', 'trim');
$this->form_validation->set_rules('title', 'Description', 'required');
if ($this->form_validation->run() == TRUE) {
//Your DB operation
$this->data['formdata'] = array(
'db_field_title' => $this->input->post('title'),
'db_field_subtitle' => $this->input->post('subtitle'),
'db_field_description' => $this->input->post('description')
);
$this->db->insert('content', $this->data['content']);
} else {
//Show error message
echo validation_errors();
}
}
In your case, i think there is missing of language file
You have to check that in system/language/english folder there is form_validation_lang.php is available or not. there is missing of form_Validation_lang.php you have to put that file
Working user manage page with codeigniter, I made edit user function my self and that works but notification can't working. Mean If problem in updating user detail that should through error or Success but nothing happening but datas are updating.
Here is Model(Admin_model.php):
public function editUser($id, $data)
{
$this->db->where('id', $id);
$this->db->update('users', $data);
}
controller(Admin.php):
public function edit_user($id)
{
//set validation rules
$this->form_validation->set_rules('firstname', 'First Name', 'trim|required');
$this->form_validation->set_rules('lastname', 'Last Name', 'trim|required');
$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('default/admin/edit_user');
} else {
$data = array(
'firstname' => $this->input->post('firstname'),
'lastname' => $this->input->post('lastname'),
'email' => $this->input->post('email'),
'updated_at' => date('Y-m-j H:i:s')
);
if($this->Admin_model->editUser($id,$data)) {
$this->session->set_flashdata('global_alert','<div class="alert alert-success text-center">User updated!</div>');
redirect('admin/users');
} else {
$this->load->view('default/admin/edit_user');
$this->session->set_flashdata('global_alert','<div class="alert alert-danger text-center">Something wrong!</div>');
}
}
}
$this->Admin_model->editUser is not returning anything. try
public function editUser($id, $data)
{
$this->db->where('id', $id);
$result = $this->db->update('users', $data);
if ($result === FALSE)
{
show_error('error !');
}
return $result;
}
I have a codeigniter form which runs some basic validation and submits data to the database. But I want to additionally alter the post data of one of the fields to use the inflector helper in order to convert the posted data to camel case before submitting to the database. How do I do this?
Here is my current form:
<?php echo form_open('instances/create') ?>
<label for="content">Content</label>
<textarea name="content"></textarea><br />
<input type="submit" name="submit" value="Create" />
</form>
Here is my current controller:
public function create(){
$this->load->helper('form');
$this->load->library('form_validation');
$this->load->helper('inflector');
$data['title'] = 'Create an instance';
$this->form_validation->set_rules('title', 'Title', 'required');
//want to camelize the 'title' here
if ($this->form_validation->run() === FALSE)
{
$this->load->view('templates/header', $data);
$this->load->view('instances/create');
$this->load->view('templates/footer');
}
else
{
$this->instances_model->set_instances();
$this->load->view('instances/success');
}
}
and here's my model:
<?php
class Instances_model extends CI_Model {
public function __construct(){
$this->load->database();
}
public function get_instances($slug = FALSE){
if ($slug === FALSE){
$query = $this->db->get('extra_instances');
return $query->result_array();
}
$query = $this->db->get_where('extra_instances', array('slug' => $slug));
return $query->row_array();
}
public function set_instances(){
$this->load->helper('url');
$slug = url_title($this->input->post('title'), 'dash', TRUE);
$data = array(
'slug' => $slug,
'title' => $this->input->post('title'),
'content' => $this->input->post('content'),
'year' => $this->input->post('year'),
'credit' => $this->input->post('credit'),
'source' => $this->input->post('source')
);
return $this->db->insert('extra_instances', $data);
}
}
I know that you can camelize a variable with the following:
echo camelize('my_dog_spot'); // Prints 'myDogSpot'
and I know that you can run custom validation like this:
$this->form_validation->set_rules('username', 'Username', 'callback_username_check');
public function username_check($str)
{
if ($str == 'test')
{
$this->form_validation->set_message('username_check', 'The {field} field can not be the word "test"');
return FALSE;
}
else
{
return TRUE;
}
}
But I'm lacking the knowledge of how to put this altogether to quickly change the POST data before submitting to the database.
Nothing too complicated, you can do it after you pass the validation, just before inserting your data array into the database:
$data = array(
'slug' => $slug,
'title' => camelize($this->input->post('title')),
// ...
I am currently trying to add data to the database using codeigniter. I have already set up a registration page using the active method and attempted to use the same method for the add news form but was unsuccessful.
When I click submit it is saying page cannot be found and the url shows the controller function name. This is the same when i purposely leave any fields blank. I have checked my database and no records have been added and no php log errors.
Here is my snippets of code:
View:
<?php echo form_open('add/add_article'); ?>
<?php echo form_input('title', set_value('title', 'Title')); ?><br />
<?php echo form_textarea('content', set_value('content', 'Content')); ?><br />
<?php echo form_input('author', set_value('author', 'Author')); ?>
<?php echo form_submit('submit', 'Add Article'); ?>
<?php echo validation_errors('<p class="error">' );?>
<?php echo form_close(); ?>
Controller:
class Add extends CI_Controller {
public function __construct() {
parent::__construct();
}
public function index() {
$this->load->view('admin/add');
}
public function add_article() {
$this->load->library('form_validation');
$this->form_validation->set_rules('title', 'Title', 'trim|required');
$this->form_validation->set_rules('content', 'Content', 'trim|required');
$this->form_validation->set_rules('author', 'Author', 'trim|required');
if($this->form_validation->run() == FALSE) {
$this->index();
}else{
$this->load->model('news_model');
if($query = $this->news_model->addArticle()) {
$this->load->view('news');
}else {
$this->load->view('news');
}
}
}
}
Model:
public function __construct() {
parent::__construct();
}
function addArticle() {
$data =array(
'title' => $this->input->post('title'),
'content' => $this->input->post('content'),
'author' => $this->input->post('author'),
'username' => $this->input->post('username'));
$insert = $this->db->insert('news', $data);
return $insert;
}
}
If it's the server that's throwing the page not found it's almost certainly a URL issue as opposed to a CI/PHP issue.
Is your base url defined properly in the config file? Is your .htaccess configured properly (an old configuration could be routing /add requests away from CI)?
Try adding the following action to the Add controller, and navigating to it directly at http://[base]/add/thetest
public function thetest() {
echo 'Controller accessed';
die;
}
If it still says page not found it's not your code, it's your config (either server config or CI).
Instead of insert use update in your model like:
$insert = $this->db->update('news', $data);
return $insert;
And I think that this part of your code in controller is wrong too (wrong if statement and no data send to model):
if($query = $this->news_model->addArticle()) {
$this->load->view('news');
}else {
$this->load->view('news');
}
try this:
$data =array(
'title' => $this->input->post('title'),
'content' => $this->input->post('content'),
'author' => $this->input->post('author'),
'username' => $this->input->post('username')
);
$query = $this->news_model->addArticle($data);
if($query)
{
// query ok
$this->load->view('news');
}
else {
// no query
$this->load->view('news');
}