我有一个显示用户数据的个人资料页面,用户可以编辑他们的个人资料信息。在个人资料页面上,用户可以编辑他们的名字,姓氏和电子邮件。用户无法更新的唯一字段是“用户名”字段,因为它仅显示为文本,我不希望用户能够编辑其用户名。
现在,所有内容都会显示并正常工作,直到用户更新其名称为例,然后按下更新按钮。这会将用户新更新的信息重新加载到字段中,此时用户名字段会出错。
错误说:
注意:未定义的索引:第128行的C:\ Program Files(x86)\ EasyPHP-Devserver-17 \ eds-www \ ProjectNet \ edit_profile.php中的用户名
第128行:
u'\u4eba\u53e3\u3058\u3093\u3053\u3046\u306b\u81be\u7099\u304b\u3044\u3057\u3083\u3059\u308b'
u'\u4eba\u53e3\u3058\u3093\u3053\u3046\u306b\u81be\uf9fb\u304b\u3044\u3057\u3083\u3059\u308b'
人口じんこうに膾炙かいしゃする == 人口じんこうに膾炙かいしゃする : True
エレベーター == エレベーター : False
エレベーター == エレベーター : True
hello == HELLO : True
hello == HELLO : False
以下是收到错误的页面的其余代码:
<label>Username: <?php echo $user_info['username'] ?></label>
后端代码:
<?php
include('init.inc.php');
if (isset($_POST['firstname'], $_POST['lastname'], $_POST['email'])){
$errors = array();
if(filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) === false){
$errors[] = 'The email address you entered is not valid.';
}
if(preg_match('#^[a-zA-Z ]+$#i', $_POST['firstname']) === 0){
$errors[] = 'Your first name must only contain a-z characters only.';
}
if(preg_match('#^[a-zA-Z ]+$#i', $_POST['lastname']) === 0){
$errors[] = 'Your last name must only contain a-z characters only.';
}
if (empty($errors)){
set_profile_info($_POST['firstname'], $_POST['lastname'], $_POST['email']);
}
$user_info = array(
'firstname' => htmlentities($_POST['firstname']),
'lastname' => htmlentities($_POST['lastname']),
'email' => htmlentities($_POST['email'])
);
}else{
$user_info = fetch_user_info($_SESSION['u_id']);
}
?>
<!DOCTYPE html PUBLIC "-//W3C/DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns=""http://www.w3.org/1999/xhtml>
<head>
<title>Edit Your Profile</title>
<style type="text/css">
form div {color: white; font-weight: bold; float: left; clear: both; margin: 0px 0px 4px 0px; }
label {font: 19px/1.5 Arial, Helvetica,sans-serif; color: white; font-weight: bold; float:left; clear:both; margin: 0px 0px 4px 0px; }
input[type="text"], textarea {font: 16px/1.5 Arial, Helvetica,sans-serif; margin-left: 10px; float:left; width: 400px; }
input[type="submit"] {
width: 300px;
background: #333;
line-height: 50px;
color: #e3e3e3;
border-radius: 6px;
box-shadow: 0px 0px 2px rgba(0,0,0,.5), 1px 1px 5px rgba(0,0,0,.3);
cursor: pointer;
font-weight: bold;
font: 17px/1.5 Arial, Helvetica,sans-serif;
float: left;
position: absolute;
top: 39%;
}
input[type="submit"]:hover {
background: #e3e3e3;
color: #333;
}
</style>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="./css/style.css">
</head>
<body>
<header>
<nav>
<div class="main-wrapper">
<div id="branding">
<li><h1><span><a href="homepage.php">ProjectNet</a></span></li>
</div>
<nav>
<ul>
<li><a href="homepage.php">Home</a>
<ul>
<li><a href="findGroup.php">Find A Group</a></li>
<li><a href="groupForm.php">Create A Group</a></li>
</ul>
</li>
<li><a href="user_list.php">Members</a></li>
<li><a href="edit_profile.php">Profile</a></li>
</ul>
</nav>
<!--
<ul>
<li><a href="index.php">Home</a></li>
</ul>
-->
<div class="nav-login">
<?php
if (isset($_SESSION['u_id'])) {
echo '<form action="includes/logout.inc.php" method="POST">
<button type="submit" name="submit">Logout</button>
</form>';
} else {
echo '<form action="includes/login.inc.php" method="POST">
<input type="text" name="uid" placeholder="Username/Email">
<input type="password" name="pwd" placeholder="Password">
<button type="submit" name="submit">Login</button>
</form>
<a href="signup.php">Sign up</a>';
}
?>
</div>
</nav>
</header>
<section id="showcase1">
<div>
<?php
if(isset($errors) === false){
echo 'Click update to edit your profile';
}else if(empty($errors)) {
echo 'Your profile has been updated.';
}else{
echo '<ul><li>', implode('</li><li>', $errors), '</li></ul>';
}
?>
</div>
<label>Username: <?php echo $user_info['username'] ?></label>
<form action="" method="post">
<div>
<label for="firstname">First name:</label>
<input type="text" name="firstname" id="firstname" value="<?php echo $user_info['firstname'] ?>" />
</div>
<div>
<label for="lastname">Last name:</label>
<input type="text" name="lastname" id="lastname" value="<?php echo $user_info['lastname'] ?>" />
</div>
<div>
<label for="email">Email: </label>
<input type="text" name="email" id="email" value="<?php echo $user_info['email'] ?>" />
</div>
<!--<div>
<label for="password">Password:</label>
<input type="text" name="password" id="password" value="" />
</div> -->
<div>
<input type="submit" value="Update" />
</div>
</form>
</section>
<footer>
<div class="wrapper">
<nav>
<ul>
<li><a href="about1.php">About</a></li>
<li><a>© 2018 ProjectNet</a></li>
</ul>
</nav>
</div>
</footer>
</body>
</html>
数据库信息: 主键:user_id 数据库中的用户名字段:user_uid
答案 0 :(得分:1)
好的,我的建议是基于你如何构建&#39; $ user_info&#39;阵列。您的表单应如下所示:
<form>
<input type="hidden" name="username" value="<?php echo $user_info['username'] ?>">
the rest of your labels, inputs and submit button go here
</form>
然后你需要修改这段代码:
$user_info = array(
'username' => htmlentities($_POST['username']),
'firstname' => htmlentities($_POST['firstname']),
'lastname' => htmlentities($_POST['lastname']),
'email' => htmlentities($_POST['email'])
);
希望这会有所帮助。如果您需要更多帮助,请回复。另外,在您的下一个学习步骤中,请注意&#39; isset&#39;所以你可以在使用它之前测试它。