我想显示表格中的用户数据,因此一旦用户登录网站,页面上会显示文字,上面写着"欢迎(名字)(姓氏)。"
名字的列是"名字"姓氏是"姓氏"。 我尝试过很多不同的方法,但是所有这些方法似乎都会返回错误而我无法使用它。如果有人可以提供帮助,那就太棒了。
这是我的代码:
<html>
<head>
<title>Landing page</title>
<link rel="stylesheet" type="text/css" href="css.css">
</head>
<body>
<?php
/* ESTABLISH FIRST YOUR CONNECTION TO YOUR DATABASE */
$con = new mysqli("localhost", "root", "", "user_login"); /* REPLACE NECESSARY DATA */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
if($stmt = $con->prepare("SELECT Username, Password FROM users WHERE Username = ? AND Password = ?")){
$stmt->bind_param("ss",$_POST["username"],$_POST["password"]);
$stmt->execute();
$stmt->store_result();
$checklog = $stmt->num_rows;
if($checklog > 0){
/* HERE IS YOUR CODE WITH SUCCESSFUL LOGIN */
session_start();
$_SESSION["logged_in"] = "YES";
echo "<h1>You are now logged in</h1>";
echo "<p><a href='secure1.php'>Link to protected file</a></p>";
echo "<p><a href='secure2.php'>Link to protected file #2</a></p>";
}
else {
/* HERE IS YOUR CODE WITH UNSUCCESSFUL LOGIN */
session_start();
$_SESSION["logged_in"] = "NO";
echo "<h1>You are NOT logged in </h1>";
echo "<p><a href='secure1.php'>Link to protected file</a></p>";
echo "<p><a href='secure2.php'>Link to protected file #2</a></p>";
}
$stmt->close();
}
?>
<p><a href="public.html">Public Page</a></p>
<p><a href="logout.html">Logout</a></p>
</body>
</html>
我在做什么错,我该如何解决?
答案 0 :(得分:1)
您可以执行$stmt->fetch(PDO::FETCH_ASSOC)
并将结果存储在$result
$result = $stmt->fetch(PDO::FETCH_ASSOC)
$Firstname = $result["Firstname"];
由于您可能只有一个匹配的行,我建议您的条件必须为$checklog==1
所以,你应该有这样的东西
$checklog = $stmt->num_rows;
if($checklog == 1) // Allows only for one match
{
session_start();
$_SESSION["logged_in"] = "YES";
$result = $stmt->fetch(PDO::FETCH_ASSOC); // Fetch the row into $result
$Firstname = $result["Firstname"]; // This is your First Name
$Lastname = $result["Lastname"]; // This is your Last Name
}
您可以在此会话中拥有First Name
和Last Name
$_SESSION["FirstName"] = $result["Firstname"];
$_SESSION["LastName"] = $result["Lastname"];
更新:
以下是专为您精心制作的酷炫且完全更新的版本:)
我花了10分钟来构建它:)
<?php
$conn = new PDO("mysql:host=localhost;dbname=user_login",'root','');
$name = $_POST["username"];
$pass = $_POST["password"];
$sql = "SELECT * FROM users WHERE Username = ? AND Password = ?";
$q = $conn->prepare($sql);
$q->execute(array($name,$pass));
$count = $q->rowCount();
if ($count==1)
{
$q->setFetchMode(PDO::FETCH_BOTH);
while($row = $q->fetch())
{
echo $row['FirstName']; //Just to show you output
session_start();
$_SESSION["FirstName"] = $row['FirstName'];
}
}
?>
答案 1 :(得分:0)
试试这个..
<?php if($stmt = $con->prepare("SELECT firstname,lastname,Username, Password FROM users WHERE Username = ? AND Password = ?")){
$stmt->bind_param("ss",$_POST["username"],$_POST["password"]);
$stmt->execute();
//$stmt->store_result();
//$checklog = $stmt->num_rows;
$results = $stmt->fetch(PDO::FETCH_ASSOC);
$_SESSION['firstname'] = $results['firstname'];
$_SESSION['lastname'] = $results['lastname'];
print_r($_SESSION);