从数据库中检索当前用户ID以在新表中使用 [英] Retrieve current users id from database to use in new tabel

查看:115
本文介绍了从数据库中检索当前用户ID以在新表中使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个个人资料页面,可以在其中检索用户信息.

I have a profile page where I retrieve users information.

Profile.php

  <?php
require 'core/init.php';

if(!$username = Input::get('user')) {
    Redirect::to('index.php');
} else {
    $user = new User($username);

    if(!$user->exists()) {
        Redirect::to(404);
    } else {
        $data = $user->data();
    }
    ?>

    <h3><?php echo escape($data->username); ?></h3>
    <p>Membership No: <?php echo escape($data->id); ?></p>
    <p>Full name: <?php echo escape($data->name); ?></p>
    <p>Date of birth: <?php echo escape($data->dob); ?></p>
    <p>Location: <?php echo escape($data->location); ?></p>
    <p>Join date: <?php echo escape($data->joined); ?></p>



    <?php

我想检索用户ID以插入到我的订单页面中的另一个表中,到目前为止,我已经拥有了

I want to retrieve the id of my user to insert into another table in my order page, so far I have this

oerder.php

    <?php
session_start();
require 'core/init.php';

$Band_id = mysql_real_escape_string($_POST['band']);
$user_id = $_SESSION['id'];

 $sql = "INSERT INTO orders (band_id,user_id) VALUES('$Band_id', '$user_id')";
mysql_query ($sql, $linkme)
    or die ("could not add to database");
?>

当前$user_id = $_SESSION['id'];没有将用户ID放在我的表orders中.

currently $user_id = $_SESSION['id']; is not placing the users id in my table orders.

我尝试了

<?php echo escape($data->id); ?>

$user_id = $_GET['id'];

但是它不起作用,有人知道如何检索用户id,所以我可以将其插入数据库?

but it dose not work, dose anyone know how to retrieve the users id so I can insert it into the db?

推荐答案

您可以做的就是将用户数据保存到会话中

What you could do is save user data to the session

$_SESSION['user_data'] = $user->data();

一旦设置了$_SESSION['user_data'],您可以将其分配回$data,否则重新查询模型.

you could assign it back to $data once you check $_SESSION['user_data'] is set, else re query the model.

session_start()也应该位于您要为其保留会话的每个文件的顶部.

And session_start() should also be at the top of every file you want to hold session for.

类似这样:

Profile.php

<?php
session_start();
require 'core/init.php';

if(!$username = Input::get('user')) {
    Redirect::to('index.php');
    exit;
}

if(!isset($_SESSION['user_data'])){
    $user = new User($username);

    if(!$user->exists()) {
        Redirect::to(404);
        exit;
    }

    $_SESSION['user_data'] = $user->data();
}
?>

<h3><?php echo escape($_SESSION['user_data']->username); ?></h3>
<p>Membership No: <?php echo escape($_SESSION['user_data']->id); ?></p>
<p>Full name: <?php echo escape($_SESSION['user_data']->name); ?></p>
<p>Date of birth: <?php echo escape($_SESSION['user_data']->dob); ?></p>
<p>Location: <?php echo escape($_SESSION['user_data']->location); ?></p>
<p>Join date: <?php echo escape($_SESSION['user_data']->joined); ?></p>

oerder.php

<?php
session_start();
require 'core/init.php';

if(!isset($_SESSION['user_data'])){
    Redirect::to('index.php');
    exit;
}

$Band_id = mysql_real_escape_string($_POST['band']);
$user_id = $_SESSION['user_data']->id;

$sql = "INSERT INTO orders (band_id,user_id) VALUES('$Band_id', '$user_id')";
mysql_query ($sql, $linkme)
or die ("could not add to database");
?>

您还应该转到PDO或mysqli.

also you should move over to PDO or mysqli.

这篇关于从数据库中检索当前用户ID以在新表中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