I have been working on a website where I am storing user data like, userid, username, password, email, rank, fname, and lname. I have created a registration page that inserts this data and a login page that requires your username and password. This creates a session and in the session with a username variable. How do I use the username to pull from the sql to find, for example, their email? I have tried looking this up but have found nothing. If someone would not mind linking me to a tutorial or something that would be amazing.
Thank you very much,
John Finberg
First search wether username is exists in the database table or not with below query.
select count(*) from Employee where UserName=#username.
if username is already available in the table then get the EmailId by using below query.
select UserName, EmailId from Employee where UserName=#username
Create a stored procedure that will take the username as parameter and return user's email address:
CREATE PROCEDURE [dbo].[uspGetUserRole] #userName VARCHAR(255)
,#emailaddress NVARCHAR(255) = '' OUTPUT
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
BEGIN TRY
SELECT #emailaddress = u.emailaddress
FROM [User] u
WHERE u.UserName = #userName
END TRY
BEGIN CATCH
THROW
END CATCH
END
You can then call this stored procedure to get the email address for the username that you pass:
DECLARE #emailaddress NVARCHAR(255)
[dbo].[uspGetUserRole] 'domain\username1', #emailaddress OUTPUT
SELECT #emailaddress
1.User can't login with there user name because username is may be a same but use of user id or user email that must be a unique.
2.on login retrieve a user data by user id or email stored there user name and user id in session.
3.now you can retrieve a user data by user id.
Related
I'm working on a registration app in android studio(with PHP and MYSQL). In my database I have two tables:
table1 (For saving firstName and lastname)
and
table2 (contains 100 activation codes).
In register activity I have 3 fields: FirstName, LastName and activation code.
Before the user data is inserted into table1, this is very important that activation code must be checked with data in table2. If this value exists in table2, then the data is inserted into table1(registration done) and after successful import, delete the same value(activation code) in table2. If data does not exist in table2, then data is not inserted and show a toast error.
I'm new at this, so bear with me. Thank you in advance.
This solution is in php
$res = mysqli_query('SELECT * from activation_table WHERE your_code = "'.$activation_code.'"');
if(mysqli_num_rows($res)>0){
mysqli_query('INSERT INTO user(firstName, lastName) VALUES("'.$fname.'","'.$lname.'")');
mysqli_query('DELETE FROM activation_code WHERE your_code = "'.$activation_code.'"');
}else{
// your error message
}
I hope this solves your problem!
I am wondering if it is possible to insert from another table (which I have managed to do) whilst also inserting a VALUE of a variable from the current php file?
I am aiming to get the user ID from another table, which I have gotten from selecting the email from the user input. I then need to insert a hash which is automatically created via a variable.
This is my current code that gets the correct id from the users table.
$forgot = $pdo->prepare("
INSERT INTO
forgot (
user_id
) SELECT
id
FROM
users
WHERE
email = :email
");
Now I just need to insert the VALUE of :hash too.
Would this need to be done with a separate query?
Thanks.
Try the following:
INSERT INTO
forgot (
user_id, hash
) SELECT
id, :hash
FROM
users
WHERE
email = :email
So, I have a table A that each time a user sends an image, a record is created storing the time it was uploaded, the username of the user and the image number out of all the images uploaded over time.
I need to make a second table B that will store the amount of images uploaded per user and the user name. I need this table B to be updated when a new entry is generated in A.
I found that a trigger function can be created, nevertheless I'm having a rough time finding an example that will suit my needs.
Does anyone know a way of doin what I want?
Just update b table with a select count of total inserted records on a from current user NEW.userid (userid is your column name or whatever name you have there, and NEW is a fixed mySql reference for the current values to be inserted):
CREATE TRIGGER img_sum AFTER INSERT ON a
FOR EACH ROW SET b.total = (SELECT COUNT(*) FROM a WHERE a.userid=NEW.userid)
WHERE b.userid = NEW.userid;
From what you have described i don't think you need a second table. You can just count the number of time a user name has occurred, and you will get the number of images that user has uploaded.
You can get the count doing something like that
SELECT COUNT(DISTINCT username) FROM table_name;
If you still need to create 2 tables, you might want to take a look at procedures and how they work.
Let's say we have 3 tables for this case:
- users(id, username, email ....),
- user_images(id, userId, image_num, date_uploaded)
- user_images_count(id, user_name, images_count)
The user_images_count is initially empty. We have to fill it up by such query:
INSERT into user_images_count(user_name, images_count)
SELECT (select username from users where ui.userId = id) as username, count(userId) as counter FROM `user_images` ui group by ui.userId;
Then, we must immediately create the trigger that will process every INSERT operation into user_images table.
CREATE TRIGGER `count_user_images` AFTER INSERT ON `user_images`
FOR EACH ROW begin
declare u_name tinytext default "";
set u_name = (select username from users where id = NEW.userId limit 1);
if(u_name != "") then
update user_images_count set images_count = images_count + 1 where user_name = u_name;
end if;
end
This two queries (user_images_count fulfillment and trigger creation must be performed in one transaction, one by one).
I've created similar triggers on my local databases. They work pretty good. )))
I have a line in MySQL with information like the username and email address and I would like to find the id of the row based on the username field. Any ideas on how to find the id based on the username field?
SELECT id FROM users WHERE username = 'NStorm';
It is very basic sql:
select id from your_table where username = 'foo'
I have a database from which I would like users to retrieve information from a certain table called "entry" based on their username.
So I want user1 to login and then a select statement be created to take the username, look it up in the username table, and then used the stored value for the person's name (which is one of the columns in the user table) to run a query to show all records for that person.
Is this possible?
thanks in advance
EDIT:
Sorry the question was so badly formed.
Basically I have a users table which holds user login details with the fields - studentName, Site, Username , Password
I then have another table called entry which holds records for all users with the following fields - entryID, date, studentName , field1 , field2, field3 etc
What want to do is for the user to login as they do now and a query to be generated based on the Username to get all records for that particular student.
Does this give a better idea of what I am try to get?
if its Oracle, then you can do:
SELECT a,b,c
FROM entry
WHERE username = user -- user is an Oracle keyword that returns
-- the oracle user name of the user running the query.
Possibly way off but asuming a table called User with an Id, Name & UserName and a related table called details with a UserId and other columns with the details the below simple join will work. Also the string 'AUserName' is the value passed in from your app.
SELECT User.Name, Details.*
FROM User
INNER JOIN Details
ON User.Id = Details.UserId
WHERE User.Username = 'AUserName'
But this is a guess based on your question, add more details and you'll get a better answer