【问题标题】:HTML Select Option and submit to fetch data from PHP PDO MySqlHTML 选择选项并提交以从 PHP PDO MySql 获取数据
【发布时间】:2016-07-23 17:18:15
【问题描述】:

我喜欢在 HTML 选择选项中通过选择月份和年份从 MySQL 中获取数据,当我提交视图时,表格将被过滤。否则将显示所有数据。但是我的代码显示错误。

注意:未定义变量:C:\xampp\htdocs\testSelect.php 中第 85 行的用户

致命错误:在第 85 行的 C:\xampp\htdocs\testSelect.php 中的 null 上调用成员函数 getAllUser()

如何解决这个错误。请帮帮我。

这里是我的代码:

<?php
    class DatabaseConnection{
        public function __construct(){
            global $pdo;
            try{
                $pdo=new PDO('mysql:host=localhost;dbname=test','root','');
            }catch(PDOException $e){
                exit('Database Error');
            }
        }
    }
?>


<?php echo "Select : &emsp;&emsp;" ; ?>

<?php
$monthArray = range(1, 12);
?>
<select name="month">
<option value="">Select Month</option>
<?php
foreach ($monthArray as $month) {
    // padding the month with extra zero
    $monthPadding = str_pad($month, 2, "0", STR_PAD_LEFT);
    // you can use whatever year you want
    // you can use 'M' or 'F' as per your month formatting preference
    $fdate = date("F", strtotime("2015-$monthPadding-01"));
    echo '<option value="'.$monthPadding.'">'.$fdate.'</option>';
}
?>
</select>

&emsp;&emsp;

<?php
// set start and end year range
$yearArray = range(2000, 2050);
?>
<!-- displaying the dropdown list -->
<select name="year">
    <option value="s">Select Year</option>
    <?php
    foreach ($yearArray as $year) {
        // if you want to select a particular year
        $selected = ($year == 's') ? 'selected' : '';
        echo '<option '.$selected.' value="'.$year.'">'.$year.'</option>';
    }
    ?>
</select>


<form method="post">
    <input type="submit" name="view" value="View" />
</form> 


<?php
function getAllUserReport($username){
    global $pdo;
    $query=$pdo->prepare("SELECT * FROM user name= $username ");
    $query->execute(array($username));
    return $query->fetchALL(PDO::FETCH_ASSOC);
}

function filterTable(){
    global $pdo;
    $query=$pdo->prepare("Select from user where year(date) = $year and month(date) = $month ");
    $query->execute();
    return $query->fetchALL(PDO::FETCH_ASSOC);
}
?>



<table class="tbl_one">
    <tr>
        <th>Serial</th>
        <th>Name</th>
        <th>Age</th>
        <th>BirthDate</th>
    </tr>
    <?php 
        $i=0;
        $alluser = $user->getAllUser($username);

        if(isset($_POST['view'])){
            $alluser = $user->filterTable();  
            foreach($alluser as $user){
            $i++;
    ?>        
            <tr>
                <td><?php echo $i; ?></td>
                <td><?php echo $user['name']; ?></td>
                <td><?php echo $user['age']; ?></td>
                <td><?php echo $user['birthdate']; ?></td>
            </tr>
            <?php } ?>
        <?php

        }else{

        foreach($alluser as $user){
            $i++;
    ?>       

    <tr>
        <td><?php echo $i; ?></td>
        <td><?php echo $user['name']; ?></td>
        <td><?php echo $user['age']; ?></td>
        <td><?php echo $user['birthdate']; ?></td>
    </tr>
    <?php } } ?>
</table>

【问题讨论】:

    标签: php html mysql select pdo


    【解决方案1】:

    注意:未定义变量:第 85 行 C:\xampp\htdocs\testSelect.php 中的用户

    致命错误:在第 85 行的 C:\xampp\htdocs\testSelect.php 中的 null 上调用成员函数 getAllUser()

    错误表明,您的 $user 变量未定义。所以你不能在上面调用方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-16
      • 2013-11-23
      • 1970-01-01
      • 2022-07-16
      相关资源
      最近更新 更多