I would like to link two SQL tables together, both the tables have data entered in them through HTML/PHP forms.
The first table is where the user enters all their details into a form (called system), and the second table is for where a user fills out another form for booking (called users). How do I link these tables together? To show the users booking(s).
I don't know how to get the booking table/form to echo out the user's username into the second table to link them together?
I understand that this does not make sense but would really appreciate some help!!!
Here is the first table's SQL for creating a user account:
And, here is the SQL for creating a booking?:
</form>
</body>
</html>
<br></br>
</div>
</div>
</div>
</body>
</html>
Here is the code that links what the user entered for the booking form (system table) to the SQL database.
Two tables can be "linked" together using JOIN. If you want to list the bookings (rows in the details table), with some user information, you can use LEFT OUTER JOIN or INNER JOIN. (The difference is that with LEFT OUTER JOIN you get all the bookings even if some of them don't have a user. With INNER JOIN you get only the bookings where there exists a corresponding row in users.)
If a column has the same name in two different tables, you can use users.user_uid and details.user_uid to refer to them.
SELECT details.*, user.email, user.first, user.last
FROM details LEFT OUTER JOIN user ON user.user_id = details.user_uid;
Then you maybe want to add a WHERE or ORDER BY and perhaps a LIMIT.
In the users table, there is a user_id (INT) and a user_uid (VARCHAR). The details table has a user_uid (INT). In the example, I was assuming that the user_uid in the details table corresponds to the user_id in the users table because these are both INT. You might want to change the name of some column to make it less confusing. Let's assume you want to rename users.user_uid to "username".
Also, you should add a unique index on the user_id and username, assuming that you want both of them to be unique. For the INT, I suggest you make this the primary key with automatically incrementing numbers and the username, just a unique key:
CREATE TABLE users (
user_id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
... ,
username VARCHAR(20) NOT NULL UNIQUE,
...
);
In the details table you probably also want to make the ID a PRIMARY KEY with AUTO_INCREMENT.
A unique key guarantees that there are no duplicates and it also makes lookups on these columns efficient.
If you are just trying to query them together a join should do the trick.
SELECT users.user_first, users.user_last, users.user_email, system.* FROM system INNER users JOIN ON users.user_uid = system.user_uid
In order to store the user_uid value in the system table you need to add a hidden input in your form like this:
<input type="hidden" name="uid" value="<?php echo $_SESSION['u_id']; ?>">
then in your php add
$user_uid = $_POST['uid'];
and insert it when you run your insert query.
INSERT INTO system (date, time, table_layout, user_uid) VALUES ('$date', '$time', '$table_layout', '$user_uid');
Just make sure that your table actually has that column. Once that value is available you should be able to run the above join query.
Let's see. You have two tables, one with user data (users), and one with booking data (details). A "details" row contains an ID of the user linked to that particular row.
Once you know, at least, the ID of the booking, you can get the id of the user like this:
SELECT user_uid FROM details WHERE ID = #specificBookingID
Once you know the user id you can get the user data:
SELECT * FROM users where user_uid = #retrievedUserID
You can do it in one query like so:
SELECT * FROM users where user_uid IN (SELECT user_uid FROM details WHERE ID = #specificBookingID)
Related
I am developing a reservation system for my school project. It is based on PHP and MYSQL.
The system allows a user to register. After successful login, the users can make a reservation which is stored in the following table in phpmyadmin.
User Table
When a user registers, he gets a user_id. When a user makes a reservation, the data is inserted into the table against the same user_id. But when the user tries to make another reservation, there is no way to store the information of the next reservation.
The question is how do I allow the user to make several reservations?
The user_id is my primary key so I understand it is not possible to create multiple records against one primary key.
Do I have to create a new reservation table and link it to user_id through a Foreign key relationship>? But if my reservation table has a primary key, then several records cannot be inserted against one primary key.
Somehow, each reservation must be linked the unique user_id so that the user can check all the bookings under his name.
Each reservation also needs a unique ID which can be used to cancel/update the reservation.
I believe it must be a basic MySQL question. Something like creating a new reservation table and connecting it with user_ID but I am unable to think of a concrete solution.
Your help is very much appreciated.
Thank You.
Make table users
then at least 2 fields id, name
then make table reservations and its One-To-Many relation so you need to put foreign key in reservation(many side)
reservation
id, name, user_id
then to get reservations from user 1 use select with join
SELECT * FROM reservations r JOIN user u ON r.user_id = u.id WHERE u.id = 1
I have a table for users. But when a user makes any changes to their profile, I store them in a temp table until I approve them. The data then is copied over to the live table and deleted from the temp table.
What I want to achieve is that when viewing the data in the admin panel, or in the page where the user can double check before submitting, I want to write a single query that will allow me to fetch the data from both tables where the id in both equals $userid. Then I want to display them a table form, where old value appears in the left column and the new value appears in the right column.
I've found some sql solutions, but I'm not sure how to use them in php to echo the results as the columns in both have the same name.
Adding AS to a column name will allow you to alias it to a different name.
SELECT table1.name AS name1, table2.name AS name2, ...
FROM table1
INNER JOIN table2
ON ...
If you use the AS SQL keyword, you can rename a column just for that query's result.
SELECT
`member.uid`,
`member.column` AS `oldvalue`,
`edit.column` AS `newvalue`
FROM member, edit
WHERE
`member.uid` = $userId AND
`edit.uid` = $userId;
Something along those lines should work for you. Although SQL is not my strong point, so I'm pretty sure that this query would not work as is, even on a table with the correct fields and values.
Here is your required query.
Let suppose you have for example name field in two tables. Table one login and table 2 information. Now
SELECT login.name as LoginName , information.name InofName
FROM login left join information on information.user_id = login.id
Now you can use LoginName and InofName anywhere you need.
Use MySQL JOIN. And you can get all data from 2 tables in one mysql query.
SELECT * FROM `table1`
JOIN `table2` ON `table1`.`userid` = `table2`.`userid`
WHERE `table1`.`userid` = 1
Okay i'm creating a little community website. A user joins my site and he has the option to
enter his informations upload photos add friends and post to his wall. By now i want to know how to implement the album table.
The album has the following attributes:
albumID
albumName
albumCover
albumDescription
albumDate
albumPrivacy enum
userID
username
Now i want to know how to link albums table with the users table. Every user has its ID and username. The usersId is primary key and the username is unique.
1 user can have many albums. 1 album can belong to only 1 user.
How can i do relationships ???
Help please because im not that good at databases and i want to get things done in optimized and secured way.
By now i use usersid and username as attributes in albums table to identify which album belongs to who. like this SELECT * FROM albums WHERE userid='$userid' AND username ='$username'. Where $userid is value from session variable and $username is value from session variable.
Is username in the Users table? If so, then you don't need to worry about including it in the Albums table, too.
The usersId is primary key and the username is unique.
Just to clarify, the userID should be a primary key on the Users table, but a foreign key on the Albums table (as matthewh mentioned). "albumID" is the column that should be the primary key for the Albums table.
Your SELECT SQL can also be simplified, as you should not need username in your WHERE clause. I'm going to assume that users can't have multiple usernames, so only stating userID in your WHERE should be sufficient.
However, if you still want to see username in your Albums result set, you can do this:
SELECT a.*, u.username
FROM albums a
INNER JOIN users u ON u.userid = a.userid
WHERE a.userid='$userid';
Also I feel compelled to mention that you should really parameterize your SQL statement (in your PHP code) instead of concatenating your SQL command string together. That will protect it from SQL injection attacks. It's a bigger danger with user input, so using a session variable should be ok. But it's still a good habit to get into.
The way you've done it is pretty much right. You only need to have the userID column in the album table, because it is a primary key in the user table and thus guaranteed to be unique. Having the username column in the album table is redundant.
You just need to make sure the userID column in the album table is defined as a FOREIGN KEY and of course has an index applied to it.
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
Im having trouble tring to figure out how to save dynamic radio buttons.
I have a project where i have to use radiobuttons instead of checkboxes.
Basically, there is a database table with "roles" in it ... i.e administrator, user, etc etc etc and these are updateable but the user.
On a specific page, these get spit out with a radio button next to them, like so:
<input name="wfa" type="radio" id="wfa" class="radio" value="1" /><label for="wfa">administrator</label>
These go on down the page, for as many entries there are in the database.
What i need to do is save the checked ones to the database, but being dynamic i cant save each value to a individual row ..... any help?
Someone mentioned serialize, i had a look at the php documentation but it didnt make much sense to me.
Cheers.
You would normally have (at least) three database tables: users, roles, and users_roles (a look-up table). The users table and roles table are self-explanatory. The users_roles table would probably have two columns: user_id and role_id.
So for example, they would look similar to as follows:
Table: users
id INT PRIMARY KEY
username VARCHAR
password VARCHAR
...
Table: roles
id INT PRIMARY KEY
name VARCHAR
Table: users_roles
user_id INT
role_id INT
With this database structure, you can then find out what roles a user has with a query like the following:
SELECT
r.*
FROM
users u, roles r, users_roles ur
WHERE
u.id = ur.user_id
AND
r.id = ur.role_id
This will then give you an array of roles assigned to the current user ID, which you can loop over and output a check box (a better solution in my opinion).
$res = mysql_query($sql);
while ($row = mysql_fetch_object($res)) {
printf('<label for="role_%d">%s</label>', $row->id, $row->name);
printf('<input type="checkbox" name="role[]" value="%1$d" id="role_%1$d" />', $row->id);
}
When you submit this, the checked check boxes will then be available as an array under $_POST['roles'], which you can loop over and insert into the users_roles table.
What i need to do is save the checked ones to the database, but being dynamic i cant save each value to a individual row ..... any help?
Why can't you maintain a reference table between roles and users with user_id and role_id? (Let's call this table users_roles_mm.)
When the form is submitted, delete all rows for the current user (user_id) and insert a row with a (user_id, role_id) pair for each checked input (role).
The idea here is that each table—roles and users—are dynamic, as is the reference between them. I.e., users_roles_mm is highly dynamic since it depends neither on the structure of the users table nor the roles table.