如何获取特定用户的数据

时间:2016-04-28 10:07:09

标签: mysql

我正在为私人货运公司开发货物预订网站。我已经向用户提供了他们可以在注册后预订航班的设施。和管理员将根据用户的输入生成报价。我创建了报价表单,但我想以该表单获取特定的用户数据。任何人都可以指导怎么做?

1 个答案:

答案 0 :(得分:0)

  

以下是预订用户列表|战斗|路线|货币|日期|   时间|出发| Qtn | Inv Regd USR | G5550 |单向|美元|   2016年5月5日| 02:00 |妈妈|杜| 15 | 450 | Qtn btn | INV btn现在   报价页面,我想要获取输入的记录   上面的表格,但应该是用户特定的..请帮助 -

现在,您的MySQL数据库中有一个名为“booking”的表。您可以编写本机SQL来检索具有特定“user_id”的“用户”的记录。在这里,我们还假设我们有一种动态了解的方法。检索“user_id”(可能来自会话或通过任何API等)。假设您使用的是PHP;您的SQL查询可以读取如下内容:

<?php
    //YOU MUST HAVE A WAY OF ACCESSING THE USER-TOKEN: PERHAPS YOU ALREADY HAVE A USER OBJECT IN THE SESSION... IF SO, YOU COULD JUST ACCESS ALL USER DATA DIRECTLY USING THIS OBJECT...
    $user_token = "figure a way to get the user_id or token"; //\User::getLoggedInUserObject(); //ID COMING FROM A DYNAMIC SOURCE, $_SESSION, ETC...
    // IF YOU DON'T HAVE SOMETHING LIKE A GENERIC USER OBJECT BUT JUST USER TOKEN OR ID AND YOU MUST RUN SQL QUERIES; THEN HERE IS THE QUERY...
    $sql = "SELECT * FROM booking WHERE User='" . $user_token  . "' LIMIT 1";
    // WITH A USER: "G5550" YOUR SQL COULD LOOK SOMETHING LIKE THIS:
    // $sql = "SELECT * FROM booking WHERE User='G5550' LIMIT 1";
    //PERFORM THE DATABASE QUERY AND RETRIEVE THE USER OBJECT - USE IT IN YOUR FORM

获得查询结果后,您可以使用它来预填充表单...

如果您使用的是PDO,我建议您在引用页面上执行以下操作:

<?php
    defined("HOST") or define("HOST", "localhost");
    defined("DBASE") or define("DBASE", "your-database-name");
    defined("USER") or define("USER", "root"); //REPLACE WITH YOUR DB-USER
    defined("PASS") or define("PASS", "root"); //REPLACE WITH YOUR DB-PASS
    $user_id = "G5550"; // HERE YOU HAVE TO HAVE A WAY OF GETTING THE USER ID OR SO...

    try {
        $dbh = new PDO('mysql:host='.HOST.';dbname='. DBASE,USER,PASS); 
        $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

        $stmt = $dbh->prepare('SELECT * FROM booking WHERE User = :user_id');
        $stmt->execute(['user_id' => $user_id]);
        $user = $sth->fetch(PDO::FETCH_OBJ);    //AS OPPOSED TO USING PDO::FETCH_ASSOC SINCE WE ARE INTERESTED IN ONLY ONE USER OBJECT);
        //Now $user is an Object containing all the information you need... Flight, Rout, Date, Time, etc.
        //And you can access each data using its Name: for example - 
        //$user->Route; $user->Departure; $user->Flight;
    }catch(PDOException $e){
        echo $e->getMessage();
    }

现在,这是有趣的部分:在您的表单中,您必须使用这些值填充字段,以便用户不必再次重新键入信息。所以,你在表格上这样做:

<html>
...
<body>
    <form>
        <label for="Flight" class="lbl-quotation">Flight:</label><br/>
        <input type="text" class="input-box Flight" id="Flight" value="<?php echo ( isset($user->Flight)? $user->Flight: ""); ?>" /><br/>
        <label for="Route" class="lbl-quotation">Flight:</label><br/>
        <input type="text" class="input-box Route" id="Route" value="<?php echo ( isset($user->Route)? $user->Route: ""); ?>" /><br/>
        <label for="Departure" class="lbl-quotation">Departure:</label><br/>
        <input type="text" class="input-box Departure" id="Departure" value="<?php echo ( isset($user->Departure)? $user->Departure: ""); ?>" /><br/>
    </form>
</body>
</html>

希望这有点帮助...