0

私はこのコードを過去 4 時間にわたって修正しようとしてきましたが、うまく機能しないようです。$_SESSION 変数は、ユーザーがログインする (またはアカウントを作成する) ときに設定され、ログアウトすると破棄されます。しかし、特定のフォームを送信すると、以前は機能していた $_SESSION 変数が突然、未定義の変数エラーをスローします。大量のコンテンツについてお詫び申し上げますが、デューデリジェンスの結果、自分で見つけることができず、誰かに助けを求める必要があるという結論に達しました.

ユーザーのアクションがコードを呼び出す順序の関連コード。

<?php
include 'db_connect.php';
include 'functions.php';
session_start();


// Define $myusername and $mypassword 
$myusername=$_POST['myusername']; 
$mypassword=$_POST['mypassword'];
$_SESSION['sessionInitialize'] = false;
// To protect mysqli injection (more detail about mysqli injection)
//$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$mypassword = md5(md5("SaLt".$mypassword."SaLt"));

$query = "SELECT * FROM secure_login.members WHERE username='" . $myusername . "' and password='" . $mypassword . "'";
$result = $mysqli->query($query) or die ($mysqli->error.__LINE__);

    // If result matched $myusername and $mypassword, table row must be 1 row
    if($result->num_rows == 1){
        initializeSessionVariables();
        // Register $myusername, $mypassword and redirect to file "acct.php"
        $_SESSION['currentUser']=$_POST['myusername']; 
        //$_SESSION['mypassword']=$_POST['mypassword']; 
        header("location:myAcct.php");
        }
    else {
        echo "Wrong Username or Password";
        header("location:index.php");
        }

$mysqli->close();
?>

ユーザーに関連するセッション変数を初期化する関数。

function initializeSessionVariables(){

    $_SESSION['currentUser'] = $_SESSION['currentUser'];
    $_SESSION['currentUserAcctId'] = "";
    $_SESSION['currentUserSummonerId'] = "";
    $_SESSION['currentUserLeagues'] = "";
    $_SESSION['currentUserEmail'] = "";
    $_SESSION['currentUserAvatarURL'] = "";
    $_SESSION['currentUserSummName'] = "";
    $_SESSION['currentUserRealName'] = "";
    $_SESSION['currentUserBday'] = "";
    $_SESSION['currentUserSecondEmail'] = "";
}

アカウントページです。

<?php
    $summonerName=$_SESSION['currentUser'];
    echo "Current User: " . $_SESSION['currentUser'] . "<br>";
    echo "<br>Current User \$summonerName: " . $summonerName; 
    //Create prepared statement.
    $query = "SELECT * FROM `stats`.`summoners` WHERE `summoners`.`name`='" . $summonerName . "'";
    $result = $mysqli->query($query) or die ($mysqli->error.__LINE__);
    echo "<br>test<br><br>";
    Print_r ($result);
        //Run query if query object returned
        if ($result->num_rows == 0){
            echo "<h1>=0</h1>";
            //Free the result so it can be used in the following functions
            $result->free();
            getSummonerData(); //defined in functions.php
            injectSummonerData(); //defined in functions.php
            $query = "SELECT * FROM `stats`.`summoners` WHERE name='" . $summonerName . "'";
            $result = $mysqli->query($query) or die ($mysqli->error.__LINE__);
            //Get associative array for $result
            $row = $result->fetch_assoc();
            $_SESSION['currentUserAcctId'] = $row['acctId'];
            //Print data.
            printf ("<h3>Summoner Name: %s\n <br> Summoner Level: %s\n <br> AcctID: %s\n <br> SummonerID: %s</h3>", $row['name'], $row['summonerLevel'], $row['acctId'], $row['summonerId']);
            //Close result object
            //$result->close();
            echo "<br>";
            //Close DB Connection
            $mysqli->close();
        }
        else if($result->num_rows == 1){
            echo "<h1>=1</h1>";
            $row = $result->fetch_assoc();
            $_SESSION['currentUserAcctId'] = $row['acctId'];
            printf ("<h3>Summoner Name: %s\n <br> Summoner Level: %s\n <br> AcctID: %s\n <br> SummonerID: %s</h3>", $row['name'], $row['summonerLevel'], $row['acctId'], $row['summonerId']);
            echo "<br><h4>This data is already in the database. Did nothing.</h4>";
        }
    echo "<br>" . $_SESSION['currentUserAcctId'];
?>

Profile.php

<p>Profile info</p>
            <?php
                displayProfileInformation($_SESSION['currentUser']);
            ?>

displayProfileInformation 関数

function displayProfileInformation($currentUser){
    include 'dbstat_connect.php';
    $query =  "SELECT * FROM `stats`.`userAccount` where `userAccount`.`profName` = '" . $currentUser ."'";
    if ($result = $mysqli->query($query) or die ($mysqli->error.__LINE__)){
        if ($result->num_rows == 1){
            $row = $result->fetch_assoc();
            echo "User Name: "; if(isset($currentUser)){echo $currentUser . "<br>";}
            echo "Email: "; if(isset($row['email'])){echo $row['email'] . "<br>"; $_SESSION['currentUserEmail'] = $row['email'];}
            echo "Avatar URL: "; if(isset($row['avatarURL'])){echo $row['avatarURL'] . "<br>"; $_SESSION['currentUserAvatarURL'] = $row['avatarURL'];}
            echo "Summoner Name: "; if(isset($row['summName'])){echo $row['summName'] . "<br>"; $_SESSION['currentUserSummName'] = $row['summName'];}
            echo "Real Name: "; if(isset($row['realName'])){echo $row['realName'] . "<br>"; $_SESSION['currentUserRealName'] = $row['realName'];}
            echo "Birthdate: "; if(isset($row['bday'])){echo $row['bday'] . "<br>"; $_SESSION['currentUserBday'] = $row['bday'];}
            echo "Secondary Email: "; if(isset($row['secondEmail'])){echo $row['secondEmail'] . "<br>"; $_SESSION['currentUserSecondEmail'] = $row['secondEmail'];}
            echo "<br>Dafuq yo =1";
        }
        else if ($result->num_rows == 0){
            echo "Dafuq yo =0";
            echo "User Name: " . $currentUser . "<br>";
            echo "Email: <br>";
            echo "Avatar URL: <br>";
            echo "Summoner Name: <br>";
            echo "Real Name: <br>";
            echo "Birthdate: <br>";
            echo "Secondary Email: <br>";
            echo "<h2>Enter what information you like by editing your profile below.</h2><br>";
        }
        else {
            echo "Critical Error: Contact Admin.";
        }


    }
}

editProfile.php

Edit Profile
        <?php editProfileInformationForm($_SESSION['currentUser']); ?>

editProfileInformationForm 関数

function editProfileInformationForm($currentUser){
    echo "<form action='processEditProfile.php' method='post'>";
        echo "Profile Name: " . $_SESSION['currentUser'] . "<br>";
        echo "Account ID: " . $_SESSION['currentUserAcctId'] . "<br>";
        if (isset($_SESSION['currentUserEmail'])){
            echo "Email: <input name='email' type='text' id='email' value='" . $_SESSION['currentUserEmail'] . "'/><br />";
        }
        else {
            echo "Email: <input name='email' type='text' id='email' value=''/><br />";
        }
        if (isset($_SESSION['currentUserEmail'])){
            echo "Secondary Email: <input name='secEmail' type='text' id='secEmail' value='" . $_SESSION['currentUserSecondEmail'] . "' /><br />";
        }
        else {
            echo "Secondary Email: <input name=secEmail type='text' id=secEmail value=''/><br />";
        }
        if (isset($_SESSION['currentUserEmail'])){
            echo "Real Name: <input name='realName' type='text' id='realName' value='" . $_SESSION['currentUserRealName'] . "' /><br />";
        }
        else {
            echo "Real Name: <input name='realName' type='text' id='realName' value=''/><br />";
        }
        if (isset($_SESSION['currentUserAvatar'])){
            echo "Avatar: <input name='avatar' type='text' id='avatar' value='" . $_SESSION['currentUserAvatarURL'] . "'/><br />";
        }
        else {
            echo "Avatar: <input name='avatar' type='text' id='avatar' value=''/><br />";
        }
        if (isset($_SESSION['currentUserSummName'])){
            echo "Summoner Name: <input name='summName' type='text' id='summName' value='" . $_SESSION['currentUserSummName'] . "'/><br />";
        }
        else {
            echo "Summoner Name: <input name='summName' type='text' id='summName' value=''/><br />";
        }
        if (isset($_SESSION['currentUserBday'])){
            echo "Birthday: <input name='bday' type='text' id='bday' value='" . $_SESSION['currentUserBday'] . "'/><br />";
        }
        else {
            echo "Birthday: <input name='bday' type='text' id='bday' value=''/><br />";
        }
        echo "<small>(Bday Format~ YYYY-MM-DD)<small>";
    echo "<input type='submit' name='submit' value='Submit'>";
    echo "</form>";
}   

最後に、これは 2 つの $_SESSION 変数でエラーがスローされる場所です。これには何の理由もないように思われます。

<?php
include 'dbstat_connect.php';
//include 'functions.php';
//echo $_SESSION['currentUser'] . "<br>";
//If stmt valid, prepare to insert profile info into `userAccount`
if ($stmt = $mysqli->prepare("INSERT INTO `stats`.`userAccount` values (". $_SESSION['currentUserAcctId'] . ", " 
    . $_SESSION['currentUser'] . ", ?, ?, ?, ?, ?, ?, NULL, NULL, NULL, NULL, NULL)")){
    //Bind paramaters
    if($stmt->bind_param('ssssss', $_POST['email'], $_POST['avatar'], $_POST['summName'],
        $_POST['realName'], $_POST['bday'], $_POST['secEmail'])){

        //Execute the query. If true, show proof. If false, display error.
        if($stmt->execute()){
            //Show proof of insertion
            echo "<h4>Your data has been inserted.</h4>";
        }
        //Check if stmt returned an error.
        else{
            Print_r ($stmt->get_warnings());
        }
        //Close statement
        $stmt->close();
    }
    //If $stmt statement returns an error, say so
    else if(!$stmt){
        printf ("Error: %s", $mysqli->error);
    }
    //Close DB Connection
    $mysqli->close();
}


echo $_POST['email'];


?>

誰かが私を助けてくれることを本当に願っています。私はかなり長い間ここで立ち往生しています。

4

2 に答える 2