如果我在数据库中有两个表,并且我想从一个表用户id获取到其他表数据,那么如何从php中的数据库中获取数据[关闭]

如果我在数据库中有两个表,并且我想从一个表用户id获取到其他表数据,那么如何从php中的数据库中获取数据[关闭]

问题描述:

I have created two table name is 'orders' and 'registration' in one database. when I am registering that is working fine and when i am trying to logged in with current registration that is also working fine. but

problem is only that suppose 5 users registered there and they submitted different orders. after loggin while going to see history of orders all users are seeing the same history and all are able to see each others orders in his account after logged in . I am only trying to stop this. so that only logged users can see their own submission only. they should be able to see others user order

我在一个数据库中创建了两个表名,即'orders'和'registration'。 当我正在注册时工作正常,当我尝试使用当前注册登录时工作正常。 但 p>

问题仅在于假设有5个用户在那里注册并且他们提交了不同的订单。 在登录后,在查看订单历史记录时,所有用户都看到相同的历史记录,所有用户都可以在登录后查看其帐户中的其他订单。 我只想阻止这一点。 这样只有登录的用户才能看到自己的提交。 他们应该能够看到其他用户订单 p> div>

As database schema is not available, I'm assuming the structure like this. Change it where you need.

registration table

Id | firstName | lastName | email | ....

Where, Id is Primary Key and auto-incremented

orders table

orderId | userID | orderName | ...

Where, orderId is Primary Key and auto-incremented ; userID is Id of registration table.

So, when you are logged in through registration table. Create one session for Id.

$_Session['user_id']; (Id of registration table will be stored here.)

For viewing his/her orders

<?
$userID=$_SESSION['user_id'];
$orderQuery="SELECT * FROM orders WHERE userID=$userID";
.
.//Execute Your query
.
?>

So in orders table you need to add column user id related with foreign key to table users and select orders only for logged/choosed user by id.

For exaple when user is logged you need to get his unique id. And when he wants to see orders query onle results for this particular user. For ex.

 set @loggeduser = session_id() from php. 
 Select * from oreders where userid=@loggeduser;

I can't comment, yet.

Like David said, you could just use something like:

Select * from order
where userid = logedinuserid

but you should also consider to rename the table order. I just asume you have the UserID in the Orders Table...