How to populate the child table with foreign key - MySql - php

I am working on MySQL database. I am new to it that is why I am facing a problem. The problem is populating the child table with foreign key which is referencing to the parent table. I have two tables employee which contains following columns
id as a primary key,
first_name
last_name
birth_date
and a borrowed table which contains following columns
ref as a primary key
employId as a foreign key
book
The employeeId is referencing the primary key id of the employee table. So simply it means the one employee with same id can borrow multiple books. When I insert some data into the employee table It get inserted, but when I have to insert data into the borrowed table, I have to manually insert the value in employeeId column. Isn't it supposed to be populated automatically. or I am misunderstanding the concept of the foreign key.
My SQL Code
$uname = "root";
$pass = "";
$sname ="localhost";
$db ="nady";
//Making database connection
$con = mysqli_connect($sname,$uname,$pass,$db);
$t1 = "CREATE TABLE IF NOT EXISTS employee (
id smallint(5) unsigned AUTO_INCREMENT NOT NULL,
firstname varchar(30),
lastname varchar(30),
birthdate date,
PRIMARY KEY (id)
) ENGINE=InnoDB";
$con->query($t1);
$t2 = "CREATE TABLE IF NOT EXISTS borrowed (
ref int(10) unsigned NOT NULL auto_increment,
employeeid smallint(5) unsigned NOT NULL,
book varchar(50),
PRIMARY KEY (ref),
FOREIGN KEY (employeeid) REFERENCES employee(id) ON UPDATE CASCADE ON DELETE CASCADE
) ENGINE=InnoDB";
$con->query($t2);
if(!$con->query($t2)){
echo $con->error;
}
$i1 = "INSERT INTO employee VALUES(NULL,\"Nadeem\",\"Ahmad\",22)";
$con->query($i1);
$i2 = "INSERT INTO borrowed VALUES(NULL,1,\"Ahmad\")";
$con->query($i2);
if(!$con->query($i2)){
echo $con->error;
}
Simple what I need is ; For example an employee with id 1. Who borrowed 3 books. So in the borrowed table the employeeId column will have three rows with values 1 and different books name. My point is how would I populate the employeeId column when I am inserting the data into it. Let say, John have have borrowed three books and have id 1 then how would I insert data to borrowed table with employeeId of john. I need the query for it. and also query to retrieve the books borrowed by john.

The foreign key is used to link two tables, indicating that the field in a column
(employId from borrowed, in your case) refers to the PRIMARY KEY of another table (id from employee).
When you're inserting a new line in borrowed, you have to indicate the user that is taking that book, to insert it in that line. You have to know the user that is doing it. If you have foreign key, you need the id of that user, which is supposed to be his unique identifier. To insert that John has taken a book, you need to know that John's id is 1.
If the user is already in your employee table and you know his first and last name, you can get the id with a simple select...
SELECT id FROM employee WHERE first_name='John' AND last_name='Smith'
... and then you can do the insert with the id obtained.
If it's new user, you need to add the user first to employee, then get the new id and then insert the new line in borrowed, to do this without having to re-query to employee table to get the new id, you can use the PHP mysqli::$insert_id/mysqli_insert_id function, that gives you the PRIMARY key of the last query. For example...
$con->query("INSERT INTO employee (first_name,last_name) VALUES ('Mark','Whatever')");
$newemployeeid = $con->insert_id;
$con->query("INSERT INTO borrowed (employeeid,book) VALUES (".$newemployeeid.",'Awesome Book Title')");
I hope it helps

Your just need change these lines
$employee_id = $con->insert_id;
$i2 = "INSERT INTO borrowed VALUES(NULL,".$employee_id.",\"Ahmad\")"
first you get last insert id as $employee_id through a inser_id mysql predefined function then you add this $employee_id in borrowed table inserted query.

You can also use the MySQL-function LAST_INSERT_ID(). This function fetches the id from any previous INSERT statement, in the OPs case after inserting a new employee into employee table.
So the INSERT statement can be shortened to this:
INSERT INTO borrowed (employeeid, book) VALUES(LAST_INSERT_ID(),'Ahmad')";
Also note, that it is not required (and probably not even allowed) to fill the column 'ref' since it has AUTO_INCREMENT.
Here's the link to MySQL's documentation/function reference about LAST_INSERT_ID().

