Search code examples
phpmysqldatabaseuser-profilepage-title

how to get page title using $_GET


I just made part of a userprofile script and I don't know how to display username in title. Please help here is the code:

    <?php
    require_once('sp/conn.php');
    $page_title = $get;
     require_once('sp/head.php');
     require_once('sp/userbar.php');
    $get = $_GET['name'];

        $query = "SELECT id,username from user_info where username = '$get'";

  $data = mysqli_query($dbc, $query);

  if (mysqli_num_rows($data) == 1) {

    // The user row was found so display the user data
    $row = mysqli_fetch_array($data);


     $img_id = $row['id'];
?>

    <body>
        <div id="main">
            <div id="user_lvl_avatar_bar">
                <img src="img/ava/<?php echo $row['id'];?>" class="ava" /><font id="username"><?php echo $row['username']; ?></font>
            </div><br />
            <span id="left_user_notif_bar">

            </span>
            <span id="right_user_notif_bar">

            </span>
            <?php
                } else {
                    $error_msg = "<div id=\"main\"><div id=\"red_field\">ERROR : Username doesn't exists.</div></div>";
                    echo $error_msg;
                }
            ?>
        </div>
    </body>
</html>

Sorry for the ugly code I didn't understood how to fix this here in Stackoverflow


Solution

  • You have the the lines in incorrect order, assigning $page_title before $get is set.

    <?php
    require_once('sp/conn.php');
    require_once('sp/head.php');
    require_once('sp/userbar.php');
    
    $get = $_GET['name'];
    $page_title = $get;
    

    Note that your code is prone to SQL injection, see: How can I prevent SQL injection in PHP?