i have 2 tables one name unete_info and unete_confirmation. unete info has all basic information for a registry and unete confirmation has a field name joind_id which is generated when user successfully registered.
join_id is located in both tables as well name last_name and email, email is the unique value to validate both tables. i need to check whithin the tables every hour to see if the user the register successfully if not get a notificacion i have a user pending.
meaning my query needs to check within both tables and if user has join id empty in both tables after an hour send a notfication. but so far i need first how to get the user that dont have join_id, so far i came up with this.
SELECT unete_info.name,unete_info.last_name,unete_info.age,unete_info.number,unete_info.language,unete_info.submitted,unete_confirmation.join_id
FROM unete_info
INNER JOIN unete_confirmation ON unete_info.email=unete_confirmation.email
im missing how to check every hour and how to get users that join_id is null(empty)
Related
I want to create a form where the logged author can fill a textarea and then select 1 or more system users from a checkbox
So when every user logs in he Will see that record only if he has been selected in the checkbox before.
How can i do it?
Must i save values of the checkbox in a single record and then split to set the correct data set?
And then how can i show the whole records to the current user as i want him to see a list of records where he is the author or where he is among the users allowed to set that very same record
I know this can be a generic request but the matter is i dont understand how i could do it, so the answer i look for is not "give me the code" but"explain me the main steps i must follow", someone can give me some hints?
How about creating a table named "record" with the following structure:
id author_id allowed_user_id text
Now, when the author with id 1 selects two users with ids 2 and 3, you insert two rows into that table, one for each allowed user
id author_id allowed_user_id text
1 1 2 just a test
2 1 3 just a test
To show the current user the records he authored
SELECT * FROM record WHERE author_id = current_user_id
and records he's allowed
SELECT * FROM record WHERE allowed_user_id = current_user_id
In an application I've been building, I have a users table. The users represent a firm's staff. I also implemented a role/permission system. Permissions are assigned to roles (staff, manager, director, etc.), and roles are assigned to users. Permissions prohibit the use of certain actions in the app.
Now, I'm introducing another user type: customer (will also be able to log into the app). The customer fields are different from the user fields, so I can't store the customer information in users table (and create a role for customers).
(Normally, just creating a customer role for the customers and store the custumers in the users table would be fine, but since the fields are different, I don't think that's an option.)
First, I thought about creating a seperate table for the customers, but I'm not sure if that's okay, because when someone tries to log into the app, I have to check two tables (users and customers) for the log-in credentials. What if I introduce a third user type? Check three tables? That doesn't seem practical/efficient.
So I thought about seperating the log-in credentials from users, and ended up with three tables:
users will hold log-in credentials
staff will hold staff meta/profile information
customers will hold customer information (same as staff)
This way, I can introduce many different types of users. And if I know what I'm looking for, I can get the record. For example, say I want to get/query a staff, I can just do:
SELECT * FROM staff
JOIN users USING (user_id);
The problem is how do I query users when I don't know what I'm looking for? I mean, the logged user can be a staff, customer, etc. I need to do something like this:
SELECT * FROM users
JOIN [specific_table_name] USING (user_id);
How do I know which type of user just logged in? I could store the user type (the target table name?) in users, but will it help in a (single) query? I mean, find the user in users (using credentials), and then join the user information from another table (staff, customers, etc.)?
Currently, I'm thinking about doing two queries. First is to get the user (credential) record, and the second is to get user (say, profile) information (using a field type from user record).
Of course I'll be doing this in PHP. For example (not real code):
$email = "someone#example.com";
$user = get_user($email); // SELECT * FROM users WHERE email = "someone#example.com"
switch ($user["type"]) {
case "staff":
$user = get_staff($email); // SELECT * FROM staff JOIN users USING (user_id) WHERE email = "someone#example.com"
break;
case "customer":
$user = get_customer($email);
break;
// ...
}
// how it's done doesn't really matter. the thing is "type" needs to be checked to get the corresponding user info
Is this best way to handle this? Is there a way to make the queries in SQL (without resorting to PHP)? Like JOIN after WHERE? Or make two queries in one (save the first query result, and use a column value from the first result as a table name in the second query)?
Mentioned tables:
I'm still researching, and I found out that what I'm doing with the tables is called (?) "Class Table Inheritance". It seems clever for non-login related entities (when going from child to parent; e.g. staff -> user), but in reverse (parent to child, e.g. user -> staff|customer|etc.), it seems problematic. Since I figure these things as I go, I'm stuck at the moment.
One solution that just (while typing) occured to me is to use different log-in forms/pages specific to user types. The form/page could let me know the user type beforehand, but I rather not use this method. So, single log-in form for all users.
I'm calling users as the base/parent table, and the stuff, customers, etc. as the child tables, because first insert happens at users, and child tables use user_id from users.
You could create a union and use an alias in the query to define different fields mapping for the same name, if there is no field in one table you just cast the alias for an empty field:
SELECT (staff.name) as name FROM users
left JOIN staff USING (user_id)
UNION
SELECT (customers.first_name) as name FROM users
left JOIN customers USING (user_id);
If the userId is a PK/FK it will be only returned by one table.
I would set all users login info and email to the parent table (customers and staff) and two tables with foreign id key for other data, with users.type as you suggested. It could be done with one query, too and Switch condition is not needed:
SELECT users.email,users.type,staff.field2,customers.field3 from users
LEFT JOIN staff ON(users.type='staff' and users.id=staff.uid)
LEFT JOIN customers ON(users.type='customers' AND users.id=customers.uid);
I'm trying to make an Address book with different users in which they can log in with their username and password. And they can store the information like contact id, first name, last name, phone etc. I want each user to have his own address book.
Can someone please explain how I create different address book for different users.
Thanks a lot.
Create a database. take a primary id to each user or provide unique id.Then create another table fill the fields that you want(address book).Match the user who logged in and and with that another table(foreign key relationship).
When you login ..it checks with db then allows.After that you have to use insert ,update keywords for further process to put data in db.That particular unique id/primary key is used to fetch the related data(address book fields) of that user.
It is actually not a good question for StackOverflow but I will still roughly explain it to you.
You will need two different tables for this. The first one will be used for login details and the second one will record user's address book.
For example:
Table auth will have 3 columns id, username, password
Table addressBook will have contact id, first name, last name, phone etc and also have one more which will be called userID
Whenever any user eneter the data in addressBook their userID from auth table coloum id will be stored along with it. Now you can display their own data to the users.
If you have anymore question ask in comment here.
I have a website built with PHP and mysqli and now I am building administration panel for this website. I am facing some difficulties for querying and displaying referral data I do not have any idea how to do it.
I have a table called user_registration and the fields include, user_name, email, password, referrer. Whenever any of the registered member is referring others the referrer username will be saved in the field "referrer".
Now what I want is to fetch only the rows of the members who has referrals (means referred by my registered members) and also want to count how many referrals a member have and echo it.
For example: I have 20 registered members and from it 5 members have some referrals so I want to query and echo those member's username who has referrals and also count how many referrals they have:
Member's Username Total Referrals
user7 8
user6 6
user1 5
user9 3
user5 2
My solution would be. Make a new column in your user table like "totalreferrals". In this way you can easily keep track of the amount of people they have referred. You could also make a new table "referral". In here you would save the referrals name, the new persons name and a timestamp or something. Now it is even possible to make queries for different periods.
I think that you need to rethink about your database setup, instead of trying to make way to complicated queries, to make your database work.
I am just giving an idea in short which may help you to complete your project -
Suppose your site URL is http://yoursite.com.
1) Whenever a user is registered, you can generate a random key for that user and save it in the database for that particular user. e.g. user1 => abc123
2) Now, you can tell your user (may be on a page after their successful registration) that his/her referrer id is http://yoursite.com/?ref_id=abc123 (by appending it as a new parameter)
or if your register page is http://yoursite.com/register then http://yoursite.com/register/?ref_id=abc123
3) Now, they can share this link to their friends, colleague etc to whomever they want.
4) Now, suppose if a person (may be his friend to whom the user1 referred) clicks on the above link. He will land on your site with the parameter 'ref_id' appended.
5) Just using $_REQUEST['ref_id'] or $_GET['ref_id'], capture its value and store it in a SESSION variable. This you can do on the main page of your site e.g. index.php
6) Now, if that user does the registration, you can make an entry in the database that he has been referred once by the user who has referrer id abc123 i.e. user1.
7) So, you can add count = 1 for user1. If more people come with the same value for the 'ref_id' parameter, you can keep on increment the count.
8) If no such parameter is exists when user lands on your site, then that means he has not referred by anyone. So, that means he is the first kind of user i.e. user in point no. 1).
9) You may also need to take care of some validation part at some places in this.
EDIT:
SELECT user_name, referrer FROM user_registration WHERE referrer > 0;
Assuming that you have a proper insert query ready which gives you the result as you shown the table in your question.
I already have a simple registration system in place using php and mysql. It operates well enough. However, when people visit my site and register, I would like for them to register as part of a particular group. So, I was thinking that registration would happen like this:
Visitor lands on index.php, clicks on "Group Registration" link.
Visitor supplies group name and group password. [A new table is created for that group where all user data will be stored for that particular group]
Visitor then is prompted for typical registration data--name, email, etc.--and that data is stored in the newly created group table.
Any subsequent visitors associated with that group would click on "User Reg"
The visitor would be prompted for group name and password
If correct, then he would be prompted for typical reg data, to be stored in his group's table.
What I don't know how to do is implement the group authentication prior to allowing user registration. Can someone help me with that?
If the visitor is entering a group name and password, then you can authenticate the same way you are doing the users. You just need to first ask yourself if the group name needs to be unique or the group/password combination.
As for your idea to add a new table for each group, that is a bad idea. Imagine if you have 100 groups. Then you will have 100 tables just for groups. If you get up to 1000 groups, then you will have 1000 tables. Try managing that. It will get really frustrating really fast. Instead, what you should do is to first create a "Group" table with all the associated data (group name, password, etc). Then add a field to your User table that will hold the associated id from the Group table. That way, whenever you look up the user, you can easily check what group the user is in simply by joining the two tables rather than trying to figure out what table to look at as in your original plan.
What you want to end up with is a table for your users and another (single) table for your group information. The user table will have a foreign key field to link it to a group. When a user joins a group, you will enter a value in that field. Users not in groups will have a null value in that field. If users can create groups, they will simply be adding a new row to the groups table.
If your users can be in multiple groups, set up your tables like this.
USER
- id
- username
- password
- etc...
GROUP
- id
- name
- password (?)
- etc...
USER_GROUP_CR
- fk_user
- fk_group
The USER_GROUP_CR table is a "cross reference" or "link" table that will allow you to create a many to many relationship. This way you can have users in multiple groups without creating extra tables. When a user joins a group, add a row to the USER_GROUP_CR table with the id of the user and the id of the group. You can query this table to find out which groups a user belongs to, or to find out which users are in a group.
You should not create a new table for every group.