我有一个编辑表单,一旦他们登录并希望编辑他们的详细信息,就应该在表单中显示用户详细信息。如果我在实际页面本身使用代码我没有问题,但我已将所有内容放在函数中,并且不能使用表单内的变量。如何在不使用全局的情况下执行此操作?
功能:
function updateProfile($link){
$sql = "SELECT user_firstname, user_lastname, user_email
FROM `users`
WHERE user_username = '{$_SESSION['username']}'";
$result = $link->query($sql);
if($result->num_rows === 1){
while($row = $result->fetch_assoc()){
$db_firstname = htmlentities($row['user_firstname']);
$db_lastname = htmlentities($row['user_lastname']);
$db_username = htmlentities($_SESSION['username']);
$db_email = htmlentities($row['user_email']);
$db_userid = $row['user_id'];
}
}
$message = "";
if(isset($_POST['updateprofile'])){
if(empty($_POST['user_firstname'])) {
$message .= "First name required<br>";
}
if(empty($_POST['user_lastname'])) {
$message .= "Last name is required<br>";
}
if(empty($_POST['user_username'])) {
$message .= "Username is required<br>";
}
if(empty($_POST['user_email'])) {
$message .= "Email address required";
}
if(!filter_var($_POST['user_email'], FILTER_VALIDATE_EMAIL)) {
$message .= "Email address invalid";
}
if(!empty($_POST['someinput'])) {
$message .= "Be gone spam bot!";
}
if ($message) {
$message = "<div class='alert alert-danger'><strong>There were errors in your form:<br></strong>" .$message. "</div>";
} else {
$user_firstname = $link->real_escape_string($_POST['user_firstname']);
$user_lastname = $link->real_escape_string($_POST['user_lastname']);
$user_username = $link->real_escape_string($_POST['user_username']);
$user_email = $link->real_escape_string($_POST['user_email']);
$update_profile = "UPDATE `users`
SET user_firstname = '$user_firstname',
user_lastname = '$user_lastname',
user_username = '$user_username',
user_email = '$user_email'
WHERE user_id = '$db_userid'";
if($result = $link->query($update_profile) === TRUE){
$message = "<div class='alert alert-success>User profile updated</div>";
}
}
}
return $message;
}
表格:
<form action="" method="post" enctype="multipart/form-data">
<div class="form-group">
<label for="firstname">First name</label>
<input type="text" class="form-control" name="user_firstname" value="<?php echo $db_firstname;?>"> </div>
<div class="form-group">
<label for="lastname">Last Name</label>
<input type="text" class="form-control" name="user_lastname" value="<?php echo $db_lastname;?>"> </div>
<div class="form-group">
<label for="Username">Username</label>
<input type="text" class="form-control" name="user_username" value="<?php echo $db_username;?>"> </div>
<div class="form-group">
<label for="form-group">Email</label>
<input type="email" class="form-control" name="user_email" value="<?php echo $db_email;?>"> </div>
<div class="form-group">
<label for="form-group">Password</label>
<input type="password" class="form-control" name="user_password"> </div>
<div class="form-group">
<label for="form-group">Repeat password</label>
<input type="password" class="form-control" name="password_repeat"> </div>
<div class="form-group">
<input type="text" style="display:none" name="someinput"> </div>
<div class="form-group">
<input class="btn btn-primary" type="submit" name="updateprofile" value="Update details"> </div>
<div>
<?php echo updateProfile($link);?>
</div>
</form>
答案 0 :(得分:0)
关于是否需要更改这些值:
A)如果您的功能以外的值不需要能够更改:
定义常量:
if(!defined('CONSTANT')) {
DEFINE('CONSTANT', 20);
}
retreive:
if(defined('CONSTANT_NAME')) $value = CONSTANT_NAME;
B)如果您的值需要更改:
将您的函数包装到一个类中,因此您得到一个方法updateProfile($link)
,如下所示:
class YourClass {
private $myVariable = 10;
function updateProfile($link) {
...
}
function anotherFunction() {
...
// access your property
$variable = $this->myVariable;
}
}
或唯一的方法是按照您的建议使用global
- 但它根本不安全。