Click here to Skip to main content
15,889,475 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Howdy,

On my NotePad++, the line at 116 shows the closing bracket of the ELSE black. Really really puzzling!
I know my code is not neat but I will tidy it up based on the feedback I got so far but in the meanwhile my great mind is filled with the question why the line at 116 shows the closing bracket of the ELSE black.
HTML
<html>
<head>
<title>
$user Home Page
</title>
</head>
<body>
<body background=".png">

<?php
session_start();
require "conn.php";

/*Check if user is logged-in or not by checking if session is set or not. 
If user is not logged-in then redirect to login page. Else, show user's account homepage.*/

if(!isset($_SESSION["user"])) 
{
    header("location:login.php");
}
else 
{
    $user = $_SESSION["user"];
    $sql = "SELECT * FROM users WHERE Username = '".$user."'";
    $result = $conn->query($sql);
    while($row = mysqli_fetch_assoc($result)) 
    {
        $db_id = $row["Id"];
        $db_username = $row["Username"];
        $db_forename = $row["Forename"];
        $db_surname = $row["Surname"];
        $db_email = $row["Email"];
        $db_bio = $row["Bio"];
        $db_status = $row["Status"];
    
    
        //Welcome user by name.
        echo "<center>Welcome <h2>$db_forename $db_surname!</center>"?></h2>|

        <?php
        //Display log-out link.
        echo "<p align='right'><a href='logout.php'>$user Log Out</a>";?>|</p><br>
    
        <form method="post" action="">    
        <div class="form-group">
        <center><label>Post Status To Friends:</label>
        <input type="text" placeholder="Post Status To Friends" name="post_status_to_friends" /></center><br>
        <center><button type="submit" class="btn btn-default" name="post_status_to_friends" />Post Status To Friends!</button></center>
        </div>

        <form method="post" action="">
        <div class="form-group">
        <center><label>Post Status To Public:</label>
        <input type="text" placeholder="Post Status To Public" name="post_status_to_public" /></center><br>
        <center><button type="submit" class="btn btn-default" name="post_status_to_public" />Post Status To Public!</button></center>
        </div>
    
        <?php
        //Post User Status To Friends in $user table.
        
        if  (isset($_POST['post_status_to_friends']))
        {
            $status_to_friends = trim($_POST["post_status_to_friends"]);
            $status_to_friends = mysqli_real_escape_string($conn,$status_to_friends);
        
            $conn->query("UPDATE $user SET Status_To_Friends $status_to_friends WHERE Username = '".$user."'");
            if($conn->query($sql)===TRUE)
            {
                echo "posted status for friends to $user table!";
                $conn->close();
                exit();
            }
            else    
            {
                echo "posting status for friends to $user table failed!";
                $conn->close();
                exit();
            }
    
            //Post User Status To Friends in "users" table.
        
            $conn->query("UPDATE users SET Status_To_Friends $status_to_friends WHERE Username = '".$user."'");
            if($conn->query($sql)===TRUE)
            {
                echo "posted status for friends to users table!";
                $conn->close();
                exit();
            }
            else    
            {
                echo "posting status for friends to users table failed!";
                $conn->close();
                exit();
            }
        
            //Display User Status To Friends. Search for User's data with $user.
            
            $query = "SELECT * FROM $user WHERE Username = '".$user."'";
            $result = mysqli_query($conn,$query);
            $numrows = mysqli_num_rows($result);
            if($numrows = 0)
            {        
                echo "No data!";
                $conn->close();
                exit();
            }
            else 
            {
                while($row = mysqli_fetch_assoc($result)) 
                {      
                    $db_status_to_friends = $row["Status_To_Friends"];
                }
                echo "<br>$user Status To Friends:<br>
                $db_status_to_friends";?><br>
                <br>
            }
    
        <?php
        //Post User Status To Public in $user table.
    
        if  (isset($_POST['post_status_to_public']))
        {
            $status_to_public = trim($_POST["post_status_to_public"]);
            $status_to_public = mysqli_real_escape_string($conn,$status_to_public);
        
            $conn->query("UPDATE $user SET Status_To_Public $status_to_public WHERE Username = '".$user."'");
            if($conn->query($sql)===TRUE)
            {
                echo "posted status for public to $user table!";
                $conn->close();
                exit();
            }
            else    
            {
                echo "posting status for public to $user table failed!";
                $conn->close();
                exit();
            }
    
            //Post User Status To Public in users table.
            $conn->query("UPDATE users SET Status_To_Public $status_to_public WHERE Username = '".$user."'");
            if($conn->query($sql)===TRUE)
            {
                echo "posted status for public to users table!";
                $conn->close();
                exit();
            }
            else    
            {
                echo "posting status for public to users table failed!";
                $conn->close();
                exit();
            }
    
            //Display User Status To Public. Search for User's data with $user.
            
            $query = "SELECT * FROM $user WHERE Username = '".$user."'";
            $result = mysqli_query($conn,$query);
            $numrows = mysqli_num_rows($result);
            if($numrows = 0)
            {        
                echo "No data!";
                $conn->close();
                exit();
            }
            else 
            {
                while($row = mysqli_fetch_assoc($result)) 
                {      
                    $db_status_to_public = $row["Status_To_Public"];
                }
            echo "<br>$user Status To Public:<br>
            $db_status_to_public";?><br>
            <br>
            <?php 
            }
        }
        
        //Display User Bio.
        echo "<br>Bio:<br>";
        echo "$db_bio";?><br>
        <br>
        
        <?php 
        //Display User's Latest View.
        echo "<br>Latest View:<br>";
        echo "$db_latest_view";?><br>
        <br>

        <?php 
        //Display User's Latest Viewed Url in iFrame.?>
        <iframe src="<?php $db_latest_view;?>"></iframe>
        <?php     
    }
}
?>
</body>
</html>


What I have tried:

Tried changing code many different ways but no luck!
Posted
Updated 17-Mar-17 22:58pm
v3
Comments
Bryian Tan 18-Mar-17 1:22am    
not sure if it copy paste issue, but look like the code is missing a php closing tag ?> it should be
else 
            {
                while($row = mysqli_fetch_assoc($result)) 
                {      
                    $db_status_to_friends = $row["Status_To_Friends"];
                }
                echo "<br>$user Status To Friends:<br>
                $db_status_to_friends";?><br>
                <br>
            }
?> 
 <?php
        //Post User Status To Public in $user table.
...
...
Member 12956789 18-Mar-17 16:11pm    
Are you sure ?
I already added the closing bracket here:

$db_status_to_friends";?>

Are sure about the double tag ?
HTML
<body>
<body background=".png">

[Update]
Quote:
I am going to later on add the img file there.

Whatever you plan to do, you can't have 2 <body> tags.
 
Share this answer
 
v2
Comments
Member 12956789 18-Mar-17 16:13pm    
I am going to later on add the img file there.
You have to be very meticulous about the pairing of opening and closing tags, brackets, and so on, for example, this block of code should be:
<?php
session_start();
require "conn.php";

/*Check if user is logged-in or not by checking if session is set or not. 
If user is not logged-in then redirect to login page. Else, show user's account homepage.*/

if(!isset($_SESSION["user"])) 
{
    header("location:login.php");
}
else 
{
    $user = $_SESSION["user"];
    $sql = "SELECT * FROM users WHERE Username = '".$user."'";
    $result = $conn->query($sql);
    while($row = mysqli_fetch_assoc($result)) 
    {
        $db_id = $row["Id"];
        $db_username = $row["Username"];
        $db_forename = $row["Forename"];
        $db_surname = $row["Surname"];
        $db_email = $row["Email"];
        $db_bio = $row["Bio"];
        $db_status = $row["Status"];
    
    
        //Welcome user by name.
		echo "<center>Welcome <h2>$db_forename $db_surname!</center></h2>";

         //Display log-out link.
		echo "<p align='right'><a href='logout.php'>$user Log Out</a></p><br>"; 
	
	}
}
?>
You have to figure out the rest if any. Last but not least, you should be using PHP Prepared Statements[^] to prevent the risk of SQL injection[^]
 
Share this answer
 
Comments
Member 12956789 18-Mar-17 16:16pm    
Can you show an example so no one can sql inject ? You can modify my code. Other newbies can learn from your example in the future who check this thread.

Thank You!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900