Below is the code for connecting my php code to mysql. I want to display all the movie of an actor for example actor_name will display all the movie it is related to.. Can you guys help me with this problem.The output for this is when i search Actor not found..Can you check if my code in variable q4 is correct thanks.
actor table
movie table
movie_actor table
What i want is to display all the movies where actor_no 1 and 2 starred in.. thanks
<?php
$dbc = @mysqli_connect('localhost', 'root', 'black98765', 'activity_7b')
OR die("Could not connect to MySQL: " . mysqli_connect_error());
if(isset($_POST['submit'])){
$actor_query = "SELECT * FROM actor where name='$actor'";
$actor_result = mysqli_query($dbc, $actor_query);
$actor_data = mysqli_fetch_array($actor_result);
$actor_no = null;
// Check if actor already exists
if (isset($actor_data['actor_no'])) {
$actor_no = (int)$actor_data['actor_no'];
} else {
// Actor does not exist in the table, so we will create a new entry
//insert into actor table
$q = "INSERT INTO actor (name) VALUES ('$actor')";
//execute the query to get primary key value
$r = mysqli_query($dbc, $q);
if ($r) {
echo "Inserted actor successfully!";
//assign to variable below
$actor_no = mysqli_insert_id($dbc);
} else {
echo "Failed to insert actor data!";
mysqli_error($dbc);
}
}
//insert into movie table
$movie_query = "SELECT * FROM movie where movie_name='$movie'";
$movie_result = mysqli_query($dbc, $movie_query);
$movie_data = mysqli_fetch_array($movie_result);
$movie_no = null;
// Check if movie already exists
if (isset($movie_data['movie_no'])) {
$movie_no = $movie_data['movie_no'];
} else {
//movie does not exist in the table, so we will create a new entry
//insert into actor table
$q2 = "INSERT INTO movie (movie_name, release_year) VALUES ('$movie',DATE_FORMAT('$year','%Y-%m-%d'))";
//execute the query to get primary key value
$r2 = mysqli_query($dbc, $q2);
if ($r2) {
echo "Inserted move successfully!";
//assign to variable below
$movie_no = mysqli_insert_id($dbc);
} else {
echo "Failed to Insert Data!";
mysqli_error($dbc);
}
}
if (null !== $movie_no && null !== $actor_no) {
$q3 = "INSERT INTO movie_actor (movie_no, actor_no, rate) VALUES ($movie_no, $actor_no, '$rate')";
//connect and insert $q
$r3 = mysqli_query($dbc, $q3);
if ($r3) {
echo "Inserted Successfully!";
} else {
echo "Failed to Insert Data!";
mysqli_error($dbc);
}
} else {
echo "Failed to Insert Data!";
}
}
if(isset($_POST['search'])){
$q4 = "SELECT movie.movie_name,actor.name
FROM movie
INNER JOIN actor
WHERE movie_name = '$search_actor'";
$r4 = mysqli_query($dbc,$q4);
if($r4){
while($row = mysqli_fetch_assoc($r4){
echo "<p>Movies of Actor which he Starred In:</p>"
echo $row['movie_name']."<b/>";
}
}else{
echo "actor not found!";
}
}
mysqli_close($dbc);
?>