简体   繁体   English

从行的MySQL数据库获取另一个值

[英]Get another value from MySQL database on row

I have made a simple login and registration system ( http://dak.esy.es/login/ ), and would like to retrieve other information from the same row in the MySQL database. 我已经制作了一个简单的登录和注册系统( http://dak.esy.es/login/ ),并希望从MySQL数据库的同一行中检索其他信息。 Currently, only the username is saved (as $_SESSION['username'] ), and this is what is entered on the login page. 当前,仅保存用户名(作为$_SESSION['username'] ),这是在登录页面上输入的内容。 On the database, the data I want to be saved is id , fullname and email - preferably in the same format as the username ( $_SESSION['(name here)'] ). 在数据库上,我要保存的数据是idfullnameemail -最好采用与用户名( $_SESSION['(name here)'] )相同的格式。 I already have a file to connect to the database called db.php . 我已经有一个文件可以连接到名为db.php的数据库。 If anyone could provide the code I would be greatly pleased. 如果有人可以提供代码,我将非常高兴。 The code on my login.php page is: 我的login.php页面上的代码是:

  <?php
require('db.php');
session_start();
// If form submitted, insert values into the database.
if (isset($_POST['username'])){
    $username = $_POST['username'];
    $password = $_POST['password'];
    $username = stripslashes($username);
    $username = mysql_real_escape_string($username);
    $password = stripslashes($password);
    $password = mysql_real_escape_string($password);
//Checking is user existing in the database or not
    $query = "SELECT * FROM `users` WHERE username='$username' and password='".md5($password)."'";
    $result = mysql_query($query) or die(mysql_error());
    $rows = mysql_num_rows($result);

    if($rows==1){
        $_SESSION['username'] = $username;
        header("Location: ./index.php"); // Redirect user to index.php
    }
        else{
            echo "<div class='form'><br/><h3>Username or password incorrect.</h3><br/>Click here to <a href='login.php'>try again</a></div>";
        }
}
?>

Many thanks. 非常感谢。

It is not clear what the requirement is: but i hope, you want every value from db row as session.. hope you have session: `$_SESSION['username'] after login; 目前尚不清楚要求是什么:但是我希望您希望将db行中的每个值都作为会话..希望您拥有会话:登录后`$ _SESSION ['username'];

    $conn = mysqli_connect($servername, $username, $password);

    // Check connection
    if (!$conn) {
        die("Connection failed: " . mysqli_connect_error());
    }

    $query = "SELECT * FROM `tbl_login` WHERE `username` = ".$_SESSION['username'];
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);
$_SESSION['id'] = $row["id"];
$_SESSION['name'] = $row["fullname"];
$_SESSION['email'] = $row["email"];
mysqli_close($conn);

or for setting session along with login validation: 或用于设置会话以及登录验证:

$conn = mysqli_connect($servername, $username, $password);

// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

$query = "SELECT * FROM `tbl_login` WHERE `username` = ".$_POST['username']." AND `password`= ".$POST['password'];

$result = mysqli_query($conn, $query);
$row = mysqli_fetch_assoc($result);
if(isset($row)){
    $_SESSION['id'] = $row["id"];
    $_SESSION['username'] = $row["username"];
    $_SESSION['name'] = $row["fullname"];
    $_SESSION['email'] = $row["email"];
} else {
    redirect('login.php');   //reload login page on failure
}
mysqli_close($conn);

this is the basic code and you can split this code to MVC. 这是基本代码,您可以将此代码拆分为MVC。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM