PHP - 如何使用POST将单选按钮选择传递到另一个页面?

时间:2016-05-30 08:52:57

标签: php mysql

这是我在第1页的表单,单选按钮是使用php生成的,它是数据库中5个表名的列表:

<form action="showtable.php" method="post">
    <table>
        while ($fieldInfo = mysqli_fetch_field($results)) { 
            <input type="radio" name="tableNames" value="<?php echo $tempName; ?>" > <?php echo $tempName ?> <br/>
        <?php } ?>
        <tr> 
            <td><input type="submit" value="submit"></td>
        </tr>
    </table>
</form>

这是我在第2页的操作页面,它需要接收表名选择并创建查询,它不会:

<?php
$tName = $_POST["tableNames"];

require_once("conn.php");

$sql = "SELECT * FROM $tName";

$results = mysqli_query($conn, $sql)
or die ('Problem with query' . mysqli_error($conn));

if (mysqli_num_rows($results) >= 1) {
    echo "query success";
} else {
    echo "query fail";
}
?>

我收到一条错误消息:

  

未定义的索引:tableNames

在第二页

1 个答案:

答案 0 :(得分:0)

将您的代码写为

<?php
     if(isset($_POST["tableNames"])){
         $tName = $_POST["tableNames"];
         require_once("conn.php");
         $sql = "SELECT * FROM $tName";
         $results = mysqli_query($conn, $sql) or die ('Problem with query' . mysqli_error($conn));

         if (mysqli_num_rows($results) >= 1) {
             echo "query success";
         } else {
             echo "query fail";
         }
     }

&GT;