validation false, although the data is filled - php

I have form validation like this :
function insert() {
$this->form_validation->set_message('required', '*column must be filled');
$this->form_validation->set_rules('judul', 'Judul', 'required');
$this->form_validation->set_rules('isi', 'Isi', 'required');
if ($this->form_validation->run() == FALSE) {
$this->layout->addJs('ckeditor/ckeditor.js');
$this->layout->addJs('js/admin/b267648789c30a07b0efa5bce7bdd9fe.js');
$this->layout->view('admin/admin_view/insertAdmin_view');
} else {
$data = array(
'title' => $this->input->post('judul'),
'content' => $this->input->post('isi'),
);
$this->db->insert('newses', $data);
... other proses ...
}
}
although I filled the data in form add data (I use ckeditor for adding data), condition always false so it's not adding data that I want. but it's got error 'if I add some html data, but if I add normal data text it's work well'. once again when I running my program in localhost xampp it's all work but when I use hosting that happen.
any suggest ?
thank you for all advice.

Print the errors of validation
function insert() {
$this->form_validation->set_message('required', '*column must be filled');
$this->form_validation->set_rules('judul', 'Judul', 'required');
$this->form_validation->set_rules('isi', 'Isi', 'required');
//echo validation_errors();
echo $this->input->post('judul')."<br>";
echo $this->input->post('isi');
if ($this->form_validation->run() == FALSE) {
$this->layout->addJs('ckeditor/ckeditor.js');
$this->layout->addJs('js/admin/b267648789c30a07b0efa5bce7bdd9fe.js');
$this->layout->view('admin/admin_view/insertAdmin_view');
} else {
$data = array(
'title' => $this->input->post('judul'),
'content' => $this->input->post('isi'),
);
$this->db->insert('newses', $data);
... other proses ...
}
}

Related

Add user on Codeigniter 3

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

Codeigniter - Edit user as admin

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;
}

Codeigniter doesn't let me update entry, because some fields must be unique

So what I'm trying to do:
Pull User data from database, including email address, username etc.
Edit it
Save it
But I want to keep username and email unique.
And for this I'm setting validation rules like this:
$this->form_validation->set_rules('firstname', 'First Name', 'required|min_length[2]|max_length[15]');
$this->form_validation->set_rules('lastname', 'Last Name', 'required|min_length[2]|max_length[15]');
$this->form_validation->set_rules('email', 'Email', 'required|valid_email|is_unique[users.email]');
$this->form_validation->set_rules('username', 'Username', 'required|min_length[4]|max_length[12]|is_unique[users.username]');
$this->form_validation->set_rules('password1', 'Password', 'required|matches[password2]');
$this->form_validation->set_rules('password2', 'Password Confirmation', 'required');
$this->form_validation->set_rules('group', 'User Group', 'required');
And as you can see I have is_unique[users.username] and is_unique[users.email] rules. But it doesn't let me update my entry using this rules.
So the question is, how can I update database entry, and keep those 2 fields unique(username and email)?
use the call back validation function
$this->form_validation->set_rules('email', 'Email', 'required|valid_email|callback_check_user_email');
function check_user_email($email) {
if($this->input->post('id'))
$id = $this->input->post('id');
else
$id = '';
$result = $this->user_model->check_unique_user_email($id, $email);
if($result == 0)
$response = true;
else {
$this->form_validation->set_message('check_user_email', 'Email must be unique');
$response = false;
}
return $response;
}
in model
function check_unique_user_email($id = '', $email) {
$this->db->where('email', $email);
if($id) {
$this->db->where_not_in('id', $id);
}
return $this->db->get('user')->num_rows();
}
use same for the user name....
In Codeigniter 4
// is_unique[table.field,ignore_field,ignore_value]
$validation->setRules([
'name' => "is_unique[supplier.name,uuid, $uuid]", // is not ok
'name' => "is_unique[supplier.name,uuid,$uuid ]", // is not ok
'name' => "is_unique[supplier.name,uuid,$uuid]", // is ok
'name' => "is_unique[supplier.name,uuid,{uuid}]", // is ok - see "Validation Placeholders"
]);
Eg.
$validation->setRules(['email' => 'required|valid_email|is_unique[users.email,id,4]']);
// 'id' = ignore table field name (users table 'id' field)
// '4' = ignore field value(currnet user id)
codeigniter 4 validation
For Codeigniter 3
Add following helper class (edit_unique_helper.php)
function edit_unique($value, $params) {
$CI =& get_instance();
$CI->load->database();
$CI->form_validation->set_message('edit_unique', "Sorry, that %s is already being used.");
list($table, $field, $current_id) = explode(".", $params);
$query = $CI->db->select()->from($table)->where($field, $value)->limit(1)->get();
if ($query->row() && $query->row()->id != $current_id)
{
return FALSE;
} else {
return TRUE;
}
}
Add to autoload ()
Use edit_unique where you pass an extra parameter which is the id of the row you're editing
eg. $this->form_validation->set_rules('user_name', 'User Name', 'required|trim|xss_clean|edit_unique[users.user_name.'.$id.']');
Source

