Codeigniter help with user registration script - php

:) This question is for anyone familiar with the Tankauth plugin.
I edited tankauth so that user profiles are populated without email validation.
In the registration form I added and validated two more fields named “first_name” and “last_name” which I want to add to the user_profiles table when the form is submitted.
Below is the tankauth function that adds the user id to the user_profiles table if user is activated. I commented out if ($activated). The problem is that I don’t know how to insert my new fields first_name and last_name to the db. I keep getting errors for anything I try.
function create_user($data, $activated = TRUE)
{
$data['created'] = date('Y-m-d H:i:s');
$data['activated'] = $activated ? 1 : 0;
if ($this->db->insert($this->table_name, $data)) {
$user_id = $this->db->insert_id();
//if ($activated)
$this->create_profile($user_id);
return array(
'user_id' => $user_id,
'first_name' => $first_name, //added by me
'first_name' => $first_name, //added by me
);
}
return NULL;
}
The above is connected with
private function create_profile($user_id)
{
$this->db->set('user_id', $user_id);
$this->db->set('first_name', $first_name); //added by me
return $this->db->insert($this->profile_table_name);
}
Can someone please share some guidance?

Try removing the private in private function create_profile
Also you might want to change your create_profile function to be:
function create_profile( $user_id ) {
$data = array(
'user_id' => $user_id,
'first_name' => $first_name // Hopefully this is being set correctly.
);
return $this->db->insert( $this->profile_table_name, $data );
}
If none of those work. Try using echo $this->db->last_query();
Hope that helps.

Related

How can I display the name of my admin full name after login - Codeigniter

