Ok I am new in php development so I did some mistakes, didnt planned my database just started because I was so excited about my first project, SO dont judg me.. :D :D
this is my Primary database Primary Database
and this is my Movies database,
Movies database
so I created the songs database first and stored the data in it, after that i created the movies database and movie_id is the foreign key of the id of movies database, so there are too many songs to add foreign keys in primary database, I want a php script which can insert the foreign key for me in my primary database,
I created a php script ( actually tried for several hours) but didnt succseeded
I wanted to get movie_name from songs database match with the movie_name of movies databse if both movies matches insert the id of movies database into the movie_id(foreign key) of song database
<?php
require_once ('../inc/db.php') ?>
<?php
$lang_query = " SELECT * FROM songs";
$query = "UPDATE songs SET movie_id = '$mov_id'";
$lang_run = mysqli_query($conn, $lang_query);
$mov_query = " SELECT * FROM movies";
$mov_run = mysqli_query($conn, $mov_query);
$mov_row = 1;
$lang_row = 1;
while ($mov_row = mysqli_fetch_array($mov_run))
{
$mov_name = $mov_row['movie_name'];
$mov_id = $mov_row['id'];
while ($lang_row = mysqli_fetch_array($lang_run))
{
echo $movie_name = $lang_row['movie_name'];
$movie_id = $lang_row['id'];
if ($movie_name == $mov_name)
{
mysqli_query($conn, "UPDATE songs SET movie_id = '$mov_id' where id = '$movie_id'");
}
}
} ?>
Please Help me, Thanks :)
I think you can do it by running following sql query -
update *songs* inner join *movies* on songs.movie_name=movies.movie_name set songs.movie_id=movie.id
I honestly think that before you worry about trying to add foreign keys based on your current schema, that you really need to revisit the schema and normalize the data. Why have the same movie name and movie slug fields in two tables? Why have movies, songs, singers, actors, categories, etc. all in the same table? These things probably all need their own tables that are related to each other.
When building your database, think in real world terms, because you are likely going to want your application users to be able to interact with the data in the database in a real world sense. To me, you would probably need the following tables at a minimum:
movies
songs
movies_to_songs (join table to express many-to-many relationship)
actors
movies_to_actors (many-to-many)
editors
movies_to_editors (many-to-many)
movie_categories
movies_to_movie_categories (if you want to treat this as many-to-many)
singers
songs_to_singers (many-to-many)
youtube_videos (a separate table where you could store all video data)
Each table would have additional columns (properties) that are specific only to a single entity of the type contained in the table. So, for example a movie table might look like
id (primary key)
name
slug
image
language
release_date
youtube_id (reference to listing on youtube_videos table)
And you might have an actors table like:
id (primary key)
name
... (sex, birthday, etc.)
And a movies_to_actors table that is just two columns with compound primary key (i.e. combinations must be unique)
movie_id (references primary key id in movie table)
actor_id (reference primary key id in actors table)
And so on across your various tables.
Just remember to think about the real world relation of one object to another and the real world properties (columns) for each of those individual objects.