Here is my function
function store(){
$this->load->library('form_validation');
$this->form_validation->set_rules('shorthand','Shorthand','required|is_unique[locations.shorthand]');
if($this->form_validation->run() == FALSE){
redirect('locations');
} else {
$id = $this->input->post('location_id');
$location_name = ucwords(strtolower($this->input->post('location_name')));
$shorthand = strtolower($this->input->post('shorthand'));
$station = ucwords(strtolower($this->input->post('station')));
$data = array(
'location_name'=>$location_name,
'shorthand'=>$shorthand,
'station'=>$station
);
}if($id){
$result = $this->location_model->update($data,$id);
if($result > 0){
$this->session->set_flashdata('success', 'Update successfull!');
}else{
$this->session->set_flashdata('error', 'Failed to update!');
}
redirect('locations');
}else{
$result = $this->location_model->create($data);
if($result > 0){
$this->session->set_flashdata('success', 'Location added!');
}else{
$this->session->set_flashdata('error', 'Failed to add location!');
}
redirect('locations');
}
}
The shorthand updates fine but station and location_name wont update no matter what I do. But if I remove this chunk of code.
$this->load->library('form_validation');
$this->form_validation->set_rules('shorthand','Shorthand','required|is_unique[locations.shorthand]');
if($this->form_validation->run() == FALSE){
redirect('locations');
} else {
All of the fields will update. Can you please look at my codes? Thanks in advance.
Here is my Update Query
function update($data, $id){
$this->db->where('location_id', $id);
$this->db->update('locations', $data);
return $this->db->affected_rows();
}
I think this what you need. Please manage according to your need
$locations = $this->db->get_where('locations', array('location_id' => $id))->row();
$original_value = $locations->shorthand;
if($this->input->post('shorthand') != $original_value) {
$is_unique = '|is_unique[locations.shorthand]';
} else {
$is_unique = '';
}
$this->form_validation->set_rules('shorthand','Shorthand','required|trim|xss_clean'.$is_unique);
Guys for the people who have commented, thank you but I have already solved the problem it
was inside controller I forgot to put else after this line
if($this->form_validation->run() == FALSE){
redirect('locations');
}
This is how the whole function looks now
function store(){
$this->form_validation->set_rules('location_name','Location_name','required');
$this->form_validation->set_rules('shorthand','Shorthand','required|callback_locationExists');
$this->form_validation->set_rules('station','Station','required');
$id = $this->input->post('location_id');
$location_name = ucwords(strtolower($this->input->post('location_name')));
$shorthand = strtolower($this->input->post('shorthand'));
$station = ucwords(strtolower($this->input->post('station')));
$data = array(
'location_name'=>$location_name,
'shorthand'=>$shorthand,
'station'=>$station,
);
if($this->form_validation->run() == FALSE){
redirect('locations');
}else{
if($id){
$result = $this->location_model->update($data,$id);
if($result > 0){
$this->session->set_flashdata('success', 'Update successfull!');
}else{
$this->session->set_flashdata('error', 'Failed to update!');
}
redirect('locations');
}else{
$result = $this->location_model->create($data);
if($result > 0){
$this->session->set_flashdata('success', 'Location added!');
}else{
$this->session->set_flashdata('error', 'Failed to add location!');
}
redirect('locations');
}
}
}
Thanks for reading
Related
I have a web application that need to send message to many friends.But i suffer an error..please help me....
Here below is the my controller
public function message(){
$this->form_validation->set_rules('message','Message', 'required');
$this->form_validation->set_rules('friend_id[]','Recipients', 'required');
if($this->form_validation->run()){
$data = $this->input->post("friend_id[]");
unset($data['submit']);
$this->load->model('Queries');
$message=$this->Queries->saveMessage();
if($message){
echo "Success";
}else{
echo "error";
}
}
else{
echo validation_errors();
}
}
This is My Model
public function saveMessage($data){
$i=0;
foreach($data['friend_id'] as $friend_id){
$record = array(
'friend_id' => $friend_id,
'message' => $data['message']
);
$this->db->insert('tbl_messages', $record);
$i++;
}
return $data;
}
When i run it..it display error and no data is inserted in database
Hope this will help you :
Your controller method message() should be like this :
public function message()
{
$this->form_validation->set_rules('message','Message', 'required');
$this->form_validation->set_rules('friend_id[]','Recipients', 'required');
if($this->form_validation->run())
{
$friend_ids = $this->input->post("friend_id[]");
$message = $this->input->post("message");
unset($data['submit']);
$this->load->model('Queries');
$insert = $this->Queries->saveMessage($friend_ids, $message);
if($insert)
{
echo "Success";
}
else
{
echo "error";
}
}
else
{
echo validation_errors();
}
}
Your model method saveMassage() should be like this :
public function saveMessage($friend_ids , $message)
{
foreach($friend_ids as $friend_id)
{
$record = array('friend_id' => $friend_id,'message' => $message);
$this->db->insert('tbl_messages', $record);
}
return true;
}
I tried to make a change password function, but nothing happened.I actually adapted codeigniter change password code but again, nothing happened.Here I attached the code, any hep would be appreciated.
the controller -
function change_password_process()
{
$this->load->view('ubahpassword_view');
$this->load->library('form_validation');
$this->load->library('session');
$this->form_validation->set_rules('pass_lama','Password Lama','trim|required|min_length[4]|max_length[32]');
$this->form_validation->set_rules('pass_baru','Password Baru','trim|required|min_length[4]|max_length[32]');
$this->form_validation->set_rules('ulangpass_baru','Ulangi Password Baru','trim|required|min_length[4]|max_length[32]|matches[pass_baru]');
if ($this->form_validation->run() == FALSE)
{
redirect('ubahpassword');
}
else
{
$query = $this->rekammedis_model->change_password();
redirect('ubahpassword');
}
}
The model -
function change_password()
{
$this->db->select('id');
$this->db->where('username', $this->session->userdata('username'));
$this->db->where('id', $this->session->userdata('id'));
$this->db->where('password', md5($this->input->post('pass_lama')));
$query = $this->db->get('user');
if ($query->num_rows() > 0)
{
$row = $query->row();
if($row->id === $this->session->userdata('id'))
{
$data = array(
'password' => md5($this->input->post('pass_lama'))
);
$this->db->where('username', $this->session->userdata('username'));
$this->db->where('password', md5($this->input->post('pass_lama')));
if($this->db->update('user', $data))
{
return "Password berhasil diganti!";
}
else
{
return "Terdapat kesalahan, password tidak terganti";
}
}
else
{
return "Terdapat kesalahan, password tidak terganti";
}
}
else
{
return "Password lama salah";
}
}
Assuming baru means new and lama means old.
You were changing your old password with old password itself (pass_baru)
So Replace
$data = array('password' => md5($this->input->post('pass_lama')));
with
$data = array('password' => md5($this->input->post('pass_baru')));
UPDATE
Found Another Bug
if ($this->form_validation->run() == FALSE)
{
redirect('ubahpassword');
}
You can't redirect it, if you do, you won't get validated here.
Load your view here itself. Also update your code above if it does not work
Whenever i run this code, i recieve a blank screen on submission. The code does not seem to work and i'm unsure why. Can anyone see an issue? I've checked it for syntax already mutliple times.
Controller:
public function changepwd(){
$this->form_validation->set_rules('oldpassword','Old Password','required|trim|xss_clean|callback_change');
$this->form_validation->set_rules('password1','New Password','required|trim');
$this->form_validation->set_rules('password2','Confirm Password','required|trim|matches[password1]');
if($this->form_validation->run()= true){
redirect('admin');
}else{
$this->form_validation->set_message('change','info is wrong');
redirect('admin');
}
}
public function change(){
$this->load->view('error_display');
$oldpass = $this->input->post('oldpassword');
if(isset($oldpass)){
if($this->input->post('password1') == $this->input->post('password2')){
$session=$this->session->userdata("logged_in");
$username= $session['username'];
$query = "SELECT * FROM database where username='$username'";
$result = $this->db->query($query);
$row=$result->row();
$pass = $row->password;
$s= $row->salt;
$user_old_pass = sha1($s.$oldpass);
if($pass == $user_old_pass){
$new = substr(uniqid(rand(), true), 0, 20);
$users_password = $this->input->post('pass1');
$new_password = sha1($new.$user_password);
$data = array($new,$new_password);
$this->update_model->insert_model($data);
$this->form_validation->set_message('change','saved');
$this->load->view('admin');
}else{
$this->form_validation->set_message('change','<p class="error">Incorrect Password</p>');
return false;
redirect('admin');
}
}else{
$this->form_validation->set_message('change','passwords dont match');
return false;
redirect('admin');
};
}else{
$this->form_validation->set_message('change','old password is not entered');
return false;
redirect('admin');
}
model:
function login($data)
{
$this->db->update('database', $data);
}
your
if($this->form_validation->run()= true){
should be
if($this->form_validation->run() == FALSE) {
because you are redirecting every time to 'admin'
For some reason im not able to access $id when im trying to redirect after i submit my form. I can access $id outside the if statement but it seems i get the redirect with an empty value of $id. Any suggestion?
function edit_user($id = NULL){
if(!$id)
{
show_404();
}
if($this->input->post('submit'))
{
$this->form_validation->set_rules('username','Nombre de usuario','required');
$this->form_validation->set_rules('password','Contraseña','required');
$this->form_validation->set_rules('checkpassword','Reingrese contraseña','required|matches[password]');
$this->form_validation->set_message('required','El Campo %s es obligatorio');
$this->form_validation->set_message('matches','Las contraseñas ingresadas no coinciden');
if($this->form_validation->run() != FALSE){
$this->Users_model->edit_users();
$mensaje = "Usuario editado correctamente";
redirect(base_url().'users/edit_user/'.$id, 301);
}
else
{
redirect(base_url().'users/edit_user/'.$id, 301);
}
}
$query = $this->Users_model->getuserid($id);
if($query == false){
$data = array('title'=>'Admin ::LxFPanamá::',
'content'=>'users/add_users_view',
'id'=>$id);
}else{
$data = array('title'=>'Admin ::LxFPanamá::',
'content'=>'users/edit_users_view',
'id'=> $query->id,
'login'=> $query->login,
'password'=> $query->password);
}
$this->load->view('themes/'.$this->config->item('theme_front').'.php', $data);
}
Try using flashdata
$this->session->set_flashdata('item',$i);
redirect(base_url().'users/edit_user','refresh',301);
to get the value back
$this->session->flashdata('item');
This question already has answers here:
CI - show database error or fail
(2 answers)
Closed 9 years ago.
I've developed a simple login system which works OK but fails, and I need to know why.
QUESTION: How to show what is causing the fail?
Here's the database function:
function login($email,$password)
{
$this->db->where("email",$email);
$this->db->where("password",$password);
$query=$this->db->get("users");
if($query->num_rows()>0)
{
foreach($query->result() as $rows)
{
//add all data to session
$newdata = array(
'user_id' => $rows->id,
'user_name' => $rows->username,
'user_email' => $rows->email,
'logged_in' => TRUE,
);
}
$this->session->set_userdata($newdata);
return true;
}
return false;
}
And here's the logic:
public function login()
{
$this->load->library('form_validation');
// field name, error message, validation rules
$this->form_validation->set_rules('email', 'Your Email', 'trim|required|valid_email');
$this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[4]|max_length[32]');
if($this->form_validation->run() == FALSE)
{
$this->signin();
}
else
{
$email=$this->input->post('email');
$password=md5($this->input->post('pass'));
$result=$this->user_model->login($email,$password);
if($result)
{
$this->dash();
}
else
{
$data['title']= 'Login Error';
$this->load->view('nav/header', $data);
$this->load->view('login', $data);
$this->load->view('nav/footer', $data);
}
}
}
I know the error is happening as I redirect back to the login page if it fails and change the title text to show me (only in testing mode for right now). But how can I find out what is going wrong?
This is the check database function:
function login($email,$password)
{
$this->db->where("email",$email);
$this->db->where("password",$password);
$query=$this->db->get("users");
if($query->num_rows()>0)
{
foreach($query->result() as $rows)
{
//add all data to session
$newdata = array(
'user_id' => $rows->id,
'user_name' => $rows->username,
'user_email' => $rows->email,
'logged_in' => TRUE,
);
}
$this->session->set_userdata($newdata);
return true;
}
return false;
}
I assume all your php code is fine, then what you need is set custom form-validation-message for each input to know which input went wrong and echo them:
<?php echo validation_errors(); ?>
write below code in your view file
<section id="notification" >
<?php
if(validation_errors() !== '' ) {
echo "<div class='alert-msg error'>";
echo validation_errors();
echo "</div>";
}
$error = $this->session->flashdata('error');
$success = $this->session->flashdata('success');
if($error)
{
echo "<div class='alert-msg error'>";
echo $this->session->flashdata('error');
echo "</div>";
}
if($success)
{
echo "<div class='alert-msg success'>";
echo $this->session->flashdata('success');
echo "</div>";
}
?>
</section>
and set success/error message conditionally in flash data in controller ( see below)
if($result) {
$this->dash();
$this->session->set_flashdata('success', 'Login successfully.');
} else {
$this->session->set_flashdata('error', 'Login failed');
}
Read more Flashdata in CI
For your changed answer:
use below logic in your model
$qry = $this->db->get_where('users', array('username' => $this->_username ));
if ($qry->num_rows() == 1) {
$user = $qry->row_array();
$submitted_pass = md5($this->_password);
$db_pass = $user['password'];
if ($submitted_pass === $db_pass) {
return $user;
} else {
// wrong username/password
$this->session->set_flashdata('error', $this->errorList[10]);
return FALSE;
}
} else {
// no such username exist
$this->session->set_flashdata('error', $this->errorList[15]);
return FALSE;
}