I am having a trouble on displaying my admin's full name, like for example admin full name is John Doe, it is not displaying. I am still learning codeigniter please give me advice thank you!
here is my controller
//Get username
$username = $this->input->post('username');
//Get and encrypt the password
$password = $this->input->post('password');
// Login user
$user_id = $this->role_model->login($username, $password);
if ($user_id) {
// Create session
$user_data = array(
'user_id' => $user_id,
'name' => $user_id->name,
'username' => $username,
'logged_in' => true
);
$this->session->set_userdata($user_data);
//Set message
$this->session->set_flashdata('user_loggedin','You are now logged in');
redirect('pages/index');
here is my View - where I would like to display my full name, as you can see 'name' is the data field I have to display but it is does not show anything, it gives an error that says name is not defined.
<li><a> Welcome, <?php echo $this->session->name ?> </a></li>
Model
public function login($username, $password){
//Validate
$this->db->where('username',$username);
$this->db->where('password',$password);
$result = $this->db->get('users');
if ($result->num_rows() == 1) {
return $result->row(0)->id;
}else{
return false;
}
}
Your method login() returns only id = digit (return $result->row(0)->id;), not object (in controller your get $user_id->name).
Do this, in the model:
if ($result->num_rows() == 1) {
return $result->row(0); // fix
}else{
return false;
}
In the controller:
$user = $this->role_model->login($username, $password);
$user_data = array(
'user_id' => $user->id, // fix
'name' => $user->name, // fix
'username' => $username,
'logged_in' => true
);
It shows undefined because it is indeed undefined.
In your view your're trying to echo the returning value of a function called userdata(), does that function actually exist? Or is $this->session->userdata an array? in which that case, you need to use [ index ] to acces a member within an array. i.e. $this->session->userdata['name'], but also, that member needs to exist first.
controller :
if($this->Login_model->login_valid($email,$password))
{
$sessiondata = array(
'username' => $email
);
$this->session->set_userdata('logged_in', $sessiondata);
redirect('narson');
}
else
{
$this->session->set_flashdata('error',"Invalid Username And password");
redirect('login');
}
model:
public function login_valid($username,$password)
{
$this->db->where('username',$username);
$this->db->where('password',$password);
$query = $this->db->get('admin');
if($query->num_rows() > 0)
{
return true;
}
else
{
return false;
}
}

How do I set a variable `true` when updating the database has been successful?

On a blog I'm coding the admin can give an 'author'-permission to users.
When the update of the db table has been successful and their permission has been set to 'author' the admin will be headed back to the list of all current authors.
I want a message("Author has been added." for e.g) to appear on this site when it has been successful.
Of course the possibility of the db-update not working is minimal I think, but I want this case to be considered.
To do this I wanted to set a $newAuthor true when the database has been updated, but it didn't worked trying it with an if.
Here are the functions in the AdminController and the UserRepository with the db query:
//AdminController
public function permissionAuthor()
{
$id = $_GET['id'];
$permission = "author";
$newAuthor = false;
if($this->userRepository->changePermission($id, $permission)) {
$newAuthor = true;
}
header("Location: authors");
}
//UserRepository
public function changePermission($id, $permission)
{
$table = $this->getTableName();
$stmt = $this->pdo->prepare(
"UPDATE `{$table}` SET `permission` = :permission WHERE `id` = :id");
$changedPermission = $stmt->execute([
'id' => $id,
'permission' => $permission
]);
return $changedPermission;
}
// authors.php / the view
<?php if(isset($newAuthor) && $newAuthor == true):?>
<p class="error">Author has been added.</p>
<?php endif;?>
How can I achieve that $newAuthor will only be set to true when the function that updates the database has been successful and the message to be displayed in the view?
EDIT
I tried it with returning $changedPermission in the UserRepository. It might be wrong because it hasn't changed anything.
You can either check the permission before changing it and see if there's a difference, or just check if the UPDATE request worked successfully.
Since the prototype: public PDOStatement::execute ([ array $input_parameters ] ) : bool
You can check if the request has been successful by verifying the return value of the execute function like that:
$result = $stmt->execute([
'id' => $id,
'permission' => $permission
]);
if ($result == FALSE)
echo 'ERROR';
else
echo 'ok';
Also directly put $newAuthor = $this->userRepository->changePermission($id, $permission); in permissionAuthor function.
But one more thing, I don't see where you are calling your permissionAuthor function in your code ? Are you sure it's executed ?

Laravel auth, disable password

I am trying to disable the password verification system from my laravel website. I want to login my users using only their first name and last name. Form wise and register wise and database wise, password field has been removed completely. But in login controller, i am having some issues, it does not seem to work. Here is my code:
public function login(Request $request)
{
$first_name = $request->first_name;
$last_name = $request->last_name;
$user = User::where(['first_name' => $first_name, 'last_name' => $last_name])->first();
if (!$user) {
return redirect()->back()->withInput($request->only('first_name', 'last_name'))->withErrors([
'first_name' => 'We could not find you in our database, if you think this is a mistake kindly contact the site administrators',
]);
}
Auth::login($user);
return redirecte('/');
}
in the above code, i am getting the error message
We could not find you in our database, if you think this is a mistake kindly contact the site administrators
regardless of what info (true of false) i insert in my form.
Yes thank you #laravel levaral for answering, but i found out the problem.
I am going to quote a user from laracasts
If you're going to group multiple where clauses into a single where(), each needs to be it's own array, within an array. You're sending a single array. You're also using =>, which isn't correct. The parameters for each where statement are separated by commas.
so for whoever wants to see the new working code:
public function login(Request $request)
{
$first_name = $request->first_name;
$last_name = $request->last_name;
$user = User::where('first_name', $first_name)
->where('last_name', $last_name)
->first();
if (!$user) {
return redirect()->back()->withInput($request->only('first_name', 'last_name'))->withErrors([
'first_name' => 'We could not find you in our database, if you think this is a mistake kindly contact the site administrators',
]);
}
Auth::login($user);
return redirect('/');
}
First of all, you have to check either the first_nameand last_name matches the database.
$user = User::where(['first_name' => $first_name, 'last_name' => $last_name])->first()
You have a problem in above lines.
public function login(Request $request)
{
$first_name = $request->first_name;
$last_name = $request->last_name;
$user = User::where(['first_name' => $first_name, 'last_name' => $last_name])->first();
if (!$user) {
return redirect()->back()->withInput($request->only('first_name', 'last_name'))->withErrors([
'first_name' => 'We could not find you in our database, if you think this is a mistake kindly contact the site administrators',
]);
}
Auth::loginUsingId($user->id);
return redirecte('/');
}

Laravel Confide ( Ardent )password confirmation works on save but not on update

I know there are many questions about something similar, but I couldn't apply any solution to my problem.
I'm using Laravel with Confide, which uses Ardent to validate.
I don't have any problem saving users, password_confirmation is removed when inserted to the database (as it should) and users are inserted correctly.
The problem comes when trying to UPDATE a user. If I try to validate the user, I get an error saying that user is not unique:
public function storeProfile()
{
$user = $this->currentUser;
$user->password = Input::get( 'password' );
$user->password_confirmation = Input::get( 'password_confirmation' );
//$user->updateUniques();
$user->save();
dd($user->errors()->all());
}
string (31) "username is not unique"
string (28) "email is not unique"
If I try with $user->updateUniques() (as I saw in another question here in SO as a possible solution), it doesn't complain about uniqueness, but about password_confirmation not being present in the DB (so it's NOT removing it prior to inserting it).
public function storeProfile()
{
$user = $this->currentUser;
$user->password = Input::get( 'password' );
$user->password_confirmation = Input::get( 'password_confirmation' );
$user->updateUniques();
dd($user->errors()->all());
}
Column not found: 1054 Unknown column 'password_confirmation' in 'field list' (SQL: update `users` set `password` = y$wyNl2xMNJqL0mY4X766EfOvO.IKICyDfXckS1cas1Psj1TLwpJZWu, `updated_at` = 2014-03-07 17:13:04, `password_confirmation` = 123456 where `id` = 1)
I've tried with
public $autoPurgeRedundantAttributes = true;
public $forceEntityHydrationFromInput = false;
public $autoHydrateEntityFromInput = false;
As I saw in Laravel/Ardent/User model editing + saving but it's not working either. Thanks in advance for your help.
Ok, it turns out the reason I was getting an error was because I was using both updateUniques() and save(). This works:
public function storeProfile()
{
$user = $this->currentUser;
$user->password = Input::get( 'password' );
$user->password_confirmation = Input::get( 'password_confirmation' );
$user->updateUniques();
if ( !$user->errors()->all() ){
return Redirect::action('UsuarioController#editProfile')
->with('success', 'Success!!');
}
}

