Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I worked on your code and made many changes. I tried to add lots of comments to make it more easy to understand. Hopefully there are no syntax errors, but I couldn't actually test is since I don't have the MySQL databases and such.</p> <p>Here is your main code:</p> <pre><code>&lt;?php //When you are developing and testing, set the error level as high as possible. //This will help you find problems early. A well written program will have no errors and warnings, ever. error_reporting(E_ALL | E_STRICT); //Starting the session should be one of the first things your code does, and should only be done once. session_start(); require 'config.php'; if(isset($_POST['logout'])) { //I don't think there is any reason to check if username is set. If you are logging out, just destroy. session_destroy(); //Also unset the session username since session_destroy() does not affect existing globals. unset($_SESSION['username']); } //I changed this to elseif, because there should not be a condition where you are logging out and checking for a login. elseif(!isset($_SESSION['username'])) { //You should not assume that variables are set, because accessing them if they are not set //will cause a warning. I've added isset(). if(isset($_POST['username']) &amp;&amp; !empty($_POST['username']) &amp;&amp; isset($_POST['password']) &amp;&amp; !empty($_POST['password'])) { //You absolutely MUST escape your strings or you are at risk of SQL injection. //Use mysql_real_escape_string() for this. $username = mysql_real_escape_string($_POST['username']); $password = mysql_real_escape_string($_POST['password']); $result = mysql_query("SELECT * FROM members WHERE username ='$username' AND password = '$password'"); //You should probably check that the value === 1 here. //I'm assuming it should always be 1 or 0. if(0 === mysql_num_rows($result)) { $_SESSION['username'] = $username; } else { echo "Fail :("; } } //If you put an else statement here, you could print an error for if the username was not specified. } //You should not have SQL queries in your template, so I moved this here. //Notice that I'm just setting $posts to the data. It's best to just pass //the data, and format it in the template. $result = mysql_query("SELECT * FROM posts ORDER BY post_id DESC"); if($result) { $posts = array(); while($row = mysql_fetch_array($result)) { $posts[] = $row['post']; } } else { $posts = false; } //Try to separate code logic from templates. //Your program is small, so it's not that important, but I would do it anyway. require 'template.php'; ?&gt; </code></pre> <p>Here is your template code, which should go in a new file called template.php:</p> <pre><code>&lt;div id = "container"&gt; &lt;h1&gt;#HookyGear Bay&lt;/h1&gt; &lt;div id = "login"&gt; &lt;?php if(!isset($_SESSION['username'])) { echo '&lt;div id = "accountBox"&gt; &lt;form name="input" action="index.php" method="post"&gt; Username:&lt;input type="text" name="username" /&gt; Password:&lt;input type="password" name="password" /&gt; &lt;input type="submit" value="Sign In" /&gt; &lt;/form&gt; &lt;/div&gt;'; } else { echo "&lt;div id='accountBox'&gt;You Are logged in as ".$_SESSION['username']." &lt;form name='logout' action='index.php' method='post'&gt; &lt;input type='submit'value='Reset' name='logout'/&gt; &lt;/div&gt; "; } ?&gt; &lt;/div&gt; &lt;div id = "content"&gt; &lt;?php if(false !== $posts) { foreach($posts as $post) { echo '&lt;div class="blogPosts"&gt;'.$post.'&lt;/div&gt;'; } } else { ?&gt; &lt;div class="blogPosts"&gt;&lt;?php echo "no blog posts"; ?&gt;&lt;/div&gt; &lt;?php } ?&gt; &lt;div style="clear:both;"&gt;&lt;/div&gt; &lt;/div&gt; &lt;/div&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload