简体   繁体   English

根据用户名或ID从数据库中检索特定行

[英]Retrieve a particular row from database based on username or id

Hope some one can help me out here, i created a system with more than 1 user, each user have a unique username and id. 希望有人可以在这里为我提供帮助,我创建了一个系统并拥有多个用户,每个用户都有唯一的用户名和ID。 My problem now is how can i set my system in such a way that when a user log in, the system will direct the user to view a particular row in the database based on their username or id. 我现在的问题是如何设置系统,以便用户登录时,系统将指导用户根据用户名或ID查看数据库中的特定行。

My Code to Access the Database is: 我访问数据库的代码是:

<?php


$con = mysql_connect("localhost","root","");
mysql_select_db("uloaku", $con);
$count = 1;
$y = mysql_query("SELECT * FROM transaction WHERE username = '".$_SESSION['username']."' LIMIT 1");
if(mysql_num_rows($y) != 0){

echo "<table border=\"1\" width=\"800\" >";
echo "<tr id=\"bold\">
<td>No</td>
<td align=\"center\" width=\"120\">Account Owner</td>
<td align=\"center\" width=\"120\">Deposit Date</td>
<td align=\"center\" id=\"bold\" width=\"150\">Current Balance</td>
<td align=\"center\" width=\"150\">Available Balance</td>
<td align=\"center\">Account Status</td>
</tr>";

while ($z = mysql_fetch_array($y, MYSQL_BOTH)){
        echo "<tr>
        <td>".$count++."</td>
        <td id=\"color\" align=\"center\">".$z[1]."</td>
        <td id=\"color\" align=\"center\">".$z[2]."</td>
        <td id=\"color\" align=\"center\">".$z[3]."</td>
        <td id=\"color\" align=\"center\">".$z[4]."</td>
        <td id=\"color3\" align=\"left\">".$z[5]."</td>
        </tr>";
    }
    echo "</table>";
    }
?>

My Login Page Looks Like This: 我的登录页面如下所示:

<?php

session_start();

$_SESSION['username'] = $username;

$username = $_POST['username'];

$password = $_POST['password'];

if ($username&&$password)

{
$connect = mysql_connect("localhost","root","") or die("cannot connect!");
mysql_select_db("uloaku") or die("cannot find data base!");
$query = mysql_query ("SELECT * FROM keyaku WHERE    username='".mysql_real_escape_string($username)."' AND     password='".mysql_real_escape_string($password)."'");
$numrows = mysql_num_rows($query);

if ($numrows!=0)
{
while ($row = mysql_fetch_assoc($query))
{
    $dbusername = $row['username'];
    $dbpassword = $row['password'];
}
if ($username==$dbusername&&$password=$dbpassword)
{
    echo "Welcome $username";
}
else
    echo "Invalid Password";
}
else
die("Invalid User");

}

else
 die("Please Enter a UserName and Password.");

?>

So far my log-in is working as i want it, and my database retrieves information the only problems is that it retrieves all information which is in the database but i want it to retrieve information based on the username or id of the person that logs in 到目前为止,我的登录可以按我想要的方式工作,而我的数据库可以检索信息,唯一的问题是它可以检索数据库中的所有信息,但是我希望它根据登录者的用户名或ID来检索信息。在

First of all, I bet this piece of code does not work at all: 首先,我敢打赌这段代码根本不起作用:

$query = mysql_query ("SELECT * FROM keyaku WHERE        username='$username'&&password='$password'");

... there is no such thing as && in MySQL, therefore the query will return empty result. ... MySQL中没有&&之类的东西,因此查询将返回空结果。 If you want it to work, do it at least this way (prevents SQL injection , too!): 如果您希望它起作用,请至少以这种方式进行(也可以防止SQL注入 !):

$query = mysql_query ("SELECT * FROM keyaku WHERE username='".mysql_real_escape_string($username)."' AND password='".mysql_real_escape_string($password)."'");

Second of all, you need some way to store your login information, so it can be reused later on. 第二,您需要某种方式来存储您的登录信息,以便以后可以重用。 If you can use sessions, the simplest way is to store username (or whichever information is linked to the row you need): 如果可以使用会话,则最简单的方法是存储用户名(或链接到所需行的任何信息):

$_SESSION['username'] = $username;

Then, in your first page, update SQL to read as follows (change the column ID per your needs): 然后,在您的第一页中,将SQL更新如下:(根据需要更改列ID):

$y = mysql_query("SELECT * FROM transaction WHERE username = '".$_SESSION['username']."' LIMIT 1");

... you don't even need the LIMIT 1 part if your username field in transaction table is indexed as a primary/unique key. ...如果交易表中的用户名字段被索引为主键/唯一键,则甚至不需要LIMIT 1部分。

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

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