简体   繁体   中英

Retrieving foreign key value joining three tables

I have three database tables: hall, movie, and screening as shown below. 数据库表

In my screening table I have movie_id and hall_id as foreign key which references table movie and hall, respectively.

While adding the details of screening in my add detail form, I want to show a dropdown menu for available movie title and hall title from table hall and movie respectively.

But after I submit the form I want the ids of respective movies and halls but not their titles saved in the screening table.

How can I acheive this in PHP?

I have joined the three tables like this.

$sql="SELECT scr_id,scr_date,scr_start,scr_end,m.movie_id,h.hall_id 
        FROM screening as scr
        join movie as m on scr.movie_id=m.movie_id 
        join halls as h on scr.hall_id=h.hall_id";

And here is my add detail form: 添加明细表

Here is the form which I am using to add the details. I have commented the first two elements as I am unable to derive the logic to add these elements.

<?php
    if(isset($_GET['action'])&&$_GET['action']=="add")
    {

  ?>
<div class="cover">
<h1>Add Schedule</h1>

    <form action="scheduleact.php" method="post">
       <!-- <label for="title">Movie Title</label><input type="text" name="title" value=""/>
          <label for="hall">Hall</label><input type="text" name="hall" value=""/>  -->     
        <label for="date">Date</label><input type="date" name="date"/>
        <label for="start">Start Time</label><input type="time" name="start"/> 
        <label for="end">End Time</label><input type="time" name="end"/>                                             
        <p><input type="submit" name="addschedule" value="submit"/></p>
    </form>
</div>
<?php
    }
?>

And this is scheduleact.php

function addschedule($date,$start,$end)
            {
                $sql="insert into screening (movie_id,hall_id,scr_date,scr_start,scr_end) values ('$movid','$hallid',$date','$start','$end')";
                $this->exeQuery($sql) or die(mysql_error());
            }
<?php
    include_once("../class/db_inc.php");

    if(isset($_POST['addschedule'])&&($_POST['addschedule']=='submit'))
    {
        //$movid=$_POST[''];
        //$hallid=$_POST[''];
        $date=$_POST['date'];
        $start=$_POST['start'];
        $end=$_POST['end'];
        $schedule->addschedule($date,$start,$end);
        echo "<script>alert('added successfully');</script>";
        echo "<script>window.location='index.php?obj=viewschedule';</script>";
    }
?>

I think joining the tables together is not the approach you need to populate your dropdown menus. You can do two separate queries, one for movies and one for halls. Here is a general example. There are some missing pieces because I don't know what you are using to run your queries, but hopefully this can give you a general idea.

<select name="movie_id">
<?php
$movies_sql = 'SELECT movie_id, movie_title FROM movie';
//execute your query
while ($row = $query_result->fetch) {
    echo "<option value=\"$row[movie_id]\">$row[movie_title]</option>";
}
?>
</select>

<select name="hall_id">
<?php
$halls_sql = 'SELECT hall_id, hall_title FROM halls';
//execute your query
while ($row = $query_result->fetch) {
    echo "<option value=\"$row[hall_id]\">$row[hall_title]</option>";
}
?>
</select>

You would mainly need to join the tables when you are retrieving screening records that you have already stored and you want to include the related information (movie and hall).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM