How can i insert data in table with a join sql? - php

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.

Related

Linking SQL Tables - Issue

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)

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.

Insert the data from the data select in other table in PDO and MYSQL

There are two table
user:
id
uid
board:
id
message
user_id
The problem is , I got the uid now , and I would like to insert data to board, but I want to simplify all things to one query, so I tried
"INSERT INTO board (message,user_id) VALUES (:message, SELECT id FROM users WHERE uid=:uid)";
but in this way is not working, would anyone kindly teach me the right syntax? Thanks
Enclose SELECT in parentheses like this
"INSERT INTO board (message,user_id) VALUES (:message, (SELECT id FROM users WHERE uid=:uid))";
You either use a VALUES clause or a SELECT clause, not both.
INSERT INTO board (message, user_id)
SELECT :message, id
FROM users
WHERE uid = :uid

INSERT INTO logins (ID) VALUES ((SELECT COUNT(ID) FROM logins)) Not Working

I am trying to insert data into my table such the id field is one more than my previous max id. For example, if I had 21 users registered and another one was added, then that users' id would be 21 (note my ids start at 0).
I tried this:
mysqli_query($con,"INSERT INTO logins (ID,UserName)
VALUES ((SELECT COUNT(ID) FROM logins),'$username')");
This is the error message:
#1093 - You can't specify target table 'logins' for update in FROM clause
I also tried this which works:
$result=mysqli_query($con,"SELECT ID FROM logins WHERE ID=(SELECT MAX(ID) FROM logins)");
while ($db_field = mysqli_fetch_assoc($result))
{
$id=$db_field['ID']+1;
mysqli_query($con,"INSERT INTO logins (ID, UserName)
VALUES ('$id','$username'");
break;
}
But I am looking for a way to do this with one command. Thanks in advance.
I suggest you make id the primary key of the table and enable AUTO_INCREMENT. You can then remove the id column from your query, and increasing id's will be added automatically.

Insert a new field when primary keys from two separate tables match

I have two tables one is called Players and the other is importdata. The importdata table consists of two fields the player id (PID) and the photo (Photo).
In the Players table I created a column for the Photo field to be imported into. What I would like to do is take the Photo field from the importdata table and insert it into the photo_high field in the Players table where the PID fields match.
I thought something like this would work, but it says that there is an unknown column.
INSERT INTO (`photo_high`)
SELECT PID, Photo
FROM importdata
WHERE Players.PID = importdata.PID
Can this be achieved with an SQL statement or do I have to write some kind of script? Any guidance would be great.
Players
PID
photo_high (empty)
importdata
PID
Photo (full of content)
I think you want update rather than insert:
update Players p join
ImportData id
on p.Pid = id.pid
set photo_high = id.photo;
insert creates new rows in a table. update changes values in existing fields.

Categories