dongxiaowei_1234 2019-02-14 19:27 采纳率: 0%
浏览 38

从另一个脚本获取用户登录时的表列值

I have a header script which runs some user check etc. One of those checks is his username his privileges and number of answers left.

this is that script called header.php

if(isset($_SESSION['activeUser']))
{
 $activeUser=$_SESSION['activeUser'];
 $activeUserName=$_SESSION['activeUserName'];
 $activeUserType=$_SESSION['activeUserType'];
 $activeUserId=$_SESSION['activeUserId'];
 $odgovori=$_SESSION['preostaliOdgovori'];

}


   if(isset($_SESSION['activeUser']))
 {

    echo "<div style='float: right;'>";
    echo "<h3>Prijavljeni ste kao: ".$_SESSION['activeUser']."</h3>";
    echo "<h3>Prava: ".$_SESSION['tipNaziv']."</h3>";
    echo "<h3>Preostali odgovori: ".$odgovori."</h3>";
    echo"</div>"; 
}
else
{
    echo "<h3 style='float: right;'>Niste prijavljeni!</h3>";
}

So I want to read preostaliOdgovori from another script (preostaliOdgovori is table column) mojaTvrtka.php is a script where this is happening

$connect = connectDB();
$query ="SELECT pitanje.pitanje_id,naslov,pitanje.tvrtka_id, tvrtka.tvrtka_id,preostaliOdgovori ,
    zaposlenik.tvrtka_id,korisnik_id
    FROM pitanje 
    INNER JOIN tvrtka ON tvrtka.tvrtka_id = pitanje.tvrtka_id
    INNER JOIN zaposlenik ON zaposlenik.tvrtka_id = pitanje.tvrtka_id
    WHERE korisnik_id = '$activeUserId'";



if(isset($_SESSION['activeUser']))
{
$query1="SELECT tvrtka.tvrtka_id,preostaliOdgovori,zaposlenik.tvrtka_id,korisnik_id
    FROM tvrtka
    INNER JOIN zaposlenik ON zaposlenik.tvrtka_id = tvrtka.tvrtka_id
    WHERE korisnik_id = '$activeUserId'";

$result2=queryDB($connect,$query1);
 while($row=mysqli_fetch_array($result2))
 {
   $odgovori=$row['preostaliOdgovori'];
 }
}
$_SESSION['preostaliOdgovori']=$odgovori;

All of this actually works but only when I manually load mojaTvrtka.php. I would like to make it that when a user logs in this number is automatically printed in header.php but all I get is undefined index. Im not sure what I am doing wrong.

  • 写回答

2条回答 默认 最新

  • dousuochu7291 2019-02-14 19:34
    关注

    In your flow you need to include mojaTvrtka.php so that it happens before header.php gets loaded and then the $_SESSION data is available. You could probably just do include_once('../PATH/TO/mojaTvrtka.php'); at the top.

    If you included that file at the top of header.php it probably would work. But something you should be aware of is if you include this in every header.php, won't every page run this SQL code? You should have it so your code checks to make sure the $_SESSION['preostaliOdgovori'] doesn't already exist before including it.

    评论

报告相同问题?

悬赏问题

  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来