gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 23:38:28 25 4
gpt4 key购买 nike

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Notice: Undefined property: stdClass::$memberID inD:\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>

我很困惑到底是什么导致了这个错误。并且以前从未遇到过。我该如何修复它?谢谢

最佳答案

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

请仔细检查此查询。检查表名、字段名小写/大写。之后你的控件应该进入这一行 while ($row = $result->fetch_object())

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

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

创建数据库、表名和字段时始终使用 Snake_cased 表示法。

您可以在核心 PHP 程序中使用驼峰命名法或蛇形命名法。

希望这有帮助!

关于php - 未定义的属性:stdClass::($variablename),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26300194/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com