-2

I am working on a web back-end that will pull information into a form, and then when updated, will update the database with the new information. However, when I try to pull information previously stored in a class private variable, it throws me an error stating that the information is NULL. What am I doing wrong here?

<?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();

?>

Everything in the code works except at $start->test2(); all of the information pulled from the function test2() is blank, and I am not sure why... Any insights?

EDIT:

I changed the code to reflect the following on the bottom, and test2() still outputs the variables as NULL:

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

$start->test2();

1 Answers1

1

If you leave your code alone, you're going to have to pass both selectRacer and editRacer parameters into the page. My guess is that you might only want to pass the one, though. In which case, you'll want to change

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

into

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

Also, if you want to pass these values in through the URL bar, you need to check $_GET, not $_POST.

And finally, everywhere that you are making method calls by executing modify_racer::my_method_here(), you should change that to $this->my_method_here(). The former is a static method call, meaning it's not actually associated with your object, meaning it can't touch those variables. For it to be able to access and change the variables, you'll need to call it through $this.

JMTyler
  • 1,604
  • 2
  • 21
  • 24
  • I have tried applying the changes, and I am still having issues. I ended up changing all static methods, and i changed the original if statement at the bottom to: if(!isset($_POST['editRacer'])) $start->list_racers(); else $start->get_racer($_POST['editRacer'])); That works fine, but if I call test2(), it still outputs the three variables as NULL. – TheSilentDrifter Jan 20 '14 at 19:37
  • When you call `get_racer()`, which subsequently calls `send_form()` internally, is the form also missing all the data, or is it only missing when you call `test2()`? Are you passing the `editRacer` parameter through your URL bar? – JMTyler Jan 20 '14 at 20:40
  • all of the data is constant within both of those functions. It is only when I call test2() that the data is returned as NULL. I am not passing the parameter through the URL bar. – TheSilentDrifter Jan 20 '14 at 21:45
  • I must say, I honestly have no idea... I even ran a simplified test locally, mimicking the essence of what you've got up there, and it worked fine for me. I did just notice that you're binding the result to `$this->racerID` instead of `$this->racer_id`, but the other fields should still be getting their values, and anyway you set `$this->racer_id` at the top of the method regardless, so it really shouldn't matter at all. I'm stumped! – JMTyler Jan 20 '14 at 22:36
  • #JMTyler so am I... I am just in the process of re-coding it, maybe I will find my mistake in the process of doing so. I am a little confused why I got a minus three on this, but whatever. Thank you everyone for your help! – TheSilentDrifter Jan 21 '14 at 16:22