从另一个表的值填充表


populate table from value from another table

我有两个视图表,第一个是国家,第二个是州。就个人而言,他们俩都工作得很好。国家表格列出所有国家,州表格列出所有州。

在国家表格中,我有一个链接,指向将country_id传递到国家表格的国家,因为我只想列出所选国家的国家,而不是所有国家。

我已经尝试了许多其他人提供的解决方案,但仍然无法实现。我很感激你在一些简单的事情上的帮助。

states.php(这是我目前所拥有的,但它不起作用,错误在选择行中。)

<?php
$per_page = 25;
$result = mysql_query("SELECT * FROM states' SET states.country_id = countries.id FROM countries INNER JOIN countries ON states = countries WHERE states.country_id = country_id ORDER BY states.name ASC;");
$total_results = mysql_num_rows($result);
$total_pages = ceil($total_results / $per_page);
if (isset($_GET['page']) && is_numeric($_GET['page']))
{
    $show_page = $_GET['page'];
    // make sure the $show_page value is valid
    if ($show_page > 0 && $show_page <= $total_pages)
    {
        $start = ($show_page -1) * $per_page;
        $end = $start + $per_page; 
    }
    else
    {
        // error - show first set of results
        $start = 0;
        $end = $per_page; 
    }       
}
else
{
    // if page isn't set, show first set of results
    $start = 0;
    $end = $per_page; 
}
echo "<p><a href='state.php'>View All</a> | <b>View Page:</b> ";
for ($i = 1; $i <= $total_pages; $i++)
{
    echo "<a href='state.php?page=$i'>$i</a> ";
}
echo "</p>";
echo "<table border='1'>";
echo "<tr> <th>State:</th></tr>";
for ($i = $start; $i < $end; $i++)
{
    if ($i == $total_results) { break; }
    echo "<tr>";
    echo '<td>' . mysql_result($result, $i, 'name') . '</td>';
    echo "</tr>"; 
}
echo "</table> "; 
?>

您没有任何包含从country.php中选择的国家的变量传递给states.php,所以您需要先弄清楚。

这是一个非常基本的查询示例,您需要根据"所选"的国家提取州。但是您需要在某个地方指定$country变量,而您的代码中没有该变量。

SELECT a.* FROM states a  
WHERE a.country_id = '".$country_id."' 
ORDER BY a.name ASC

如果你没有使用PDO(你没有),那么你需要确保$country变量是安全的,最好至少也能逃脱它。

我已经能够让它按照我需要的方式使用以下内容:

SELECT * FROM states
WHERE country_id='.$_GET['country_id'].'
ORDER BY name ASC'