类变量为NULL?

时间:2014-01-20 18:22:45

标签: php mysqli webpage

我正在开发一个将信息提取到表单中的Web后端,然后在更新时,将使用新信息更新数据库。但是,当我尝试提取以前存储在类私有变量中的信息时,它会向我抛出一个错误,指出该信息为NULL。我在这里做错了什么?

<?php
class modify_racer
{
    private $mysqli, $racer_id, $firstname,
        $lastname, $banner, $bio;

    public function error($code)
    {
        switch($code)
        {
            case 1:
                echo '<p id="error"><b>Error:</b> Please fill out all fields!</p>';
                modify_racer::send_form($this->firstname, $this->lastname, $this->banner, $this->bio);
                break;
            case 2:
                echo '<p id="error"><b>Error:</b> Racer already exists!</p>';
                break;
            case 3:
                echo '<p id="error"><b>Error:</b> Could not connect to MySQLi: ' . mysqli_error();
                break;
        }
    }

    public function send_form($modify = 1)
    {
?>

<div id="form">
    <h3>Edit Racer:</h3>
    <form method="post" action="">
        <label for="firstname">First Name: </label>
        <input type="text" id="firstname" name="firstname"
            placeholder="Racer's First Name"
            value="<?php echo $this->firstname;?>" />
        <br />
        <label for="lastname">Last Name: </label>
        <input type="text" id="lastname" name="lastname"
            placeholder="Racer's Last Name"
            value="<?php echo $this->lastname;?>" />
        <br />
        <label for="banner">Banner Location: </label>
        <input type="text" id="banner" name="banner"
            placeholder="Racer's Banner Image Location:"
            value="<?php echo $this->banner;?>" />
        <br />
        <label for="bio">Racer's Bio Info: </label>
        <textarea rows="5" cols="50" id="bio" name="bio"
            placeholder="Racer Statistics / Biography"
            value=""><?php echo $this->bio;?></textarea>
        <input type="submit" id="submit" name="modify" value="submit" />
    </form>
</div>

<?php
    }

    public function get_racer($racerID)
    {
        $this->racer_id = $racerID;

        $this->mysqli = new mysqli(MYSQLI_HOST,MYSQLI_USER,MYSQLI_PASS,MYSQLI_DATABASE)
            or die(error(3));

        $racer_info = "SELECT * FROM ArtecRacers WHERE RacerID=?";
        $load_racer = $this->mysqli->prepare($racer_info);
        $load_racer->bind_param('s', $racerID);
        $load_racer->execute();
        $load_racer->bind_result($this->racerID, $this->firstname, $this->lastname, $this->banner, $this->bio);
        $load_racer->fetch();

        modify_racer::send_form();
    }

    public function list_racers()
    {
?>

<div id="form">
    <h3>Select Racer:</h3>
    <form method="post" action="">

        <?php
            $this->mysqli = new mysqli(MYSQLI_HOST,MYSQLI_USER,MYSQLI_PASS,MYSQLI_DATABASE)
                or die(error(3));
            $racer_list = "SELECT * FROM ArtecRacers";

            $get_racers = $this->mysqli->query($racer_list);

            while($list = $get_racers->fetch_array(MYSQLI_NUM))
            {
                echo '<input id="part" type="radio" name="editRacer" value="' . $list[0] . '"/>';
                echo '<label for="part">' . $list[1] . ' ' . $list[2] . '</label><br />';
            }
        ?>

        <input type="submit" name="selectRacer" id="submit" value="Select Racer" />
    </form>
</div>

<?php
    }

    function test2()
    {
        echo $this->firstname;
        echo $this->lastname;
        echo $this->racer_id;
    }
}

$start = new modify_racer();


if(!isset($_POST['selectRacer']))
    $start->list_racers();

if(isset($_POST['selectRacer']))
    $start->get_racer($_POST['editRacer']);

$start->test2();

?>

除了$ start-&gt; test2()之外,代码中的所有内容都有效;从函数test2()中提取的所有信息都是空白的,我不确定为什么...有任何见解?

编辑:

我更改了代码以反映底部的以下内容,而test2()仍然将变量输出为NULL:

if(!isset($_POST['editRacer']))
    $start->list_racers();
else
    $start->get_racers($_POST['editRacer']);

$start->test2();

1 个答案:

答案 0 :(得分:1)

如果您单独留下代码,则必须将selectRacereditRacer参数都传递到页面中。我的猜测是你可能只想通过那个。在这种情况下,您需要更改

if(isset($_POST['selectRacer']))
    $start->get_racer($_POST['editRacer']);

if(isset($_POST['editRacer']))
    $start->get_racer($_POST['editRacer']);

此外,如果您想通过网址栏传递这些值,则需要检查$_GET $_POST

最后,在您通过执行modify_racer::my_method_here()进行方法调用的任何地方,都应该将其更改为$this->my_method_here()。前者是静态方法调用,这意味着它实际上并不与您的对象相关联,这意味着它无法触及这些变量。要使其能够访问和更改变量,您需要通过$this调用它。

相关问题