bindParam是否可以成为Session变量?

时间:2015-04-28 05:29:44

标签: php

我想要实现的是当用户登录时,用户ID被保存为会话值,当登录用户选择日期时,产品将根据仅从该客户显示的日期显示。

确认此处有2个问题: 1,如何在Session中保存用户ID?,2,我如何bindParam sessionid?

以下是代码方面的内容:

When user logs in:
    $_SESSION['customer_loggedin'] = $row['id']; 
THIS DOES NOT DISPLAY THE ID ON ROW?

当用户选择日期时,Ajax发送到:

session_start();
    include('db_config.php');
    $datepicker = $_POST['dateorderpicker'];

    $sql = "SELECT * FROM orders WHERE deliveryDate = ? AND cusomerId= ?"; 
    $stmt = $conn->prepare($sql);
    $stmt->bindParam(1, $datepicker, PDO::PARAM_STR);

    // HOW DO I BIND PARAM THE SESSION ID?

    $stmt->execute();
    while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
    {   
        ?>
            <div class="col-sm-4 col-md-4">
                <div class="content-boxes style-two top-column clearfix animated flipInY" style="opacity: 1;">
                <div class="content-boxes-text">
                    <h4><?php echo $row['itemName']; ?></h4>
                <img src="../wholesale/img/sourdough.jpg" class="img-reponsive">
                <p>Our best seller.  Full of flavour.</p>
                <form class="form-inline pull-right">
                <div class="form-group">
                <label class="sr-only" for="exampleInputAmount">Qty</label>
                <div class="input-group">
                <input type="number" class="form-control" id="exampleInputAmount" placeholder="<?php echo $row['qty']; ?>">
                </div>
                </div>
                <button type="submit" class="btn btn-primary">Update</button>
                </form>
                </div>
               </div>
              </div>
        <?php
    }

如果您需要任何其他信息,请随时提出。 我还在学习,如果你看到不对的东西请告诉我,这样我可以更好的我的PHP :) 感谢

    if($login && $password)
    {
        $sql = "SELECT * FROM customer WHERE (code=:code OR phone=:phone OR businessName=:businessName OR email=:email) AND password=:password";
        $stmt = $conn->prepare($sql);
        $stmt->bindValue(':code', $login, PDO::PARAM_STR);
        $stmt->bindValue(':phone', $login, PDO::PARAM_STR);
        $stmt->bindValue(':businessName', $login, PDO::PARAM_STR);
        $stmt->bindValue(':email', $login, PDO::PARAM_STR);
        $stmt->bindValue(':password', $password, PDO::PARAM_STR);
        $stmt->execute();
        $num = $stmt->fetchColumn();
        if($num > 0)
        {
        $_SESSION['customer_loggedin'] = $row['id'];
            header('location:order.php');
        }
        else{
            header('Location:index.php');
            $_SESSION['errmsg'] = 'Incorrect Login Details';
            die;
        }
    }
}  

2 个答案:

答案 0 :(得分:1)

  

如何在Session中保存用户ID?

您只需启动会话并将其设置为任何其他变量,就像您在问题中一样。只需在其前添加session_start()

session_start();
$_SESSION['customer_loggedin'] = $row['id'];
  

我如何bindParam sessionid?

同样,它就像绑定任何其他参数一样。致电session_start()后,所有先前设置的会话变量都会被放入$_SESSION数组。

$stmt->bindParam(2, $_SESSION['customer_loggedin'], PDO::PARAM_INT);

答案 1 :(得分:0)

将会话变量分配给变量绑定它: -

 $row_id=$_SESSION['customer_loggedin'];
 $sql = "SELECT * FROM orders WHERE deliveryDate = ? AND cusomerId= ?"; 
 $stmt = $conn->prepare($sql);
 $stmt->bindParam(1, $datepicker, PDO::PARAM_STR);

 $sth->bindValue(2, $row_id, PDO::PARAM_STR);
 $stmt->execute();