Getting Additional Field From Different Table - php

I saw the following post and I'm trying to use it in a way for my queries however I'm using two different tables.
I have my logins table which uses the logins_model and I have the users table.
Inside of the logins table it only includes login information needed to log the user in.
I currently have the email address as the way for the user to login with the password field.
In my users table it includes all the personal information about the user.
What I am wanting to do is when the user logs in successfully, they are sent to the control panel where I query the database for users table for their personal information to display, however I also need the email address to display.
I'm trying to figure out how I can gather that email address field when I do the initial query.
Here is the GitHub repo for MY_Model from Jamie Rumbelow.
Does anybody have any suggestions on this? I would certainly appreciate it.
I'm hoping someone else can shed some additional insight that has working knowledge of the MY_Model.
I've still been battling this all day and would like any other suggestions that anybody will suggest.
EDIT :
I've found out that I can can make an additional function however it only puts the email address field into the object instead of the email address with the data from the users table. Is there something I"m doing wrong.
$user_data = $this->user->with_email_address()->get_by('user_id', $user_id);
public function with_email_address()
{
$this->db->join('logins', 'logins.user_id = users.user_id');
$this->db->select('logins.email_address AS email_address');
return $this;
}

If you are going to provide a function in the model to return all the information with the email address, you must select all the fields for the JOIN.
Try this:
public function with_email_address($user_id)
{
$this->db->join('logins', 'logins.user_id = users.user_id');
$this->db->select('users.*');
$this->db->select('logins.email_address AS email_address');
return parent::get_by('user_id', $user_id);
}

Instead of querying your other table, you can save the users email address in a session variable when the user logs in. This way you won't need to query your database multiple times for just one variable.
Here you can find additional information about sessions:
http://ellislab.com/codeigniter/user-guide/libraries/sessions.html
If you still consider using multiple queries, you could write a function in one of your models which will return an array with the data of the user.
EDIT: You can also join the results in one Query, this way you only need one query.
Here is a good start:
http://ellislab.com/codeigniter/user-guide/database/examples.html

Related

login page check user from 2 different tables

I have a site which has two types of users that are stored in two different tables one is saved as a user and the other one as a business in the user table its columns are as follow:
first_name, last_name, user_email,user_pws
And the business columns is as follow:
business_name, business_email,business_pws
I want to make a PHP coded login page that if the user enters email and password the login system will check which one the user is. how can I do this? my current code is a simple login form.
MAY LEAD TO SECURITY RISKS
this was just for what the user here was asking
It's a little weird because you have to make sure that emails are unique keys however making them a unique key in each table would still mean someone could make one in each table. I'm not sure if that's what you're getting at and I guess to counteract that you could have a table of used emails....
But to get to your question, IF the emails do not overlap between tables I imagine some SQL statement such as:
select users.user_email, business.business_email from users,business where users.user_email='foo' or business.business_email='foo';. I'm not too great with SQL yet either but you can mess around with something like this and I'm sure it'll get you on the right track.

Best way to query in CodeIgniter

I tried to look for an answer of this but I have a question about filtering with codeigniter. The code below checks if the administrator is logged in shows every idea(brain) status in the query, which could be public or hidden, otherwise, it only shows the ones with public status.
I want to change this to keep showing the ideas with public status to everyone and only the ideas with hidden status of the person that is logged in.
if($this->ion_auth->is_admin()) {
$brain_status = null;
}
else {
$brain_status = $brain::PUBLIC_STATUS;
}
$brain_collection = $this->em->getRepository('Entities\Brain')
->getListPerPage($brain::BRAIN_PER_PAGE, $page, $brain_status);
I tried using
$this->session->user_id and I don't know how many other syntaxes and I can't get it to work, every idea has a field in the database where the user_id is logged when the idea is created to accomplish this.
Any help is extremely appreciated
to complete this Q/A pair, I'm transforming my comments into an answer:
the requirement:
admin sees all ideas: public and hidden, regular user only sees public ideas
the solution:
create a database column "published", associated to each idea (and consequently user_id). Now you can show in your view all records which match published=1 to everyone and those published=0 only to whom where user_id matches (or admins).
If a user is logged-in (you set a session with their user_id) and if this user_id matches the user_id of the idea then you show it. It has the advantage that you could implement for a user to be able to publish or un-publish their own idea, without admin intervention, only viewable to themselves, unless published by admin.

PHP Use multiple accounts for one MySQL query

