我已经制作了一个简单的注册表格。当我注册时,我必须输入电子邮件,密码和全名。但是一旦我注册,它就成功插入到数据库中。但是我想让它在顶级heeader上显示用户的名字。首先,下面的代码如下:
registerForm.php:
<form id="myForm" action="actions/register.php" method="POST">
<p> Email Address : </p>
<p> <input name="email" required="required" type="text" placeholder="eg. john.wick@yahoo.com"/> </p>
<p> Password : </p>
<p> <input name="password" required="required" type="password" placeholder="Your password"/> </p>
<p> Full Name : </p>
<p> <input name="name" required="required" type="text" placeholder="Your full name"/> </p>
<p class="centre"> <input type="submit" value="REGISTER"> </p>
</form>
register.php:
<?php
session_start();
include "../config/dbconnect.php";
$name = ucwords(htmlspecialchars($_POST['name']));
$emailAddress = htmlspecialchars($_POST['email']);
$registerPassword = $_POST['password'];
$name = $mysqli->real_escape_string($name);
$emailAddress = $mysqli->real_escape_string($emailAddress);
$registerPassword = $mysqli->real_escape_string($registerPassword);
$salt = sha1(md5($registerPassword));
$hashedPW = md5($registerPassword.$salt);
$check = "SELECT * FROM memberTable where email = '$emailAddress'";
$checkTitle = mysqli_query($mysqli,$check) or die(mysqli_error($mysqli));
if (mysqli_num_rows($checkTitle) > 0) {
header("location: ../header.php");
} else {
$insertSQL = "INSERT INTO memberTable (email, password, name) VALUES ('$emailAddress', '$hashedPW', '$name')";
$queryResult = mysqli_query($mysqli,$insertSQL) or die(mysqli_error($mysqli));
if($queryResult) {
$user_info = array(
'name' => $name,
);
$_SESSION['login'] = true;
$_SESSION['user_info'] = $user_info;
}
header("location: ../index.php");
exit;
}
mysqli_close($mysqli);
?>
如果未成功注册,则会显示登录表单。注册成功,它将直接到header.php。要知道我已将我的全名,电子邮件和密码插入数据库。我希望我的全名显示在标题上。如下所示:
<div class="topRight">
<?php
ini_set('display_errors',1);
include "config/dbconnect.php";
if(isset($_SESSION['login']) && !empty($_SESSION['login'])) {
$name = $_SESSION['user_info']['name'];
echo '<p> <a href="profileUser"> Profile : $name </a>';
echo '<p> <a href="actions/logout.php"> Logout </a> </p>';
} else {
echo '<form action="actions/login.php" method="POST">
<input type="text" name="email" placeholder="Email">
<input type="password" name="password" placeholder="Password">
<input type="submit" value="LOGIN">
</form>';
echo '<p> <a href="registerForm.php" class="underline"> Not a member? Register here! </a> </p>';
}
?>
</div>
问题是Profile : $name
无效。它没有显示名称,只是在网页上显示$name
。有什么想法吗?
答案 0 :(得分:0)
单引号内的变量不会被解析。 PHP Documentation
答案 1 :(得分:0)
试试这个。,
echo '<p> <a href="profileUser"> Profile :'.$name.'</a>';
你应该保持正确关闭报价,