doufangzhang4454 2017-06-30 08:52
浏览 103
已采纳

如何从数据库中获取登录用户user_id并将其插入另一个表?

I've been trying to get logged persons user_id which is in user_login table in my database and insert it to the another table. What I know is, get the user_id from the user_login using query and assign that query to the SESSION variable and put that variable in another query where I want to insert it in the table but I'm unable to write a perfect code for it.The only thing I'm achieving is it taking the user_id as 1 in the new table.It would be a great help if you can review my code correct it.

    <?php 
    ob_start();
    include ('header.php');
    require('includes/connect.php');
    require('includes/product.php');

    $product = new Product;         
    if(isset ($_GET['id'])) {
        $id = $_GET['id'];
        $data = $product -> fetch_data($id);    

        if(isset($_POST['add'])){
            if (isset($_SESSION['logged_in'])) {
                $query = $pdo->prepare("SELECT user_id FROM user_login ");
                $user_id=$query->execute();
                $_SESSION['user_id']['id']=$user_id;
                $query = $pdo -> prepare("INSERT INTO cart_items(product_id , user_id, Price) VALUES (?,?,?)");
                $query -> bindValue(1, $id);
                $query -> bindValue(2, $_SESSION['user_id']['id']);
                $query -> bindValue(3, $data['new_price']);
                $query ->execute();
                header('location:cart.php');
            }
            else{
                header('location:Login Page.php');
            }
        }
?>

This is where I have done the user validation while logging in (connect.php)

    <?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "ecom";
try{
    $pdo = new PDO('mysql:host=localhost;dbname=ecom','root','');
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e){
    exit('Database error.');
}

 function login(){
     global $pdo;
     $username = $_POST['email'];
                $password = md5($_POST['password']);
                if(empty($username) or empty($password))
                {
                    $error = "Please fill all the fields";
                }
                else
                {
                    $query = $pdo->prepare("SELECT * FROM user_login WHERE name = ? AND password = ? ");
                    $query->bindValue ( 1, $username);
                    $query->bindValue ( 2, $password);
                    $query->execute();
                    $num=$query->rowCount();

                if($num==1) {
                    $_SESSION['logged_in']= true;
                    //header('location :' .$_SESSION['redirectURL']);
                    header('location: index.php');
                    exit();
                }
                else{
                    $error = "Please enter correct Username and Password";
                }
            }


}


?>

Thank you in advance.

  • 写回答

1条回答 默认 最新

  • dongyongyin5339 2017-06-30 09:24
    关注

    replace your else part with this code

    $query = $pdo->prepare("SELECT * FROM user_login WHERE name = ? AND password = ? ");
    $query->bindValue ( 1, $username);
    $query->bindValue ( 2, $password);
    $query->execute();
    $data = $query->fetchAll(PDO::FETCH_ASSOC);
    $num=$query->rowCount();
    
    if($num==1) {
        $_SESSION['logged_in']= true;
        $_SESSION['user_id'] = $data[0]["user_id"];
        //header('location :' .$_SESSION['redirectURL']);
        header('location: index.php');
        exit();
    } else{
         $error = "Please enter correct Username and Password";
    }
    

    then you can get user id from session

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