简体   繁体   中英

PHP using session in a MYSQL query

I can echo the session name so i know its working but it doesn't work in a MySQL query. I even tried turning it into a variable and its still not working:

File list: index.php response.php

$repname = $_SESSION['name'];
$sql = "SELECT * FROM `employee` WHERE rep='".$repname."' ";

also

$sql = "SELECT * FROM `employee` WHERE rep=".$_SESSION['name']." ";

any ideas on what's wrong?

UPDATE**

Here's the right query

        $sql = "SELECT * FROM `employee` WHERE rep='".$_SESSION['name']."' ";

I know its the right query because now i'm getting records to display but its only records where rep is blank. This means i'm not getting the session name for some reason.

I tried adding:

     session_start();
 if(isset($_GET['name'])){
 $_SESSION['name']=$_GET['name'];
  }

But im still only getting records where rep is blank

Be sure to add session_start() to the top of your session to initiate your session start

    <?php
    session_start(); ?>

Also be sure to add this to retrieve your name field:

<?php 
     session_start();
     if(isset($_GET['name']){
     $_SESSION['name']=$_GET['name'];

 ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM