user3435159
user3435159

Reputation: 33

php newcomer retrieve db information

I have a database set up. I am struggling with something quite simple I guess but I can pull the data I require from my db but not in multiple places if that makes sense?

So I have the content I want in my body but would also like to show it above and below this also. I have tried numerous things but it either kills my work or does nothing at all.

    <nav>
        <div id="wrappertop">
            <ul class="main_menu">
                <li class="main_menu"><a href="#">HOME</a></li>
                <li class="main_menu"><a href="#">SEARCH</a></li>
                <li class="main_menu"><a href="#">MESSAGES</a></li>
                <li class="main_menu"><a href=logout.php>LOGOUT</a></li>
            </ul>
        </div>
    </nav>

    <div class="subnav">
        <div id="wrappertop">
            <ul class="main_menu">
                <li class="main_menu"><a href="#">I WANT A USERNAME HERE</a></li>
            </ul>
        </div>
    </div>
</header>
<div id="wrappermain">
    <div class="leftcolumn"><img src="uploads/profileimage/me.png" /></div>
    <div class="rightcolumn">
        <?php
        $result = mysqli_query($mysqli, "SELECT * FROM members WHERE email='" . $_SESSION['email'] . "'");
        while ($row = mysqli_fetch_array($result)) {

            if ($_SESSION['email'] == $row['email']) {
                // If you find the right user with the same E-Mail address, DO SOME COOL THINGS :)
            }

            echo '<h2>' . $row['username'] . '</h2>';
            echo '<button>Get in touch</button>' . '<br>';
            echo $row['yourage'] . ' years old' . '<br>';
            echo 'I am a ' . $row['orientation'] . ' ' . $row['gender'] . '<br>';
            echo 'Looking for a ' . $row['lookingfor'] . '<br>';
            echo 'Living in ' . $row['location'];
            echo '<br /><br />';
            echo '<hr />';
            echo '<h2>About me:</h2>';
            echo $row['aboutme'] . '<br>';
        }
        mysqli_close($mysqli);
        ?>
    </div>
</div>

So if you take a look in the subnav for example id like to show the username again just like form the body but I am not sure how?

Upvotes: 0

Views: 57

Answers (1)

Funk Forty Niner
Funk Forty Niner

Reputation: 74217

EDIT

I don't know what you are using for a login method, so here's a basic way of doing it and to test it.

Sidenote: You will obviously need to add an associated password method, so I could not implement that in this edit. Consult my footnotes regarding passwords.

(Login method-tested)

HTML FORM:

<form action="signin.php" method="post">
Email: 
<input type="text" name="email">
<input type="submit" name="submit" value="Submit">
</form>

PHP/SQL: (signin.php)

<?php
$DB_HOST = "xxx";
$DB_NAME = "xxx";
$DB_PASS = "xxx";
$DB_USER = "xxx";

$mysqli = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);
if($mysqli->connect_errno > 0) {
  die('Connection failed [' . $mysqli->connect_error . ']');
}

session_start();

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

$result = mysqli_query($mysqli,"SELECT * FROM members WHERE email='".$_SESSION['email']."'");

$email = mysqli_real_escape_string($mysqli,$_POST['email']);
$_SESSION['email'] = $email;
$email = $_SESSION['email'];

    while($row = mysqli_fetch_array($result))
    {

    $_SESSION['email']=$row['email'];
    $email = $row['email'];

    $_SESSION['username']= $row['username'];
    $username = $row['username'];

    $_SESSION['yourage']= $row['yourage'];
    $yourage = $row['yourage'];

    $_SESSION['orientation']= $row['orientation'];
    $orientation = $row['orientation'];

    $_SESSION['gender']= $row['gender'];
    $gender = $row['gender'];

    $_SESSION['lookingfor']= $row['lookingfor'];
    $lookingfor = $row['lookingfor'];

    $_SESSION['location']= $row['location'];
    $location = $row['location'];

    $_SESSION['aboutme']= $row['aboutme'];
    $aboutme = $row['aboutme'];

    }

} // brace for if(isset($_SESSION['email'])

?>

<?php

    echo $email;
    echo "<br>";
    echo $username;
    echo "<br>";
    echo $yourage;

?>

<nav>
        <div id="wrappertop">
            <ul class="main_menu">
                <li class="main_menu"><a href="#">HOME</a></li>
                <li class="main_menu"><a href="#">SEARCH</a></li>
                <li class="main_menu"><a href="#">MESSAGES</a></li>
                <li class="main_menu"><a href=logout.php>LOGOUT</a></li>
            </ul>
        </div>
    </nav>

    <div class="subnav">
        <div id="wrappertop">
            <ul class="main_menu">
                <li class="main_menu"><a href="#">USERNAME HERE <?php echo $username; ?></a></li>
            </ul>
        </div>
    </div>
