Please check my code below. When I insert values to the actor and movie table i get the different primary key but the the same values. I want to put all the same values into one primary key( expendables 3 should be in the primary key 19). Is this possible?By the way my primary key is auto_increment.See the picture below.
movie
movie_no movie_name release_year
38 Expendables 3 2013-02-13
40 Kiss of the Dragon 2014-10-03
actor
actor_no name
81 Jet Li
82 Sylvester Stallone
movie_actor
movie_no actor_no rate
51 0 Good
48 91 Excellent
0 92 Excellent
0 93 Excellent
.
$dbc = @mysqli_connect('localhost','root','black98765','activity_7b')
OR die("Could not connect to MySQL: ".mysqli_connect_error());
//insert into actor table
$q = "INSERT INTO actor (name)
VALUES ('$actor')
ON DUPLICATE KEY UPDATE
name='$actor'";
//execute the query to get primary key value
$r = mysqli_query($dbc,$q);
//assign to variable below
$actor_no = mysqli_insert_id($dbc);
//insert into movie table
$q2 = "INSERT INTO movie (movie_name, release_year)
VALUES ('$movie',DATE_FORMAT('$year','%Y-%m-%d'))
ON DUPLICATE KEY UPDATE
movie_name='$movie', release_year=DATE_FORMAT('$year','%Y-%m-%d')";
//execute the query to get primary key value
$r2 = mysqli_query($dbc,$q2);
//assign to variable below
$movie_no = mysqli_insert_id($dbc);
$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($r && $r2 && $r3){
echo "Inserted Successfully!";
}else{
echo "Failed to Insert Data!";
mysqli_error($dbc);
}
mysqli_close($dbc);
?>
.
<?php
if(isset($_POST['submit'])){
if($_SERVER['REQUEST_METHOD'] == 'POST'){
$error = array();
//choose actor
if(!isset($_POST['actor'])){
$error[] = "Please choose of the following actors!";
}else{
$actor = $_POST['actor'];
}
//choose movie
if(!isset($_POST['movie'])){
$error[] = "Please choose of the following movies!";
}else {
$movie = $_POST['movie'];
}
//choose release year
if(!isset($_POST['year'])){
$error[] = "Please choose of the following release year!!";
}else{
$year = $_POST['year'];
}
//choose rate
if(!isset($_POST['rate'])){
$error[] = "Please choose of the following rate!";
}else{
$rate = $_POST['rate'];
}
//if no errors
if(empty($error)){
require('connect.php');
}else{
echo "<p>System Error!</p>";
foreach($error as $msg){
echo $msg."<br/>\n";
}
}
}
}
?>
<form action="form.php" method="POST">
<p>Select the Actor and the Movie they Starred In</p>
<p><label for="actor">Name of Actor:</label></p>
<ul><input type="radio" name="actor" value="Jet Li"/>Jet Li
<input type="radio" name="actor" value="Sylvester Stallone"/>Sylvester Stallone
<input type="radio" name="actor" value="Jason Statham"/>Jason Statham</ul>
<p><label for="movie">Name of Movie:</label></p>
<ul><input type="radio" name="movie" value="Expendables 3"/>Expendables 3
<input type="radio" name="movie" value="Rocky"/>Rocky
<input type="radio" name="movie" value="Kiss of the Dragon"/>Kiss of the Dragon</ul>
<p><label for="year">Release Year:</label>
<input type="text" name="year" placeholder="yyyy-mm-dd" value="<?php if(isset($_POST['submit'])) echo $_POST['year'];?>"</p>
<p><label for="rate">Rate:</label>
<input type="radio" name="rate" value="Excellent"/>Excellent
<input type="radio" name="rate" value="Good"/>Good
<input type="radio" name="rate" value="Worst"/>Worst</p>
<p><input type="submit" name="submit" value="Insert"/></p>
</form>