Search in MySQL foreign field - php

I'm fairly new to PHP and MySQL.
I have two tables as follows:
1.`users`: `id`//primary key
: `name`//user's name
2.`events`: `u_id`//index key and foreign field to users' id
: `u_name`
A user will input an id in a form. That id will be searched in the users table and the relevant details will be taken and inserted in the events table.
I've created the foreign fields and and till now I made a function that took id as a variable and returned details from the users tables as variables which I then inserted in the events table. But then, it meant using "a lot" of variables and I thought what was the use of foreign field.
I'm still learning PHP and don't know how to find and insert using FOREIGN fields from one table to another. I just know how to create foreign fields. Please help.

Is this what you're talking about?
This is how foreign key is created.
CREATE TABLE parent (id INT NOT NULL,
PRIMARY KEY (id)
);
CREATE TABLE child (id INT, parent_id INT,
INDEX par_ind (parent_id),
FOREIGN KEY (parent_id) REFERENCES parent(id)
ON DELETE CASCADE
);
Apologize if I didn't understand your question
UPDATED
INSERT table1 (col1, col2, col3)
SELECT col1, col2, col3
FROM table2
WHERE col1 = 'xyz'
Hope this helps

You don't need to store the user name in the events table. The point of the foreign key is that you only need to store the user ID in the events table, because that is a REFERENCE to the user.
To get the user name for an event, say event number 6, you would do
select name from users join events on users.id = events.u_id where events.id = 6
So, you should not be trying to insert user data into the events table. Just put the ID in there, and the user data will be available for you to retrieve using the foreign key.

Related

Store multiple value in same cell

I am trying to add a "Kudos" system in my website. I am using phpMyAdmin. When a user press a button, it gives kudos/like on a image. The table contains these columns: id, user, photo and kudos. Then I am thinking about returning the count of how many has gives kudos to that post. I am not sure how to do this, but I am thinking about store the users id inside the kudos column. But how can I store multiple names inside the cell? For example if James, Jack and John give kudos on the same post, it will say Number of kudos: 3.. Any suggestions?
You should make a separate table with columns user_id and image_id. That way every row represents a kudo.
By making both columns a primary key (combined called a composite key), the combination of both creates a unique key. That way a user can't give a kudo to the same image twice!
Because I don't know the specifics of your other tables I assume you have two tables. users with primary key id, type BIGINT(20) and image with primary key id, type BIGINT(20). If that's different you will have to change the CREATE TABLE statement accordingly.
CREATE TABLE IF NOT EXISTS kudo (
user_id BIGINT(20) NOT NULL,
image_id BIGINT(20) NOT NULL,
PRIMARY KEY (user_id, image_id),
FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE,
FOREIGN KEY (image_id) REFERENCES image (id) ON DELETE CASCADE
);
If you use REPLACE INTO rather than INSERT INTO you won't get errors when inserting a duplicate. https://dev.mysql.com/doc/refman/5.7/en/replace.html
To save a kudo from user_id = 1 for image_id = 1:
REPLACE INTO kudo (user_id, image_id) VALUES (1,1);
To count all kudo's for image_id = 1
SELECT COUNT(*) AS kudo_count FROM kudo WHERE image_id = 1;

PHP/SQL/Mysql/phpmyadmin - Foreign Keys

