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条)

报告相同问题?