未定义的属性:stdClass ::($ variablename)

时间:2014-10-10 13:06:51

标签: php mysql forms

我目前正在创建一个接收数据并将其输出到表中的表单。在将数据插入表单并按下提交按钮后,我收到以下错误:

    Notice: Undefined property: stdClass::$memberID in D:\Xamp\htdocs\Assignment\Addmember\New Ver\view.php on line 40

    Notice: Undefined property: stdClass::$username in D:\Xamp\htdocs\Assignment\Addmember\New Ver\view.php on line 41

    Notice: Undefined property: stdClass::$password in D:\Xamp\htdocs\Assignment\Addmember\New Ver\view.php on line 42

    Notice: Undefined property: stdClass::$firstname in D:\Xamp\htdocs\Assignment\Addmember\New Ver\view.php on line 43

    Notice: Undefined property: stdClass::$lastname in D:\Xamp\htdocs\Assignment\Addmember\New Ver\view.php on line 44

    Notice: Undefined property: stdClass::$address in D:\Xamp\htdocs\Assignment\Addmember\New Ver\view.php on line 45

    Notice: Undefined property: stdClass::$email in D:\Xamp\htdocs\Assignment\Addmember\New Ver\view.php on line 46

    Notice: Undefined property: stdClass::$memberID in D:\Xamp\htdocs\Assignment\Addmember\New Ver\view.php on line 47

    Notice: Undefined property: stdClass::$memberID in D:\Xamp\htdocs\Assignment\Addmember\New Ver\view.php on line 48

    Notice: Undefined property: stdClass::$memberID in D:\Xamp\htdocs\Assignment\Addmember\New Ver\view.php on line 40

    Notice: Undefined property: stdClass::$username in D:\Xamp\htdocs\Assignment\Addmember\New Ver\view.php on line 41

    Notice: Undefined property: stdClass::$password in D:\Xamp\htdocs\Assignment\Addmember\New Ver\view.php on line 42

    Notice: Undefined property: stdClass::$firstname in D:\Xamp\htdocs\Assignment\Addmember\New Ver\view.php on line 43

    Notice: Undefined property: stdClass::$lastname in D:\Xamp\htdocs\Assignment\Addmember\New Ver\view.php on line 44

    Notice: Undefined property: stdClass::$address in D:\Xamp\htdocs\Assignment\Addmember\New Ver\view.php on line 45

    Notice: Undefined property: stdClass::$email in D:\Xamp\htdocs\Assignment\Addmember\New Ver\view.php on line 46

    Notice: Undefined property: stdClass::$memberID in D:\Xamp\htdocs\Assignment\Addmember\New Ver\view.php on line 47

    Notice: Undefined property: stdClass::$memberID in D:\Xamp\htdocs\Assignment\Addmember\New Ver\view.php on line 48

    Notice: Undefined property: stdClass::$memberID in D:\Xamp\htdocs\Assignment\Addmember\New Ver\view.php on line 40

    Notice: Undefined property: stdClass::$username in D:\Xamp\htdocs\Assignment\Addmember\New Ver\view.php on line 41

    Notice: Undefined property: stdClass::$password in D:\Xamp\htdocs\Assignment\Addmember\New Ver\view.php on line 42

    Notice: Undefined property: stdClass::$firstname in D:\Xamp\htdocs\Assignment\Addmember\New Ver\view.php on line 43

    Notice: Undefined property: stdClass::$lastname in D:\Xamp\htdocs\Assignment\Addmember\New Ver\view.php on line 44

    Notice: Undefined property: stdClass::$address in D:\Xamp\htdocs\Assignment\Addmember\New Ver\view.php on line 45

    Notice: Undefined property: stdClass::$email in D:\Xamp\htdocs\Assignment\Addmember\New Ver\view.php on line 46

    Notice: Undefined property: stdClass::$memberID in D:\Xamp\htdocs\Assignment\Addmember\New Ver\view.php on line 47

    Notice: Undefined property: stdClass::$memberID in D:\Xamp\htdocs\Assignment\Addmember\New Ver\view.php on line 48

