列出从多个表到多个表的数据

时间:2016-11-06 04:08:28

标签: php

我试图列出" date"表按类别排序,我被卡住了。 任何帮助将更有帮助。我如何做"从日期中选择"列出网址?

Sql结构:

category
-cat_id (int)=>PK
-name

date
-id (int)=>PK 
-url

video_cat
-id (int) => FK to date.id
-cat_id (int) => FK to category.cat_id

PHP:

<?php
$sql = "select * from category";
$result = mysql_query($sql);

if (mysql_num_rows($result) > 0) {

    while ($row = mysql_fetch_assoc($result)) {

        if (!empty($_POST['category']) AND $_POST['category'] == $row["cat_id"]) 
          $select = "selected='selected'";
        else 
          $select = '';

        echo '<option value="' . $row["cat_id"] . '"   ' . $select . ' >' . $row["name"] . '</option>';
    }
}
?>

<?php
if (!empty($_POST['category'])) {

    $category_id = $_POST['category'];
    $sql = "select * from video_cat WHERE cat_id = '" . $category_id . "'";
    $result = mysql_query($sql);

    if (mysql_num_rows($result) > 0) {

        while ($row = mysql_fetch_assoc($result)) {

            echo '<option value="' . $row["id"] . '">' . $row["url"] . '</option>';
        }
    }
}
?>

1 个答案:

答案 0 :(得分:0)

答案应该是这样的

<?php
$sql = "select * from category WHERE date=$date";
$result = mysql_query($sql);
if(mysql_num_rows($result) > 0)
{
while ($row = mysql_fetch_assoc($result)) {
if(!empty($_POST['category']) AND $_POST['category']==$row["cat_id"])

$select="selected='selected'";
    else
    $select = '';
    echo '<option value="'.$row["cat_id"].'"   '.$select.' >'.$row["name"].'</option>';
}
}
?>

<?php
if(!empty($_POST['category'])) {
$category_id = $_POST['category'];
$sql = "select * from video_cat WHERE 
cat_id = '".$category_id."'";
$result = mysql_query($sql);
if(mysql_num_rows($result) > 0)
{
while ($row = mysql_fetch_assoc($result)) {
echo '<option value="'.$row["id"].'">'.$row["url"].'</option>';
  }
}
}
?>