Related

Skipping modifying CTE and returning an existing key instead of inserting a new one (if value already exists)

I am writing a query in PostgreSQL that I use in my php code where a user is supposed to be able to add actors to the list of actors by typing the actors name. I'm using a modifying CTE to do this because it needs to update three tables in the database:
Person(Person_ID:Serial, Name: Varchar,... other not relevant information, it's ok to leave null)
Personnel(Personnel_ID:Serial, Role: Varchar, Person_ID:Int (FK))
FilmPeople(Personnel_ID (FK), Media_ID (FK))
So the actor as a person is in the person table, then connected to a role in personnel, and the role is then connected to a media_ID.
The query so far:
WITH person_cte AS (
INSERT INTO Person(Name)
SELECT :actorname
WHERE NOT EXISTS (
SELECT 1 FROM person WHERE name = :actorname2)
RETURNING person_id
),
personnel_cte AS (
INSERT INTO Personnel(role, person_id)
SELECT 'Actor', person_id FROM person_cte
RETURNING personnel_id
)
INSERT INTO FilmPeople(Personnel_ID, Media_ID)
SELECT personnel_id, :id from personnel_cte¨
So the problem as you guys probably see is that my query so far only works if the actor doesnt exist in the database at all. It fails if it finds the actor in the Person table. What I actually want is if the Actor exists in person, then don't insert anything but do return the already existing person_id to the next cte. And then the same thing there, if the personnel (role) already exists for this actor, then move on with the personnel_id and only add the actor to the last table to connect him/her to a movie. And then even here, if it already exists then don't do anything.
EDIT: This is how the tables are created as of now:
CREATE TABLE Person(
Person_ID serial,
Name varchar(255),
DateOfBirth date,
Gender varchar(45),
CONSTRAINT PK_Person PRIMARY KEY (Person_ID)
);
CREATE TABLE Personnel(
Personnel_ID serial,
Role varchar(255),
Person_ID bigint,
CONSTRAINT PK_Personnel PRIMARY KEY (Personnel_ID),
CONSTRAINT FK_PersonPersonnel FOREIGN KEY (Person_ID)
REFERENCES Person(Person_ID)
);
CREATE TABLE FilmPeople(
Personnel_ID bigint,
Media_ID int,
CONSTRAINT PK_FilmPeople PRIMARY KEY (Personnel_ID, Media_ID),
CONSTRAINT FK_PersonnelFilmPeople FOREIGN KEY (Personnel_ID)
REFERENCES Personnel(Personnel_ID),
CONSTRAINT FK_MediaFilmPeople FOREIGN KEY (Media_ID)
REFERENCES Media(Media_ID)
);
When nothing is inserted, the returning clause comes back empty - so this is not the approach you want here. Instead, I would recommed joining the tables. For this to work properly, I would strongly suggest having unique keys on each table, like so:
person(name)
personnel(role,person_id)
filmpeople(personnel_id, media_id)
Then you can use on conflict:
with
person_cte as (
insert into person(name)
values (:actorname)
on conflict (name) do nothing
),
personnel_cte as (
insert into personnel(role, person_id)
select 'actor', p.person_id
from person p
where p.name = :actorname
on conflict (role, person_id) do nothing
)
insert into filmpeople(personnel_id, media_id)
select personnel_id, :id
from pl.personnel pl
inner person p on p.person_id = pl.person_id
where p.name = :actorname and pl.role = 'actor'
on conflict (personnel_id, media_id) do nothing

Auto primary key insert custom table $wpdb

I need to insert to custom table that has primary key 'id' by number, but I got this problem for example
I have 5605 rows (start with id = 1) in my table,so I have to set current id = 5606 to insert.
1/I set $data['id'] = 5606 by hand to insert it, it works fine. current row with id 5606 is inserted.
but I want it automatically get the right id to insert so I do
2/select * to returns the current number of rows in table, it returns 5604 (always lesser by 1 when I check database has 5605). so I + 2 then do insert.
It ends up insert 3 times like 5606 5607 5608 in my table.
Please help me here is my code
$data = array(
'name' => 'naomi',
'ability' => 'walk',
);
$wpdb->get_results("SELECT * FROM contest");
$numid = $wpdb->num_rows;
$numid +=2;
$data['id'] = $numid;
$wpdb->insert('contest', $data);
The given number is for example, my problem is in that format.
Just declare column id (or whatever you use as primary key) as AUTO_INCREMENT (in MySQL) or SERIAL (in PostgreSQL) and insert all other columns but your primary key.
Example:
CREATE TABLE Persons
(
ID int NOT NULL AUTO_INCREMENT,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255),
PRIMARY KEY (ID)
);
INSERT INTO persons (LastName,FirstName,Address,City) VALUES (
'Sample','Person','Sample-street','Sample-city'
);
More than! You should not use any manual inserts for primary keys, because it can make you a lot of problems with handling unsuccessfull queries etc.
SECOND PART. To return number of rows in your table just use
SELECT COUNT(id) FROM persons;

Insert values in table with foreign key in php

I am new to PHP and SQL. I simply want to register a user after a team of users has been created.
Tables that i have used -
team
idTeam int(11) PK + AI
teamName varchar(25)
user
idUser int(11) PK + AI
name varchar(30)
username varchar(30)
password varchar(30)
team int(11) FK
I have used the following code-
<?php
session_start();
include('conn.php');
$name=$_POST['name'];
$team=$_POST['team'];
$username=$_POST['username'];
$password=$_POST['password'];
$qry="SELECT * FROM team WHERE idTeam='$team";
if(mysqli_query($con,$qry)){
mysqli_query("INSERT INTO user(name, team, username, password)VALUES('$name', '$team', '$username', '$password')");
header("location: add_user.php?remarks=success");
mysqli_close($con);
}
else
mysqli_error($con);
?>
i used to get error- Mysql error 1452 - Cannot add or update a child row: a foreign key constraint failsMysql error 1452 - Cannot add or update a child row: a foreign key constraint fails
Example - I have pre-entered the contents of team table-
idTeam - teamName
1 Arsenal
2 Chelsea
3 Liverpool
Now if i want to add a user then I would add him by entering in user table-
idUser team name username password
1 2 abc root pass
So here i am unable to figure out what query should i use in PHP code?
There is a single quotation within your first sql query near idTeam='$team. idTeam is integer type. So it need not within single quotation. Make sure that you are passing value for $team variable that is exist in team table. Try following code.
$name=$_POST['name'];
$team=$_POST['team'];
$username=$_POST['username'];
$password=$_POST['password'];
$qry="SELECT * FROM team WHERE idTeam=$team";
$result = mysqli_query($qry);
$num_rows = mysqli_num_rows($result);
if($num_rows > 0){
mysqli_query("INSERT INTO user(name, team, username, password)VALUES('$name', $team, '$username', '$password')");
}else{
echo "Team is not valid!!!";
}
mysqli_close($con);
header("location: add_user.php?remarks=success");
There needs to be exist a record in the team table that corresponds to the value being passed to $team
Foreign key relationships involve a parent table that holds the central data values, and a child table with identical values pointing back to its parent. The FOREIGN KEY clause is specified in the child table.
It will reject any INSERT or UPDATE operation that attempts to create a foreign key value in a child table if there is no a matching candidate key value in the parent table.
https://dev.mysql.com/doc/refman/5.6/en/create-table-foreign-keys.html

Adding lists of data to MySQL

I have some data to add to my database, I'm not sure what my table schema should be. I have an id number for each specific user, 4 categories of games, and a possible number of items (0+) each user has for each game. I want to set a table for each game with the categories ->id and ->items, so I can save the list of user id's in the table, with the items they have for that game.
I can't seem to get it to work, I think because of the dynamic number of items for each user. Is it possible for me to achieve my above mentioned table schema? Why not/how?
I have been trying:
foreach ($json->rgDescriptions as $mydata)
{
$sql = $dbh->prepare('INSERT INTO user_items_tf2 (items) VALUES (:item) WHERE steam_id = :steamid');
$sql->bindParam(':item', $mydata->name);
$sql->bindParam(':steamid', $steamprofile['steamid']);
$sql->execute();
}
There are numbers of ways to do this but one which is very flexible and seems to answer your questions would be this.
-- Players
CREATE TABLE player
(`id` int primary key auto_increment, `name` varchar(255))
;
-- Games
CREATE TABLE game
(`id` int primary key auto_increment, `name` varchar(255))
;
-- Items and what game they belong to
CREATE TABLE item
(`id` int primary key auto_increment, `game_id` int, `name` varchar(255))
;
-- What games players are playing
CREATE TABLE player_game
(`player_id` int, `game_id` int)
;
-- What items players have
CREATE TABLE player_item
(`player_id` int, `item_id` int, index(`player_id`))
;
If you never needed to ask the question which users had a given item you could skip the player_item table and stuff the data (as JSON for instance) of their items into a column of the player table with a blob type.
$sql = $dbh->prepare('INSERT INTO user_items_tf2 (items, steam_id) VALUES (:item, :steamid)');

MySql constraint violation. Inserting

I have database with 5 tables
students PK : ID -> anum, first, last
studentinfo PK/FK : ID -> why, student_commenets, finished, aidyear
Times PK/FK : ID -> signintime, counselor_start_time,
additional_time, finish_time
counselor PK/FK : ID -> firstcounselor, secondcounselor, thirdcounselor
Comments PK/FK : ID -> counselorcomments, additional_commenets
I have a page called signinpage.php
on that page I have to write to three different tables (student, studentinfo, and time)
My code is as fallows :
if (empty($errors) === true)
{
include('core/queries/inserts.updates.selects/students.php');
include('core/queries/inserts.updates.selects/studentinfo.php');
include('core/queries/inserts.updates.selects/signintime.php');
$dbh = null;
header('location: signedin.php');
exit();
}
each of the files are actual insert queries. (if you yall need to see them I will update this post)
The error I am having is :
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or
update a child row: a foreign key constraint fails (test.times,
CONSTRAINT times_ibfk_2 FOREIGN KEY (id) REFERENCES students
(id) ON DELETE CASCADE ON UPDATE CASCADE)
To add on to this, the first query (the students.php and the second query studentinfo.php)
are inserting just fine. Same ID, the problem occurs with the signintime inserting into table : times.
In phpmyadmin both tables (studentinfo and times) are configured alike with both have cascade on delete and update to the original table (student) since the student him/her starts the session (which is the PK ID).
How can I solved this error?
Edit :
<?php
require('core/init.php');
try
{
$null = NULL;
$query = $dbh->prepare("INSERT INTO `times` (signintime) VALUES (:signintime)");
$query->bindParam(':signintime' , $null);
$query->execute();
}
catch (PDOException $e)
{
error_log($e->getMessage());
die($e->getMessage());
}
?>
Your table design looks wrong to me. I'm assuming there can be multiple entries in the times table for each row in the students table. In that case, you would need the following columns in times:
id - PK
student_id - FK
signintime
counselor_start_time
additional_time
finish_time
Then each row for a particular student would have the same student_id value, but different id values.
The following statements and example is different from the tables you have mentioned but the thought is still the same.
The reason why the error was generated is because you are trying to insert a value on a child table in which that value is not yet present on the parent table. The child table means that it is dependent on the other table (which is the Parent).
To explain further, consider the following schema,
CREATE TABLE StudentList
(
ID INT PRIMARY KEY,
NAme VARCHAR(50)
);
CREATE TABLE AddressList
(
StudentID INT,
Address VARCHAR(50),
CONSTRAINT tb_fk FOREIGN KEY (StudentID)
REFERENCES StudentList(ID)
);
INSERT INTO StudentList VALUES (1, 'Jon');
INSERT INTO StudentList VALUES (2, 'Skeet');
INSERT INTO AddressList VALUES (1, 'Hello');
INSERT INTO AddressList VALUES (2, 'World');
INSERT INTO AddressList VALUES (1, 'Other Address');
There are two tables: StudentList and AddressList. The table Address is the child table and which is dependent to table StudentList (also called the Parent table). The only values that are allowed to be inserted on column StudentID of table AddressList is only 1 and 2 because those are the only IDs found on table StudentList.
When you try to insert record with ID other than 1 and 2 on table Address, eg
INSERT INTO AddressList VALUES (1, 'Other Address');
it will generate an error telling that:
Cannot add or update a child row: a foreign key constraint fails
(db_2_ec2e8.addresslist, CONSTRAINT tb_fk FOREIGN KEY
(StudentID) REFERENCES studentlist (ID)):
because the value of the column StudentID being inserted on the table is not available on the parent table (StudentList).
So, I hoped that this will help you understand now.
SQLFiddle Demo

Categories