This question already has answers here:
“Notice: Undefined variable”, “Notice: Undefined index”, and “Notice: Undefined offset” using PHP
                                
                                    (28个答案)
                                
                        
                                3年前关闭。
            
                    
这是我在第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


在第二页

最佳答案

将您的代码写为

<?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";
         }
     }


?>

10-08 11:06