douzen1880 2013-10-08 07:10
浏览 16
已采纳

会话用户名仅在使用按钮后有效

I have a code and want to edit it. Now I try to change stuff, but I dont get why it worked before and now it doesn't anymore. First I had this:

    <?php
session_start();
$mysqli = new mysqli("localhost", "root", "usbw", "accounts");
$mysqli2 = new mysqli("localhost", "root", "usbw", "forum");
$alles_goed=false;

if (isset($_POST['title'])) {
    $alles_goed=true;
    $description=$_POST['description'];
    $title=$_POST['title'];
    $username=$_SESSION['username'];
    $usernamecheck = $mysqli->query("SELECT * from account_information where username ='". $username ."'");
//etc

This was my edit:

    <?php
session_start();
$mysqli = new mysqli("localhost", "root", "usbw", "accounts");
$mysqli2 = new mysqli("localhost", "root", "usbw", "forum");
$alles_goed=false;
$username=$_SESSION['username'];
$usernamecheck = $mysqli->query("SELECT * from account_information where username ='". $username ."'");


if (isset($_POST['title'])) {
    $alles_goed=true;
    $description=$_POST['description'];
    $title=$_POST['title'];
    //etc

This should work fine right? Still for some reason I get this error:

Undefined index: username

at:

$username=$_SESSION['username'];

What am I doing wrong and why isn't the second one possible?

EDIT:

I want to not show my form when the SESSION doesn't exists. So I got that but I want the errors gone.

  • 写回答

4条回答 默认 最新

  • doubeizhong5178 2013-10-08 07:21
    关注

    Use this

    if(isset($_SESSION['username'])){
        $username = $_SESSION['username'];
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)