I'm building a shop for school purposes. I have finished my shop but i didn't joined tables and used foreign keys (requirement) because i forgot..
I have a newbie 'question' about foreign keys.
PRODUCTS RATING TABLE:
rate_id (id of the rate),
rate, (stars 0 to 5)
comment (user input textarea),
user_id (id of the user who commented the product),
product_id (id of the product that was commented)
USERS TABLE:
`id` (id of the user),
`email` (email of the user),
`name` (name of the user),
`age`,
`username`,
`password`,
`profile_pic`,
`role` (ADMIN/USER)ยด
When i enter a comment on X product (if (isset($_POST['comment_rating])):
$sql = mysqli_query($link, "SELECT id FROM users WHERE username='$user_id'");
and then later:
$sql = "INSERT INTO products_rating (rate, comment, user_id, product_id)
VALUES ('$rate_text', '$comment', '$user_id', '$product_id');
FINAL: What i want to know is, what do i need to change in my code if i add a foreign key? And should i add a foreign key in this particular case? I must interligate all tables, and I don't understand much about joins / foreign keys. If i wasn't clear let me know. Thank you for you help!
The idea of a foreign key is pretty straightforward.
In the products_rating table, we are storing a value for user_id.
And the value that we stored there is a value of id column from a row in the user table. This establishes a relationship between the row in products_rating an a row in user.
We could get the name and email address of the user that left a product rating
SELECT u.name
, u.email
, r.rate
, r.comment
FROM product_rating r
JOIN user u
ON u.id = r.user_id
WHERE ...
A FOREIGN KEY is a constraint that implements a rule. The rule we want to implement is that we want to allow only valid user.id values to be stored in product_rating.user_id.
For example, if we attempt to store a value of '42' in the product_rating.user_id column, the database is going to check that the value '42' appears in a row in the user table (in the id) column.
The syntax that we use to implement that constraint would be on the product_rating table:
ALTER TABLE `product_rating`
ADD CONSTRAINT -- we are adding a constraint
`FK_product_rating_user` -- the name we assign to the constraint
FOREIGN KEY -- the type of constraint
(`user_id`) -- the column(s) in this table
REFERENCES -- refer to
`user` -- the name of the "foreign" table
(`id`) -- the column(s) in the "foreign" table
We can add some additional configuration that affects behavior...
ON DELETE RESTRICT
ON UPDATE CASCADE
This implements a rule... if we attempt to assign a value to user_id column in this table, and that value is not found in a row in user table id column, an error is thrown, and the statement fails.
This rule also says that if we attempt to remove a row from user, if there are any rows in product_rating that have a user_id value that matches the id value of the row we're deleting, an error is thrown, and the statement fails.
The rule also says that we update a row in user, and assign a new value to the id column, than any rows in product_rating that have a user_id value that matches the old id value will be updated, to assign the new id value to the user_id column... preserving the relationship between the rows.
Also note that this implies a "one-to-many" relationship. A user can be related to zero, one or more product_rating. A product_rating is associated with one user. (If the user_id column allows for NULL values to be stored, a NULL value represents that the row in product_rating is not associated with any user.)
That's just a rough overview of FOREIGN KEY constraints.
The basic idea is that we establish a relationship between rows by storing a common value. In the example we use, a common value of '42' in the id column of a row in the user table, and in the user_id column of one (or more) row(s) in the product rating table.
With no foreign key constraint defined, the database allows us to store any value in the user_id column. We could store '43' or '8670', and it doesn't matter if those values appear in user.id or not.
The foreign key we defined constrains (restricts) what values we can store in the user_id column. When we attempt to add or modify a row in product_rating, the database checks the value of the user_id column, and if a non-NULL value doesn't reference a row in user, an error is raised.
Note that the foreign key constraint does not cause the user_id column to be automatically populated. If we want to establish a relationship to a row in user, we have to supply a value for user_id.

Store multiple data into one primary ID in mysql (audit trail)

I thought i was in the right track. I forgot that a user can do multiple activities. What I did was it's only one activity. What can i do with this? I came up with a solution that might work, come up with an sql statement that updates the activity column without overwriting the text. But that seems stupid. Pretty sure theres a better solution?
I made some adjustment but I think this will improve your database. See this in action via http://sqlfiddle.com/#!2/c6546/9/0
CREATE TABLE workers
(
id_workers int primary key,
username varchar(30),
type varchar(30)
);
CREATE TABLE activities
(
id_activities int auto_increment primary key,
activity varchar(30)
);
CREATE TABLE timesheet
(
id_timesheet int auto_increment primary key,
id_workers int,
id_activities int,
start_time datetime,
end_time datetime
);
INSERT INTO workers
(id_workers, username, type)
VALUES
('1','Danice', 'Administrator'),
('2','Micheal', 'Administrator');
INSERT INTO activities
(activity)
VALUES
('Edit Employee'),
('Edit Employee Information'),
('Edit Department');
INSERT INTO timesheet
(id_workers, id_activities,start_time, end_time)
VALUES
('1', '1', '2013-03-14 13:15:00','2013-03-14 13:20:00'),
('2', '2', '2013-03-14 13:00:00','2013-03-14 14:00:00'),
('1', '2', '2013-03-14 13:21:00','2013-03-14 13:23:00'),
('1', '3', '2013-03-14 13:24:00','2013-03-14 13:45:00');
SELECT a.id_activities, w.username, w.type, a.activity, t.start_time, t.end_time
FROM (timesheet AS t LEFT JOIN activities AS a ON t.id_activities = a.id_activities ) LEFT JOIN workers AS w ON t.id_workers = w.id_workers;
BTW you could use MYSQL workbench if you want to get a good overview from you database layout. Set up a schema inside workbench for reference when coding. Or just use pen and paper. ;-)
With regard to you audit trail, you could give workers the option to delete there activity from the timesheet by adding and extra column (e.g. audittrail) to timesheet. By default the column audittrail is set to 0 and if an worker deletes his entry you give this column the value 1. Then add to you SELECT query "WHERE audittrail = 0" to display all the timesheet entries. And in the case you require an audit trail you leave out the WHERE clause.
http://www.c-sharpcorner.com/uploadfile/nipuntomar/normalization-and-its-types/
What you're trying to do is called database normalization. You want meta data against one particular record. So just 'glancing' at your problem. Make a table for storage, X, and a have a foreign key constraint to the data record you want to describe. In this case, it looks like the users table, and then have fields in this X table which stores the 'multiple data' type which is related to users table.
Tables
users -- table
id
username
password
status
updation_date
creation_date
x -- table
id
users_id -- foreign key constraint
part1_of_multiple_data -- meta log data you are wanting
part2_of_multiple_data -- meta log data you are wanting
part3_of_multiple_data -- add more fields as you need in the table.
updation_date
creation_date

New table or field with array in field (php/mysql)

I need to store multiple id's in either a field in the table or add another table to store the id's in.
Each member will basically have favourite articles. Each article has an id which is stored when the user clicks on a Add to favourites button.
My question is:
Do I create a field and in this field add the multiple id's or do I create a table to add those id's?
What is the best way to do this?
This is a many-to-many relationship, you need an additional table storing pairs of user_id and article_id (primary keys of user and article tables, respectively).
You should create a new table instead of having comma seperated values in a single column.
Keep your database normalized.
You create a separate table, this is how things work in a relational database. The other solution (comma separated list of ids in one column) will lead to an unmaintainable database. For example, what if you want to know how many times an article was favorited? You cannot write queries on a column like this.
Your table will need to store the user's id and the article's id - these refer to the primary keys of the corresponding tables. For querying, you can either use JOINs or nested SELECT queries.
As lafor already pointed out this is a many-to-many relationship and you'll end up with three tables: user, article, and favorite:
CREATE TABLE user(
id INT NOT NULL,
...
PRIMARY KEY (id)
) ENGINE=INNODB;
CREATE TABLE article (
id INT NOT NULL,
...
PRIMARY KEY (id)
) ENGINE=INNODB;
CREATE TABLE favorite (
userID INT NOT NULL,
articleID INT NOT NULL,
FOREIGN KEY (userID) REFERENCES user(id) ON DELETE CASCADE,
FOREIGN KEY (articleID) REFERENCES article(id) ON DELETE CASCADE,
PRIMARY KEY (userID, articleID)
) ENGINE=INNODB;
If you then want to select all user's favorite articles you use a JOIN:
SELECT * FROM favorite f JOIN article a ON f.articleID = a.id WHERE f.userID = ?
If you want to know why you should use this schema, I recommend reading about database normilization. With multiple IDs in a single field you would even violate the first normal form and thus land in a world of pain...

How to set a foreign key and retrieve or delete data from multiple tables?

I am new to php and mysql. I created a database named 'students' which contain two tables as 'student_details' which have fields like 'ID, Name, Age, Tel#, Address' and another table as 'fee_details' which have fields like 'ID(student_details table ID), Inst Id, Date, Receipt No'.
I want to set a foreign key and retrieve data from both tables when a student paid their fees and if a student passed out or discontinued I want a delete option to delete his all records from my tables. So please help me to solve this by PHP code and displays it in HTML while using a search form.
Enforcing referential integrity at the database level is the way to go. I believe when you said you wanted the delete "to delete his all records from my tables" you meant deleting the row and all its child records. You can do that by using foreign keys and ON DELETE CASCADE.
CREATE TABLE students
(
student_id INT NOT NULL,
name VARCHAR(30) NOT NULL,
PRIMARY KEY (student_id)
) ENGINE=INNODB;
CREATE TABLE fee_details
(
id INT,
date TIMESTAMP,
student_id INT,
FOREIGN KEY (student_id) REFERENCES students(student_id)
ON DELETE CASCADE
) ENGINE=INNODB;
With this, when a student is deleted from the students table, all its associated records will be deleted from fee_details.
you can try mysql_query() and mysql_assoc_array()

Categories