Jump to content

Educational News Script


Skatecrazy1

Recommended Posts

Hi, well I'm creating a news script that will serve two purposes:

-display news on a user's website
-teach beginning-intermediate php coders the basics of using MySQL

I'm trying to make the code as simple as possible, so the latter objective can be achieved.
here's the first thing I created: the installation file.  I wanted to keep everything within one file, yet still keep the code looking simple, however I haven't been able to achieve that.

Keep in mind all the code I show you here is fully functional.

I was wondering if someone here could show me things I could change/replace to make the code more readable by those who have a limited understanding of PHP.

[code]
<?php
//install.php
/*Installs the Database tables, gets the username and
password of the main admin*/
/*also asks for the server, username, and password
of the mysql for the user */
$self = $_SERVER['PHP_SELF'];
//SQL STUFF*/
$id = $_GET['act'];
if(isset($id)){
              if($id == "sqlinfo"){
$host = $_POST['server'];
$user = $_POST['server_user'];
$pass1 = $_POST['pass1'];
$pass2 = $_POST['pass2'];
$db = $_POST['db'];
      if(isset($host) && isset($user)){
        if($pass1 == $pass2){
      $filename = "db.php";
      $file = fopen($filename, "w+");
      $dbfile = "
      <?php
      \$conn = @mysql_connect(\"$host\", \"$user\", \"$pass1\") or die(\"Sorry, could not connect to MySQL.\");
      \$rs = @mysql_select_db(\"$db\") or die(\"Sorry, couldn't select MySQL database.  Make sure you entered the correct database name.\");
      ?>";
      fwrite($file, $dbfile);
      header("location:$self?act=sqlsetup");
        } else {
          echo("<font color=\"red\"><b>Passwords Did not Match</b></font>");
          }
      }
              $msg = "<form method=\"post\" action=\"$self?act=sqlinfo\">";
              $msg .= "<h3>MySQL Server Information</h3>";
              $msg .= "<p>Before you install the script, we need some basic
              MySQL information so that the news system can successfully
              be installed on your MySQL server.</p>";
              $msg .= "<table cellspacing=\"2\" cellpadding=\"0\">";
              $msg .= "<tr>";
              $msg .= "<td>";
              $msg .= "MySQL Server<br />
              <small>If you are not sure, just leave as \"localhost\"</small>";
              $msg .= "</td><td>";
              $msg .= "<input type=\"text\" name=\"server\" value=\"localhost\" />";
              $msg .= "</td></tr>";
              $msg .= "<tr><td>";
              $msg .= "Database Name";
              $msg .= "<br /><small>This is where all your data will go.  Make sure this is correct.</small>";
              $msg .= "</td><td><input type=\"text\" name=\"db\" /></td>";
              $msg .= "</tr>";
              $msg .= "<tr><td>";
              $msg .= "MySQL Username<br />
              <small>This username was most likely given to you by your sysadmin.</small>";
              $msg .= "</td><td>";
              $msg .= "<input type=\"text\" name=\"server_user\" />";
              $msg .= "</td></tr>";
              $msg .= "<tr><td>";
              $msg .= "MySQL Password";
              $msg .= "</td><td>";
              $msg .= "<input type=\"text\" name=\"pass1\" />";
              $msg .= "</td></tr><tr>";
              $msg .= "<td>Repeat Password</td><td>";
              $msg .= "<input type=\"text\" name=\"pass2\" />";
              $msg .= "</td></tr><tr>";
              $msg .= "<td colspan=\"2\"><input type=\"submit\" value=\"Install\" /></td>";
              $msg .= "</tr></table>";
              $msg .= "</form>";
              //echo out that really long form we just created
              echo $msg;
              }
             
      elseif($id == "sqlsetup"){
        require('db.php');

        $sql = array();
       
        $sql[0] = "
        CREATE TABLE `users` (
        id TINYINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
        username TEXT NOT NULL,
        password TEXT NOT NULL,
        access_level TINYINT NOT NULL,
        email TEXT NOT NULL,
        aim TEXT NOT NULL,
        msn TEXT NOT NULL,
        yahoo TEXT NOT NULL,
        website TEXT NOT NULL
        )";
       
        $sql[1] = "
        CREATE TABLE `posts` (
        id TINYINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
        title TEXT NOT NULL,
        body TEXT NOT NULL,
        timestamp TEXT NOT NULL,
        author TEXT NOT NULL,
        comment_amt INT NOT NULL
        )";
       
        $sql[2] = "
        CREATE TABLE `comments` (
        id TINYINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
        name TEXT NOT NULL,
        body TEXT NOT NULL,
        email TEXT NOT NULL,
        postid INT NOT NULL
        )
        ";
       
        $sql[3] = "
        CREATE TABLE `styles` (
        id TINYINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
        css TEXT NOT NULL
        )
        ";
       
        foreach($sql as $node)
        {
          @mysql_query($node, $conn) or die("Installation failed.");
          }
        sleep(3);
        echo("<meta http-equiv=\"refresh\" content=\"0;url=$self?act=usrreg\">");
       
       
      }
     
      elseif($id == "usrreg"){
      require('db.php');
      $username = $_POST['username'];
      $pass1 = $_POST['password'];
      $pass2 = $_POST['passconf'];
      if(isset($username)){
        if($pass1 == $pass2){
          $sql = "INSERT INTO `users` (username, password, access_level) VALUES(
          '$username',
          '$pass1',
          '1'
          )";
          @mysql_query($sql, $conn) or die("Installation failed.");
          echo("<meta http-equiv=\"refresh\" content=\"0;url=$self?act=finished\">");
          } else {
          echo("<font color=\"red\"><b>Passwords did not match.</b></font>");
          }
        }
      $form = "<h3>Final Step</h3>";
      $form .= "<form method=\"post\" action=\"$self?act=usrreg\">";
      $form .= "<p><small>Before EzNews can be put into action, you must first specify your administrative
      username and password, so you may log into the control panel.</small></p>";
      $form .= "<table cellspacing=\"2\" cellpadding=\"0\">";
      $form .= "<tr><td>Desired Username</td><td><input type=\"text\" name=\"username\" /></td></tr>";
      $form .= "<tr><td>Desired Password</td><td><input type=\"password\" name=\"password\" /></td></tr>";
      $form .= "<tr><td>Confirm Password</td><td><input type=\"password\" name=\"passconf\" /></td></tr>";
      $form .= "<tr><td><input type=\"submit\" value=\"Finish\" /></td></tr>";
      $form .= "</table>";
      $form .= "</form>";
      echo $form;
      }
     
      elseif($id == "finished"){
      $msg = "<h3>Installation Complete!</h3>";
      $msg .= "<p>Installation of EzNews has been successful; you may now log into the
      administrative control panel with your selected username and password combination.</p>";
      $msg .= "<p><a href=\"admin/index.php\">Log In Now</a></p>";
      echo $msg;
      }
     
} else {
echo("<meta http-equiv=\"refresh\" content=\"0;url=$self?act=sqlinfo\">");
}
?>
[/code]
Link to comment
Share on other sites

I like what your doing here, I'm doing something similar that I thought would be a good educational tool for learning PHP (for myself and other newbies).

I think that the second script admin/index.php will probably be the place for a beginner - intermediate user to start looking on how to interact with a database.  I think all the file writing and quote escaping stuff will be more of a hindrance than a help.

If you don't mind, I'd like to see the index.php script too. 

Regards,
John Sladek
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.