Codeigniter Form Validation not submitting form and no errors

I had this form working and to be honest I can not figure out what I might have done. The form will not submit now and I don't see any form validation errors.
controller:
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<div class="ncerror" >', '</div>');
$this->form_validation->set_rules('media_consent', 'Media Consent', 'required');
$this->form_validation->set_rules('aic_name', 'Name', 'required');
$this->form_validation->set_rules('aic_phone', 'Cell phone', 'required');
$this->form_validation->set_rules('aic_email', 'Email', 'required');
if ($this->form_validation->run() == FALSE) {
$data = $this->ncformdata();
$this->load->view('templates/sponsorheader', array('title' => 'National Convention Registration'));
var_export($_POST);
$this->load->view('ncform', $this->ncformdata());
$this->load->view('templates/footer2');
}
I have
<?php echo "errors" . validation_errors();
at the top of my form. After submitting form, the form reloads with "errors" displayed but no other output from the validation_errors function. Any help with troubleshooting?
should this line
$this->load->view('ncform', $this->ncformdata());
be this?
$this->load->view('ncform', $data);
You have a condition when
$this->form_validation->run() == FALSE
but I can not see any condition for when the form validation returns TRUE. Try adding a condition when
$this->form_validation->run() == TRUE
Example:
if ($this->form_validation->run() == FALSE) {
$data = $this->ncformdata();
$this->load->view('templates/sponsorheader', array('title' => 'National Convention Registration'));
var_export($_POST);
$this->load->view('ncform', $this->ncformdata());
$this->load->view('templates/footer2');
}
else
{
echo 'There are no form validation errors!.';
}

Duplicate error in creating a login user

I have used codeigniter code in the below when I create a user if it is already exist it should display a message duplicate value but it displays 1062 error. Pls help to solve the issue.
Controller
function create_member()
{
$this->load->library('form_validation');
// field name, error message, validation rules
$this->form_validation->set_rules('first_name', 'Name', 'trim|required');
$this->form_validation->set_rules('last_name', 'Last Name', 'trim|required');
$this->form_validation->set_rules('email_address', 'Email Address', 'trim|required|valid_email');
$this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[4]');
$this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[4]|max_length[32]');
$this->form_validation->set_rules('password2', 'Password Confirmation', 'trim|required|matches[password]');
if($this->form_validation->run() == FALSE)
{
$this->load->view('signup_form');
}
else
{
$this->load->model('membership_model');
if($query = $this->membership_model->create_member())
{
$data['main_content'] = 'signup_successful';
$this->load->view('includes/template', $data);
}
else
{
$this->load->view('signup_form');
}
}
}
Model
function create_member()
{
$new_member_insert_data = array(
'first_name' => $this->input->post('first_name'),
'last_name' => $this->input->post('last_name'),
'email_address' => $this->input->post('email_address'),
'username' => $this->input->post('username'),
'password' => md5($this->input->post('password'))
);
$insert = $this->db->insert('membership', $new_member_insert_data);
if ($this->db->_error_number() == 1062)
{
$this->session->set_flashdata('duplicate_email', 'Duplicate value');
redirect('login');
}
return $insert;
}
It seems that your error is because your database is not allowing to add multiple entries with same username/email, for this you need to add codeigniter is_unique validation on that particular field. Below is example for codeigniter unique validation : $this->form_validation->set_rules('username', 'Username', 'required|min_length[5]|max_length[12]|is_unique[membership.username]');
Try to use $this->db->query instead of insert and create a SQL statement using INSERT IGNORE and you can handle the duplicates silently.
Why just not do a previous query to check if the email exists aleady ?
Anyway, you can do something like that:
$query_string = $this->db->insert_string('users', $data);
$this->db->query($query_string.' ON DUPLICATE KEY UPDATE id=id');
// or
$this->db->query(str_replace("INSERT INTO", "INSERT IGNORE INTO", $query_string));
if( $this->db->affected_rows() == 0 ) {
//duplicate email
}

Categories