How to get last id for relationships in a transaction between models in Codeigniter?

I have this code inside a function in user-controller. I'm, using Codeigniter.
$name_user = $this->input->post('contact-company');
$company_name = $this->input->post('name-company');
$company_orgnr = $this->input->post('orgnr-company');
$phone_company = $this->input->post('phone-company');
$email_company = $this->input->post('email-company');
//Insert a a new user
$user_info = array(
'username' => $email_company,
'name_user' => $name_user
);
$company_info = array(
'name' => $company_name,
'orgnr' => $company_orgnr,
'phone' => $phone_company,
'email' => $email_company
);
//Insert a new user in db
$query_insertuser = "START TRANSACTION";
//Get sql for inserting a new user
$um = new Usermodel();
$query_insertuser .= $um->getSQLInsert($user_info);
//Get sql for inserting a new company
$cm = new Companymodel();
$query_insertuser .= $cm->getSQLInsert($company_info);
//Get sql for inserting relation between user
//and company (How do I get ID of user and ID of company to use?)
$upm = new Userprofilemodel();
$query_insertuser .= $upm->getSQLInsert();
$query_insertuser .= "COMMIT";
//Do the atual insert
$um->insert($query_insertuser);
It's used for handling a registration through a form. (Validation is done through the form-validation libray)-
Users with username, name_user is stored in a users-table
Companies are stored in a companies-table
Relations between companies and users are stored in a
userprofile-table
I think the code is kind of self-explainatory, but I'm not clear in how to insert the relation in the userprofile-model. I do need the last inserted id for user and the last inserted id for company, but in my code I don't have that because I don't actually insert any users or companies before calling $upm->getSQLInsert();
Or am I doing this incorrectly? Please give me any pointers...
I was obviously not doing this correctly. In case anyone care to bother this is my solution:-)
User-controller (part of it)
//Insert a new user in db
$um = new Usermodel();
$um->startTransaction();
$user_id = $um->insert($user_info);
$cm = new Companymodel();
$company_id = $cm->insert($company_info);
//Insert a new user-profile for newly inserted user
$upm = new Userprofilemodel();
$userprofile_info = array(
'user_id' => $user_id,
'company_id' => $company_id
);
$upm->insert($userprofile_info);
$um->endTransaction();
User-model (part of it)
public function startTransaction() {
$this->db->trans_start();
}
public function endTransaction() {
$this->db->trans_complete();
}
public function insert(array $user_info) {
$this->db->insert('user', $user_info);
return $this->db->insert_id();
}
Company-model (part of it)
public function insert(array $company_info) {
$this->db->insert('company', $company_info);
return $this->db->insert_id();
}
Userprofile-model (part of it)
public function insert(array $userprofile_info) {
$this->db->insert('user_profile', $userprofile_info);
return $this->db->insert_id();
}

Categories