显示SQL数据库中的特定数据

时间:2016-07-21 04:55:15

标签: php mysql select html-table

我有一个名为'成员'用一个名为' users'的表格。用户表中有一个' id'每次添加新用户时自动递增的列。我想获取登录用户的id并显示该单行数据。 这是我的登录代码:

    <?php
    session_start();
    // Connect to server and select databse.
    mysql_connect("localhost", "root", "")or die("cannot connect"); 
    mysql_select_db("members")or die("cannot select DB");
    // username and password sent from form 
    $prepreemail=$_POST['email']; 
    $prepremypassword=$_POST['mypassword']; 
    $preemail = stripslashes($prepreemail);
    $premypassword = stripslashes($prepremypassword);
    $email = mysql_real_escape_string($preemail);
    $mypassword = mysql_real_escape_string($premypassword);
    $sql="SELECT * FROM users WHERE email='$email' and password='$mypassword'";
    $result=mysql_query($sql);


    // Mysql_num_row is counting table row
    $count=mysql_num_rows($result);
    // If result matched $myusername and $mypassword, table row must be 1 row
    if($count==1){
    // Register $myusername, $mypassword and redirect to file "login_success.php"
    header ("location:home.html");
    }
    else 
    {
    header ("location:sign_in.php");
    }
    ?>

这是我的注册码:

    <?php
    session_start();
    $con = mysql_connect("localhost","root","");
    if (!$con)
      {
      die('Could not connect: ' . mysql_error());
      }

    mysql_select_db("members", $con);

    $sql="INSERT INTO users (name, email, address, dob, password)
    VALUES
    ('$_POST[name]','$_POST[email]','$_POST[address]','$_POST[dob]','$_POST[mypassword]')";

    if (!mysql_query($sql,$con)) die('Error: ' . mysql_error());
    $_SESSION['userID'] = "SELECT id from users";

    {
        header ("Location: sign_in.php");
        exit;
    }

    mysql_close($con)
    ?>

这是我的显示用户详细信息代码:

    <?php
    session_start();
    // Create connection
    $conn = new mysqli("localhost", "root", "", "members");
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } 
    $userID = $_SESSION['userID'];
    $sql = "SELECT name, email, address, dob FROM users WHERE `users`.`id`=$userID";
    $result = $conn->query($sql);

    if ($result->num_rows > 0) {
        echo "<table><tr><th>Name</th><th>Email</th><th>Address</th><th>DOB</th></tr>";
        // output data of each row
        while($row = $result->fetch_assoc()) {
            echo "<tr><td>".$row["name"]."</td><td>".$row["email"]."</td><td>".$row["address"]."</td><td>".$row["dob"]."</td></tr>";
        }
        echo "</table>";
    } else {
            echo "<p>No Details</p><style>p{color: #336699; font-size: 20pt; font-family: Gulim; font-weight: bold; align: center; padding: 10px; position: absolute; top: 250; left: 600; z-index: 2; border-spacing: 0px;}</style> ";
    }
    $conn->close();
    ?>

由于某种原因,它不起作用,有人请帮助!

1 个答案:

答案 0 :(得分:0)

实际上你的代码有几个错误......并且不清楚你的问题......我认为你的问题和代码需要进行一些更改。请在从数据库中获取信息时设置会话变量。

<?php
session_start();
// Connect to server and select databse.
mysql_connect("localhost", "root", "")or die("cannot connect"); 
mysql_select_db("members")or die("cannot select DB");
// username and password sent from form 
$prepreemail=$_POST['email']; 
$prepremypassword=$_POST['mypassword']; 
$preemail = stripslashes($prepreemail);
$premypassword = stripslashes($prepremypassword);
$email = mysql_real_escape_string($preemail);
$mypassword = mysql_real_escape_string($premypassword);
$sql="SELECT * FROM users WHERE email='$email' and password='$mypassword'";
$result=mysql_query($sql);
 $count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"
    $row=mysql_fetch_array($result);
    $_SESSION['id']=$row['id'];
    if($_SESSION['id'])
    {
header ("location:home.php");
    }
}
else 
{
header ("location:sign_in.php");
}
?>

在此记录的人员中,id存储在$ _SESSION ['id']中。您可以在PHP页面home.php中获取此值,如下所示

<?php
session_start();
$logid=$_SESSION['id'];
echo $logid;

请在显示用户详细信息代码中进行一些更改,如下所示。

<?php
session_start();
// Create connection
 $conn = new mysqli("localhost", "root", "", "members");
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
$userID = $_SESSION['id'];
$sql = "SELECT name, email, address, dob FROM users WHERE `users`.`id`=$userID";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table><tr><th>Name</th><th>Email</th><th>Address</th><th>DOB</th></tr>";
    // output data of each row
while($row = $result->fetch_assoc()) {
        echo "<tr><td>".$row["name"]."</td><td>".$row["email"]."</td><td>".$row["address"]."</td><td>".$row["dob"]."</td></tr>";
    }

 echo "</table>";
} else {
 echo "<p>No Details</p><style>p{color: #336699; font-size: 20pt; font-family: Gulim; font-weight: bold; align: center; padding: 10px; position: absolute; top: 250; left: 600; z-index: 2; border-spacing: 0px;}</style> ";
}
$conn->close();