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.
Related
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)
i have 2 tables on db.
First table:
users
id|name|mail|password|group
second table
scores
id|name|score
The idea is get the name from users using the id (this id already know because i get that by php), then insert a score in table scores using the name obtained by a id.
I suppose that can i do with a inner join between users and scores.
How can i do that?
Insert into scores(id, name, score)
select ID, name, score(that you can pass)
from users
where id = (parameter pass by PHP)
Agree. You can simply use this:
INSERT INTO scores values (null, (select name from users where users.id= 1),100);
Replace the score and id with the values you get.
Assume you have the auto increment for your ids.
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;
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.