Como obter o user_id do usuário logado do banco de dados e inseri-lo na outra tabela usando Sessões? [fechadas]

0

Sou novo no tópico Sessões, por isso, tentei obter a user_id (coluna) da pessoa logada da tabela user_login usando as sessões e inseri-la na outra tabela, mas não consegui. Eu apreciaria se alguém pudesse me ajudar em como alcançar.

Obrigado antecipadamente.

Here's my code where I want to insert that user_id

<?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'])) {
            //$user_id = $_SESSION['user_id'];
            $query = $pdo -> prepare("INSERT INTO cart_items(product_id ,  Price) VALUES (?,?)");
            $query -> bindValue(1, $id);
            //$query -> bindValue(2, $user_id); //This is where I want to insert logged user's user_id in to the database.
            $query -> bindValue(2, $data['new_price']);
            $query ->execute();
            header('location:cart.php');
        }
        else{
            header('location:Login Page.php');
        }
    }
?>

Here's my Login.php(Login Page)

<?php
include ('header.php');
include ('includes/connect.php');
if( ! isset($_SESSION['logged_in'])) {
    if(isset($_POST['email'] , $_POST['password']))
    {
            $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";
                }
            }
    }
}
else{
    $error = "you have already logged in";
}
?>
 <div class="login">
            <div class="container">
                <div class="login-grids">
                    <div class="col-md-6 log">
                             <h3>Login</h3>
                             <div class="strip"></div>
                             <p>Welcome, please enter the following to continue.</p>
                             <form action="" method="post">
                                <?php if(isset($error)){ ?>
                                        <small style = "color : #aa0000"; ><?php echo $error ?></small>
                                        <br><br>
                                <?php } ?>
                                <div class="form-group">
                                    <label for="email"  class="head">Email</label>
                                    <input class="form-control login-box" name="email"  type="text" id="email"/>
                                </div>
                                <div class="form-group">
                                    <label for="password"  class="head">Password</label>
                                    <input class="form-control login-box" name="password"  type="password" id="password"/>
                                </div>          
                                 <button type="submit" class="btn login-btn">Submit</button>
                             </form>
                            <a href="#">Forgot Password ?</a>
                    </div>
                    <div class="col-md-6 login-right">
                            <h3>New Registration</h3>
                            <div class="strip"></div>
                            <p>By creating an account with our store, you will be able to move through the checkout process faster, store multiple shipping addresses, view and track your orders in your account and more.</p>
                            <a href="register.html" class="button">Create An Account</a>
                    </div>
                    <div class="clearfix"></div>
                </div>
            </div>
        </div>
    
por srikar reddy 29.06.2017 / 20:58

1 resposta

0

Você pode obter o user_id com:

$query=mysql_query("SELECT * FROM user_login WHERE name = '$username' AND password = '$password');

$row=mysql_fetch_assoc($query);
$num=mysql_num_rows();

if($num==1)
{
    session_start(); 
    $_SESSION['user_id']= $row['user_id'];
}

Não tenho certeza, mas acho que o mssql_fetch_assoc se o comando php para obter o assoc de busca para sql.

O 'user_id' dentro dos parênteses $ row é o nome da coluna na sua tabela

    
por 29.06.2017 / 21:40