PHP函数调用从表单不起作用

Tux*_*ter 0 php sql forms html-table function

我正在尝试从同一个.php文件中的表单调用函数,但是当点击"提交"按钮时,表格不会生成.

这是代码:

<p>
<?php
function selectQuery()
{
    $con = mysql_connect("localhost","readonly","");
    if (!$con)
    {
      die('Could not connect: ' . mysql_error());
    }

    mysql_select_db("mediadb", $con);
    $result = mysql_query("SELECT title, director FROM movies WHERE year = '$_POST[year_txt]'");
    echo "<table border='1' background='lightgray'>
        <tr>
            <th>Title</th>
            <th>Director</th>
        </tr>";

    while($row = mysql_fetch_array($result))
    {
      echo "<tr>";  
      echo "<td>" . $row['title'] . "</td>";
      echo "<td>" . $row['director'] . "</td>";
      echo "</tr>";
    }
    echo "</table>";
    mysql_close($con);
}
?>
</p>
<p>
<!-- start of entry form -->
<form action="index.php?action=selectQuery" method="post">
Year: <input type="text" name="year_txt" />
<input type="submit"/>
</form>
<!-- end of entry form -->
</p>
Run Code Online (Sandbox Code Playgroud)

知道为什么这不起作用吗?

Pas*_*cal 6

我没有看到你实际调用函数的位置,你只定义它.您需要实现一个读取如下内容的块:

if (isset($_GET['action'])) {
    if ('selectQuery' == $_GET['action']) {
        selectQuery();
    }
}