</header>
<div id="wrappermain">
    <div class="leftcolumn"><img src="uploads/profileimage/me.png" /></div>
    <div class="rightcolumn">

<?php

            if ($_SESSION['email'] == $row['email']) {
                // If you find the right user with the same E-Mail address, DO SOME COOL THINGS :)
            }

            echo '<h2>' . $username . '</h2>';
            echo '<button>Get in touch</button>' . '<br>';
            echo $yourage . ' years old' . '<br>';
            echo 'I am a ' . $orientation . ' ' . $gender . '<br>';
            echo 'Looking for a ' . $lookingfor . '<br>';
            echo 'Living in ' . $location;
            echo '<br /><br />';
            echo '<hr />';
            echo '<h2>About me:</h2>';
            echo $aboutme . '<br>';

        mysqli_close($mysqli);
?>

        </div>

</div>

<hr>

<?php
    echo $email;
    echo "<br>";
    echo $username;
    echo "<br>";
    echo $yourage;
?>

<a href="logout.php">LOGOUT</a>

(Logout)

<?php
  session_start();
  unset($_SESSION);
  session_destroy();
?>

<a href="login.php">LOGIN</a>

Original answer

You need to assign session variables to rows and fetch the data first.

Try this: (the code placement is important)

Sidenote: Make sure the column names match. I don't know what you have for yours.

<?php
session_start();

// replace with your credentials
$DB_HOST = "xxx";
$DB_NAME = "xxx";
$DB_PASS = "xxx";
$DB_USER = "xxx";

$mysqli = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);
if($mysqli->connect_errno > 0) {
  die('Connection failed [' . $mysqli->connect_error . ']');
}

$result = mysqli_query($mysqli,"SELECT * FROM members WHERE email='".$_SESSION['email']."'");
while($row = mysqli_fetch_array($result))
    {
        $email = $_SESSION['email'];
        $_SESSION['email'] = "[email protected]";
        // or use $_POST['email'] if coming from a login

        $_SESSION['username']= $row['username'];
        $username = $row['username'];

        $_SESSION['yourage']= $row['yourage'];
        $yourage = $row['yourage'];

        $_SESSION['orientation']= $row['orientation'];
        $orientation = $row['orientation'];

        $_SESSION['gender']= $row['gender'];
        $gender = $row['gender'];

        $_SESSION['lookingfor']= $row['lookingfor'];
        $lookingfor = $row['lookingfor'];

        $_SESSION['location']= $row['location'];
        $location = $row['location'];

        $_SESSION['aboutme']= $row['aboutme'];
        $aboutme = $row['aboutme'];
    }
?>

<?php
// If everything is set, you can echo things here
echo $email;
echo "<br>";
echo $username;
echo "<br>";
echo $yourage;
?>

<nav>
        <div id="wrappertop">
            <ul class="main_menu">
                <li class="main_menu"><a href="#">HOME</a></li>
                <li class="main_menu"><a href="#">SEARCH</a></li>
                <li class="main_menu"><a href="#">MESSAGES</a></li>
                <li class="main_menu"><a href=logout.php>LOGOUT</a></li>
            </ul>
        </div>
    </nav>

    <div class="subnav">
        <div id="wrappertop">
            <ul class="main_menu">
                <li class="main_menu"><a href="#">USERNAME WILL APPEAR HERE <?php echo $username; ?></a></li>
            </ul>
        </div>
    </div>
</header>
<div id="wrappermain">
    <div class="leftcolumn"><img src="uploads/profileimage/me.png" /></div>
    <div class="rightcolumn">

<?php

            if ($_SESSION['email'] == $row['email']) {
                // If you find the right user with the same E-Mail address, DO SOME COOL THINGS :)
            }

            echo '<h2>' . $username . '</h2>';
            echo '<button>Get in touch</button>' . '<br>';
            echo $yourage . ' years old' . '<br>';
            echo 'I am a ' . $orientation . ' ' . $gender . '<br>';
            echo 'Looking for a ' . $lookingfor . '<br>';
            echo 'Living in ' . $location;
            echo '<br /><br />';
            echo '<hr />';
            echo '<h2>About me:</h2>';
            echo $aboutme . '<br>';

        mysqli_close($mysqli);
?>

        </div>
</div>

<hr>

<?php
// If everything is set, you can echo things here
echo $email;
echo "<br>";
echo $username;
echo "<br>";
echo $yourage;
?>

Footnotes:

(Regarding passwords)

Use one of the following, if you're not already using a password storage method:

Other links:

Upvotes: 2

Related Questions