foreign keys mysql php - php

I have two table in which I need to submit data. It is a directory listing website on the first table has the common data name etc but I have to manage business hours as well look below
**Users**
Users_Id Name Age AddressId .........
----+------+------+-----------
**users_hours**
hours_id Users_Id day day day .........
----+------+------+-----------
I have to make a foreign key for this but the only issue is how will I get the id of the user dynamically?
As both of these rows will be created together.

So you want to get the last insert id of Users table, for getting the last id you need to use the mysql_insert_id() here.
First make a insert query for Users and use $userId = mysql_insert_id().
The $userId now hold the last id of the Users table, so you can now make a second query for users_hours.
More about mysql-insert-id.php

Related

Get Single record for Each Ids in Laravel

How to get Single Record (Last Inserted Record) for Selected Ids in Laravel.
My Query :
$familymembers=Membersfamilydetails::Where('member_id',$id)->select('id')->get();
foreach($familymembers as $famil){
$faids[]=$famil->id;
}
$familyeducationDatas=Familyeducation::whereIn('familymemberid', $faids)->orderBy('id','DESC')->first();
If Family Members Table Return Four Ids, I Need Last Inserted Record for this Four Ids in Family Education Table.
You should use a column
created_at
which should be the timestamp in Family Education table and re-write the query as follows:
$familyeducationDatas=Familyeducation::whereIn('familymemberid',
$faids)->orderBy('created_at','DESC')->first();
Use SELECT MAX(id) FROM TABLE. This will return the highest currently in use primary key for the table.

Correct relationship between user table(present) and employee table(past and present)

I´m creating a Mysql database where the user table is implemented for login/access purposes and i need to create an employee table with current and past employees. The current employees should have a (1 to 1)corresponding user, past employees shouldn´t. What would be the best way of doing this? Is the design correct?
Thanks
My approach would be to have an employee table with an employee_id Primary Key. This contains current and past employees. I would then have a user table with an employee_id field which has a foreign key relationship with employee.employee_id.
When employees leave the record from user can be deleted to remove their access, while keeping their record in the employee table.
You can make an attribute 'status' in employee table and assign 0 for past employees and 1 for present employees.
Take the employee_id as foreign key in Users table.
Make a join and check status at the login if the status is 1 then proceed to login.

PHP script for adding friends

Well I'm trying for last 5 days to create simple register, confirm, login PHP script, which is for assignment at UNI, but thing which I'm trying for last 5 days and it's not working is adding friends into friend list. Kid a like Facebook but much much simpler, it's for Android game we got as group assignment.
I have one TABLE users where I have fields ID, username, password, email, friends.
Into field friends I would like to save multiple values as ID's of your friends. To retrieve in game some of user information.
This db and tables are on MySQL and INSERT or UPDATE are not working for me, INSERT is creating new record and can't insert only to one column of existing record and UPDATE can't just insert value but will delete old one and insert new one in.
Seeing as this is a many-to-many relation (if I'm correct) so it'd be smart to create a seperate table that records this.
Table: Friends
userID
userID2 (or friendID)
Which you can fill.
For more info: http://www.singingeels.com/Articles/Understanding_SQL_Many_to_Many_Relationships.aspx
Normally you would add a freinds table with the fields:
user_id
friend_id
where both fields are references to the user tables id field.
If you - for some reason - need it to be a field in user table serialize the id values and save them there.
ATTENTION: you won't be able to easily join the tables and there is no automated possibility to keep integrity. If a user is deleted none of the references to this user in friends field will be deleted. This would all be possibile with the secondary friends table and foreign keys.
What you've described here is a many-to-many relationship between people and their friends. The canonical way do implement this in a relational database is to use a pivot table in which each row represents a "friendship" between two people. You'd have two fields to hold the IDs:
users table:
id, name, email, etc.
friendships table:
user_id_1, user_id_2
Then if user 1 is friends with user 2 and user 3, you'd have records (1,2) and (1,3) in the friendships table. You can treat these as reciprocal relationships if you like, or you can require a (2,1) record to denote that user 2 is also friends with user 1.

Mysql non-sequential insert problem

I have a table with 100,000 records described as:
ID primary unique int (5)
Ticket unique int (5)
user varchar (20)
Only fields populated on that table are the first two, ID and Ticket. I need to now assign a user to that ticket when requested. How can i do this? How can I find where the next null user is on the table?
Edit: Explaining Scenario as requested
Its a lottery system of sorts. The Ticket numbers have already been made and populated into the table. Now when a user signs up for a ticket, their username has to be inserted next to the next available ticket, in the user field. Im sure theres a much simpler way to do this by inserting the ticket with all the information on a new table, but this is th exact requirement as dumb as it sounds.
So how can I find out where the next null user is on the table?
What is the sorting scheme of the table ?
If the Id numbers are sequential this should work:
SELECT ID FROM TABLE WHERE user is null ORDER by ID LIMIT 1
If Id numbers are NON sequential and you are OK with using the natural sort of the table (sorted as they were entered)
SELECT ID FROM TABLE WHERE user is null LIMIT 1
Find the next NULL row by doing:
SELECT ID
FROM Ticket
WHERE user IS NULL
LIMIT 1;
When you update though you'll have to be careful you don't have a race condition with another process also getting the same ID. You could prevent this duplicate allocation problem by having a separate table holding the TicketAllocation, and giving it a unique foreign key constraint pointing back to the Ticket table.
you can also do it in a single query:
UPDATE users SET user = [username] where id =
(select min(id) from users where user is null)
This assumes ID is auto-incremented.
Start by finding the first record where the user field is null:
Select * from users where user is null order by id asc limit 1;
Then fill it in:
Update users set user = [username] where id = [id from select];

Most efficient way to insert two rows that depend on each other

I have a site that is essentially a collection of links. A mysql database which stores users, links and votes. My links table has two foreign keys, user_id and vote_id. When a link is inserted into the database it will start with one vote so that means I need to insert a row into the votes table but they essentially depend on one another to exist. I need the ID of the links for the foreign key of the votes table and vice versa. My current "plan" is to insert a links row with a vote_id of 0, select the same row to get it's ID and then insert a votes row using the links row id as it's foreign key, select it's ID and update my original links row. This seems really inefficient but I need to make sure users votes are recorded for time keeping and eliminating duplicate votes. Am I going about this the wrong way?
In PHP, you can use mysql_insert_id:
mysql_query("INSERT INTO mytable (product) values ('kossu')");
printf("Last inserted record has id %d\n", mysql_insert_id());
You could use this to get the row ID of the newly inserted link.
Assuming one vote can only belong to one link, the links table should not have a vote_id column. So you shouldn't need the identity of the newly inserted vote.

Categories