INSERT value from another table and variable - php

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

Related

FOREIGN KEY doesn't complete column after INSERT (PHP)

I have 2 tables. In the first i have id_user (that is primary key),username,name,password and in the second i have id_post(primary key),post,username,view,id(foreign key that references id_user).
After i use the query:
$query ="INSERT INTO posts (username,post,view) VALUES ('$username','$postare','$isprivate')";
It completes my column id with NULL. I want in the column id to have the id_user of the user that make the query.Not NULL.
You need to look up the value to get that in the data. So, using your construct the query would look something like this:
insert into posts (userid, post, view)
select u.userid, $postare, $isprivate
from users u
where u.username = '$username';
Notes:
Do not store both userid and username in the posts table. Only store the username in the users table and use userid to get the name when you need it.
Don't munge your query string by putting values directly in it. Learn to use parameterized queries.
view is a really bad name for a column, because it is an SQL keyword.

Retrieve User Info Via Username From sql

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.

How to send data to two tables with same user ID as primary key?

I am totally new to PHP and I'm trying to create a registration page. To register, a user has to create a username, password, email, which are put into a table called users.
They also enter address details which is put into a table called customer_info.
In both tables I have created an auto increment, primary key called 'user_id'.
When the form is completed it fills out and enters the data, but the data is not banded and so there are two user_id, one in users and one in customer_info.
First I create values (from the post) that have been entered and assign them to variables. Then I put the variables into my table using the following query:
$result = mysql_query(
"INSERT INTO `users`(username, password, email) VALUES ('$value1', '$value2','$value3')"
);
and
$result = mysql_query(
"INSERT INTO `customer_info`(firstname, lastname, b_add_num, b_add_road, b_add_town, b_add_pc, p_add_num, p_add_road, p_add_town, p_add_pc) VALUES ('$value4','$value5','$value6','$value7','$value8','$value9','$value10','$value11','$value12','$value13')"
);
How would I set it so that it creates only one user id for both tables (making a connection between the sets of data)?
Is there something missing in my query, that should connect the tables?
Before anything, you should not use mysql_* extension anymore. Go towards PDO or mysqli
Your technique generates two different unique ids. The point is to have only one, so that it can be unique, and link information on that unique id.
The users table is the one with that unique id, user_id, which is your auto_increment column. The customer_info table can also have a info_id unique column, but must contain a user_id column, which will contain the user's user_id, linking the rows together.
It would also be a great moment to add a foreign key to your tables so that integrity of the data won't be compromised.
so after this query:
$result = mysql_query(
"INSERT INTO `users`(username, password, email) VALUES ('$value1', '$value2','$value3')"
);
get the insert id:
$id = mysql_insert_id();
then run your other query with it:
$result = mysql_query(
"INSERT INTO `customer_info`(user_id,firstname, lastname, b_add_num, b_add_road, b_add_town, b_add_pc, p_add_num, p_add_road, p_add_town, p_add_pc) VALUES ('$id','$value4','$value5','$value6','$value7','$value8','$value9','$value10','$value11','$value12','$value13')"
);
I would configure USER_ID as an AUTO_INCREMENT column only in Users table, insert the data into Users table first, then get the ID of the user inserted, using the mysql_insert_id ($connection_id); and use that while inserting data into Customer_Info table. This way, you can leverage the ID generation (sequence) feature of MySQL as well.

Complex Update Sql Statement

I have two tables one table has an id and a username the name of the table is user. I have another table called value that table has an id which is to store the id from the user table. The table valuealso has a column called value and item_id which is used to store the item.
I want to write an update statement that updates the following columns within value id, value, item_id however the values I have to execute the statement is username = $username, value = $value and item_id=$item_id (based on the application)
How can I write an update statement that stores the id (username id), value and item_id
The reason for using an update statement is because that user can change the value within the value column at any time
UPDATE `value` SET `value` = $value WHERE item_id = $item_id
AND user_id IN (SELECT id FROM `users` WHERE username = $username)
be sure to sanitize your database inputs.
this will only update, if you want to create the entry (i assume that user_id and item_id are the combined primary key of your second table), you can use a replace into statement.
please tell me if you need that, because i think it was not the question.
it may be not as efficient but i think that will not really matter, but you can of course fetch the user id first with a separate query.
this will probably be better understandable.
Try this
UPDATE value, user
SET value = $value
WHERE item_id = $item_id
AND value.user_id = user.user_id
AND user.username = $username;

SQL Select statement based on login details

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

Categories