表单代码

 <?php
    /*
        Allows the user to both create new records and edit existing records
    */

    // connect to the database
    include("connection.php");

    // creates the new/edit record form
    // since this form is used multiple times in this file, I have made it a function that is easily reusable
    function renderForm($username = '', $password ='', $firstname ='', $lastname ='', $address ='', $email ='', $error = '', $memberID = '')
    { ?>
        <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
        <html>
            <head>  
                <title>
                    <?php if ($memberID != '') { echo "Edit Record"; } else { echo "New Record"; } ?>
                </title>
                <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
            </head>
            <body>
                <h1><?php if ($memberID != '') { echo "Edit Record"; } else { echo "New Record"; } ?></h1>
                <?php if ($error != '') {
                    echo "<div style='padding:4px; border:1px solmemberID red; color:red'>" . $error
                        . "</div>";
                } ?>

                <form action="" method="post">
                <div>
                    <?php if ($memberID != '') { ?>
                        <input type="hidden" name="memberID" value="<?php echo $memberID; ?>" />
                        <p>memberID: <?php echo $memberID; ?></p>
                    <?php } ?>

                    <strong>Username: *</strong> <input type="text" name="username" value="<?php echo $username; ?>"/><br/>
                    <strong>Password: *</strong> <input type="password" name="password" value="<?php echo $password; ?>"/><br/>
                    <strong>First Name: *</strong> <input type="text" name="firstname" value="<?php echo $firstname; ?>"/><br/>
                    <strong>Last Name: *</strong> <input type="text" name="lastname" value="<?php echo $lastname; ?>"/><br/>
                    <strong>Address: *</strong> <input type="text" name="address" value="<?php echo $address; ?>"/><br/>
                    <strong>Email: *</strong> <input type="text" name="email" value="<?php echo $email; ?>"/><br/>
                    <p>* required</p>
                    <input type="submit" name="submit" value="Submit" />
                </div>
                </form>
            </body>
        </html>

    <?php }



        /*

           EDIT RECORD

        */
    // if the 'memberID' variable is set in the URL, we know that we need to edit a record
    if (isset($_GET['memberID']))
    {
        // if the form's submit button is clicked, we need to process the form
        if (isset($_POST['submit']))
        {
            // make sure the 'memberID' in the URL is valmemberID
            if (is_numeric($_POST['memberID']))
            {
                // get variables from the URL/form
                $memberID = $_POST['memberID'];
                $username = htmlentities($_POST['username'], ENT_QUOTES);
                $password = htmlentities($_POST['password'], ENT_QUOTES);
                $firstname = htmlentities($_POST['firstname'], ENT_QUOTES);
                $lastname = htmlentities($_POST['lastname'], ENT_QUOTES);
                $address = htmlentities($_POST['address'], ENT_QUOTES);
                $email = htmlentities($_POST['email'], ENT_QUOTES);
                // check that firstname and lastname are both not empty
                if ($username == '' || $password == '' || $firstname == '' || $lastname == '' || $address == '' || $email == '')
                {
                    // if they are empty, show an error message and display the form
                    $error = 'ERROR: Please fill in all required fields!';
                    renderForm($username, $password, $firstname, $lastname, $address, $email, $error, $memberID);
                }
                else
                {
                    // if everything is fine, update the record in the database
                    if ($stmt = $mysqli->prepare("UPDATE players SET firstname = ?, lastname = ?
                        WHERE memberID=?"))
                    {
                        $stmt->bind_param("ssi", $firstname, $lastname, $memberID, $username, $password, $address, $email);
                        $stmt->execute();
                        $stmt->close();
                    }
                    // show an error message if the query has an error
                    else
                    {
                        echo "ERROR: could not prepare SQL statement.";
                    }

                    // redirect the user once the form is updated
                    header("Location: view.php");
                }
            }
            // if the 'memberID' variable is not valmemberID, show an error message
            else
            {
                echo "Error!";
            }
        }
        // if the form hasn't been submitted yet, get the info from the database and show the form
        else
        {
            // make sure the 'memberID' value is valmemberID
            if (is_numeric($_GET['memberID']) && $_GET['memberID'] > 0)
            {
                // get 'memberID' from URL
                $memberID = $_GET['memberID'];

                // get the recod from the database
                if($stmt = $mysqli->prepare("SELECT * FROM members WHERE memberID=?"))
                {
                    $stmt->bind_param("i", $memberID);
                    $stmt->execute();

                    $stmt->bind_result($memberID, $firstname, $lastname, $username, $password, $address, $email);
                    $stmt->fetch();

                    // show the form
                    renderForm($firstname, $lastname, $username, $password, $address, $email, NULL, $memberID);

                    $stmt->close();
                }
                // show an error if the query has an error
                else
                {
                    echo "Error: could not prepare SQL statement";
                }
            }
            // if the 'memberID' value is not valmemberID, redirect the user back to the view.php page
            else
            {
                header("Location: view.php");
            }
        }
    }



        /*

           NEW RECORD

        */
    // if the 'memberID' variable is not set in the URL, we must be creating a new record
    else
    {
        // if the form's submit button is clicked, we need to process the form
        if (isset($_POST['submit']))
        {
            // get the form data
                $username = htmlentities($_POST['username'], ENT_QUOTES);
                $password = htmlentities($_POST['password'], ENT_QUOTES);
                $firstname = htmlentities($_POST['firstname'], ENT_QUOTES);
                $lastname = htmlentities($_POST['lastname'], ENT_QUOTES);
                $address = htmlentities($_POST['address'], ENT_QUOTES);
                $email = htmlentities($_POST['email'], ENT_QUOTES);

            // check that firstname and lastname are both not empty
            if ($username == '' || $password == '' || $firstname == '' || $lastname == '' || $address == '' || $email == '')
            {
                // if they are empty, show an error message and display the form
                $error = 'ERROR: Please fill in all required fields!';
                    renderForm($username, $password, $firstname, $lastname, $address, $email, $error, $memberID);
            }
            else
            {
                // insert the new record into the database
                if ($stmt = $mysqli->prepare("INSERT members (username, password, firstname, lastname, address, email) VALUES (?, ?, ?, ?, ?, ?)"))
                {
                    $stmt->bind_param("ss", $username, $password, $firstname, $lastname, $address, $email, $error, $memberID);
                    $stmt->execute();
                    $stmt->close();
                }
                // show an error if the query has an error
                else
                {
                    echo "ERROR: Could not prepare SQL statement.";
                }

                // redirec the user
                header("Location: view.php");
            }

        }
        // if the form hasn't been submitted yet, show the form
        else
        {
            renderForm();
        }
    }

    // close the mysqli connection
    $mysqli->close();

?>

这是我的观看代码:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
        <head>  
                <title>View Records</title>
                <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        </head>
        <body>

                <h1>View Records</h1>

                <p><b>View All</b> | <a href="view-paginated.php">View Paginated</a></p>

                <?php
                        // connect to the database
                        include('connection.php');

                        // get the records from the database
                        if ($result = $mysqli->query("SELECT * FROM members ORDER BY memberID"))
                        {
                                // display records if there are records to display
                                if ($result->num_rows > 0)
                                {
                                        // display records in a table
                                        echo "<table border='1' cellpadding='10'>";

                                        // set table headers
                                        echo "<tr><th>memberID
                                        </th><th>username
                                        </th><th>password
                                        </th><th>firstname
                                        </th><th>lastname
                                        </th><th>address
                                        </th><th>email
                                        </th><th></th><th></th></tr>";

                                        while ($row = $result->fetch_object())
                                        {
                                                // set up a row for each record
                                                echo "<tr>";
                                                echo "<td>" . $row->memberID . "</td>";
                                                echo "<td>" . $row->username . "</td>";
                                                echo "<td>" . $row->password . "</td>";
                                                echo "<td>" . $row->firstname . "</td>";
                                                echo "<td>" . $row->lastname . "</td>";
                                                echo "<td>" . $row->address . "</td>";
                                                echo "<td>" . $row->email . "</td>";
                                                echo "<td><a href='records.php?memberID=" . $row->memberID . "'>Edit</a></td>";
                                                echo "<td><a href='delete.php?memberID=" . $row->memberID . "'>Delete</a></td>";
                                                echo "</tr>";
                                        }

                                        echo "</table>";
                                }
                                // if there are no records in the database, display an alert message
                                else
                                {
                                        echo "No results to display!";
                                }
                        }
                        // show an error if there is an issue with the database query
                        else
                        {
                                echo "Error: " . $mysqli->error;
                        }

                        // close database connection
                        $mysqli->close();

                ?>

                <a href="records.php">Add New Record</a>
        </body>
</html>

我很难过会导致这个错误。从来没有遇到过它。我该怎么办呢?感谢

1 个答案:

答案 0 :(得分:0)

if ($result = $mysqli->query("SELECT * FROM members ORDER BY memberID"))

请仔细检查此查询。检查表名,字段名称下/上案例。之后,您的控件应该进入此行while ($row = $result->fetch_object())

然后你必须能够获得记录列表。如果没有,那么我建议在打印单个行之前使用以下代码:print "<pre>"; var_dump($row); exit;

这只是大写/小写字母不匹配的愚蠢情况。

在创建数据库,表名和字段时始终使用基于snake_的表示法。

您可以在Core PHP程序中使用camelCase或snake_case表示法。

希望这有帮助!