I have a new website that does the following for account creation:
First, it checks in the users table of the Users db if there is an account with that name
Next, it INSERTs name into the users table
Finally, using a different account, it creates a table named after the user, using a statement like: CREATE TABLE $name LIKE template
However, this creates some issues. Occasionally, table creation fails, so I have a "ghost user" that lacks a table. Ideally, I would have the account which accesses the User database be separate from the one which accesses the other db with all the tables.
How could I do this? Could I have one account with permission ONLY to run stored procedures in both DBs? If I did the last option, how could I guarantee completion of both tasks? This is using PHP.
Edit: Thanks for the help in the comments! If someone could summarize the lessons learned & answer this, I will accept your answer. I've taken the advice and completed the change in about 2 hours.

Using the If conditional with sessions in PostgreSQL+PHP

My doubt is quite simple probably, but as a first time for me, this things get confusing
My PHP code is basically a session based one where each person gets texts to display on their page once logged in, but each person gets different ones, and those are saved on a database with their names too
So I need to know how would I make the query on php to get the information belonging to my current user
Something like
if(user=="John"){
display(documents of John);
}
Of course "documents of John" is a field instead
But, hopefully you can get the idea
Thanks
use the user and run a sql query to get document from documents table where username = user. Retrieve the data.
You got the document related to that unique user.

Adding entries into database instantly or after filling out form

I'm working on a PHP/MySQL application that allows for organization members to be maintained within the database. Currently, upon clicking on a "Add Member" span, I insert a blank entry into the database and return the created ID to PHP. Upon receipt of a valid ID, the application user is redirected via jQuery to an edit page that refers to the newly-created member.
As far as I can tell, this has the following advantages/disadvantages:
Advantages
Can instantly associate purchases/payments with a member upon submitting a jQueryUI dialog, since I already have the ID of that member.
Unifies what would have been separate add/edit screens, so easier maintainability on my side.
Disadvantages
There is a high possibility that I will have stale entries. That is, someone could click on "Add Member" multiple times and not save the new page, therefore causing entries to remain blank.
Not able to enforce as many constraints in the table, since I need to be able to accept NULL for all of the columns.
Am I thinking of all of the scenarios/advantages/disadvantages? Should I make a separate page for adding members, or is it better to accept the stale entries, and possibly add a few checks when I fetch all members to make sure that I'm not displaying a stale entry?
My database function for adding members currently:
public static function addMember()
{
$q = 'INSERT INTO ' . MemberTable::TABLE_NAME
. ' (' . MemberTable::ID
. ') VALUES (null)';
try
{
$db = new DBConnection();
$toRet = $db->execute($q);
}
catch(Exception $e)
{
error_log($e->getMessage());
$toRet = -1;
}
if($toRet > 0)
{
DBSystemEvent::logMessage("Added new member with ID $toRet");
}
unset($db);
return $toRet;
}
EDIT 1: After rereading the question, I need to clarify that members and users referred to in the first paragraph are different. Users refer to the person logged into the application. Members are not able to log into the application. This is similar to a hospital application (patients may not log in or edit their own information; only application users such as nurses or doctors may log in and edit information).
EDIT 2: While none of the given answers completely fit my problem (since I may have to insert into the database without knowing an ID), I decided to accept an answer based on how my question was worded (since making it any more specific may cross into too-localized territory).
It's a common problem - you need to know the ID before INSERT, but it's known only after. So there is only one adequate solution: use GUID (http://en.wikipedia.org/wiki/Globally_unique_identifier) instead of autoincrement ID. Generate guid from PHP code, for example com_generate_guid(), and do not preINSERT empty rows at all. And make relations between tables with GUID fields.
It's little bit unclear to me what exactly is the workflow of your site.
If user comes to you page then I assume that he must login from where you get his ID. If he is new user then he is redirected to userdata.php?id=0 where he enters his data. After submitting you should check if $id=0 and if the user with the same username/id/.. exists (SELECT... WHERE ID=xxx) and warn user to change his username. If no match is found then you can do INSERT and obtain the new ID.
If in future user wants to change his data then after login you can direct him to userdata.php?id=123 (where 123 is his ID). Then you can check if $id>0 and do UPDATE.
If you can, switch to postgresql. This will allow you to use a sequence to provide you with a unique ID without entering empty entities into you database.
Funny enough one of my clients is using the same approach you haven chosen and so far this lead to a lot of maintenance and work load overhead to weed out the empty entries from the db.
If you cannot use a database that offers sequences consider using an otherwise empty table which only atomically gives you unique ids. That way you can already start using the id to prepare relations on the client side and then enter them in bulk into the db when the member is finally created.

Categories