Thursday, July 4, 2013

Basic Student Enrollment Program

This program can;
1. add a student
2. edit a student
3. delete a student
4. display all students
Coding style is still a mess but I might need some codes here for future use.

[index.php]

<HTML><TITLE>STUDENT RECORD</TITLE>
<BODY>
<DIV ALIGN='center'>
    <H3>Login Page</H3>
    <FORM METHOD='POST' ACTION='validate.php'>
    <TABLE>
        <TR>
            <TD>Id Number:</TD>
            <TD><input type="text" name="IdNumber" size='2'></TD>
        </TR>
        <TR>
            <TD>Password:</TD>
            <TD><input type="password" name="Password"></TD>
        </TR>
    </TABLE>   
    <input type="submit" value="Sign In">
    </FORM>
</DIV>
</BODY>
</HTML>


[config.php]
<?php
    $db_user = "root"// mysql username to access the database with.
    $db_pass = "crawl"// mysql password to access the database with.
    $server = "localhost"// server to connect to.
   
    $database = "crawler_attendance"// the name of the database.
    $table = "students";    // the table that this script will set up and use.

    $link = mysql_connect($server, $db_user, $db_pass);
    mysql_select_db($database,$link);
?>


[validate.php]
<?php
    session_start();
   
    include("config.php"); //including config.php in our file
    $id_num = $_POST["IdNumber"]; //Storing username in $username variable.
    $password = $_POST["Password"]; //Storing password in $password variable.
   
    $query = "select id, firstname, lastname, rank from $table where id = '".$_POST['IdNumber']."'
          and password = '".$_POST['Password']."';";
   
    $execqry = mysql_query($query);
    $getValue = mysql_fetch_array($execqry); //place query result in an array?

    $num_rows = mysql_num_rows($execqry);

    if ($num_rows <= 0) {
        include("index.php");
        echo "Sorry, incorrect credentials entered.";
        echo "Try again";
        exit;
    } else {
        $id_firstname;
        $firstname = $getValue['firstname'];
        $rank = $getValue['rank'];
            $_SESSION['firstname']= $id_firstname;
        if($rank == 'administrator')
        {
            header("Location: addAccount.php");
            header("Location: editAccount.php");
            header("Location: deleteAccount.php");
            header("Location: showAll.php");
            header("Location: accountpage.php");
        }
        else
        {
            header("Location: studentpage.php");
        }
    }

?>

[studentpage.php]

<?php
    session_start();
    $firstname = $_SESSION['firstname'];
?>
<HTML>
<BODY>
    <DIV ALIGN='right'><a href="index.php">logout</a></DIV>
    <DIV ALIGN='center'>
    <?php
        echo "Welcome $firstname.";
    ?>
    <H3>Always WEAR your school id properly.</H3>
    <p>Study hard.<p>
    </DIV>

</BODY>
</HTML>

[accountpage.php]

<?php
    session_start();
    $firstname = $_SESSION['firstname'];
?>
<HTML>
<BODY>
    <DIV ALIGN='center'>
    <?php
        echo "Welcome $firstname.";
    ?>
        
    <TABLE>
    <TR>
        <td><a href="addAccount.php">Add an account</a></td>
        <td><a href="editAccount.php">Edit an account</a></td>
        <td><a href="deleteAccount.php">Delete an account</a></td>
        <td><a href="showAll.php">Show all accounts</a></td>
        <td><DIV ALIGN='right'><a href="index.php">logout</a></DIV><td>
    </TR>
    <H3>STUDENT RECORD</H3>
    </DIV>
    </TABLE>
</BODY>
</HTML>


[addAccount.php]

<?php
    session_start();
    $firstname = $_SESSION['firstname'];
?>
<HTML>
<BODY>
    <DIV ALIGN='center'>
    <?php echo "Welcome $firstname."; ?>
   
    <TABLE>
    <TR>
        <td><B>Add an account</B></td>
        <td><a href="editAccount.php">Edit an account</a></td>
        <td><a href="deleteAccount.php">Delete an account</a></td>
        <td><a href="showAll.php">Show all accounts</a></td>
        <td><DIV ALIGN='right'><a href="index.php">logout</a></DIV><td>
    </TR>
    <H3>STUDENT RECORD</H3>
    </DIV>
    </TABLE>
   
    <FORM METHOD='POST' ACTION='addAccount_backend.php'>
   
    <H1>REGISTRATION FORM</H1>
    <TABLE>
        <TR>
            <TD>Firstname:</TD>
            <TD><input type="text" name="Firstname"></TD>
        </TR>
        <TR>
            <TD>Lastname:</TD>
            <TD><input type="text" name="Lastname"></TD>
        </TR>
       
        <TR>
            <TD>Position:</TD>
            <TD>
                <select name="Position">
                  <option value="user">User</option>
                  <option value="supervisor">Supervisor</option>
                </select>
            </TD>
        </TR>
       
        <TR>
            <TD>Password:</TD>
            <TD><input type="password" name="Password"></TD>
        </TR>
       
        <TR>
            <TD></TD>
            <TD><DIV ALIGN='right'><input type="submit" value="Save"></DIV></TD>
        </TR>
    </TABLE>   
    </FORM>
</BODY>
</HTML>

[addAccount_backend.php]
 <?php
    session_start();
    include("config.php");

    $Firstname = $_POST["Firstname"];
    $Lastname = $_POST["Lastname"];
    $Position = $_POST["Position"];
    $Password = $_POST["Password"];

$query = "INSERT INTO students(password, firstname, lastname, rank)
        VALUES    ('$Password', '$Firstname', '$Lastname', '$Position')";

$execqry = mysql_query($query);
include("accountpage.php");
echo "Account successfully added.";
?>

[editAccount.php]
 
<?php
    session_start();
    $firstname = $_SESSION['firstname'];
?>
<HTML>
<BODY>
    <DIV ALIGN='center'>
    <?php echo "Welcome $firstname."; ?>
   
    <TABLE>
    <TR>
        <td><a href="addAccount.php">Add an account</a></td>
        <td><B>Edit an account</B></td>
        <td><a href="deleteAccount.php">Delete an account</a></td>
        <td><a href="showAll.php">Show all accounts</a></td>
        <td><DIV ALIGN='right'><a href="index.php">logout</a></DIV><td>
    </TR>
    <H3>STUDENT RECORD</H3>
    </DIV>
    </TABLE>
    <FORM METHOD='POST' ACTION='editAccount_backend01.php'>
        <H1>UPDATE A STUDENT INFO</H1>
        <TABLE>
            <TR>
            <TD>Enter ID:</TD>
            <TD><input type="text" name="Id" size='1'><input type="submit" value="Show"></TD>
            </TR>
        </TABLE>
    </FORM>
</BODY>
</HTML>

[editAccount_backend01.php]
<?php
session_start();
include("config.php");

$Id = $_POST["Id"];

$query = "SELECT `id`, `firstname`, `lastname`, `rank`
      FROM `students`
      WHERE `rank` != 'administrator'
      AND `is_active` = 1
      AND `id` = $Id";

$Firstname = '';
$Lastname = '';
$Rank = '';
    
$execqry = mysql_query($query);
include("editAccount.php");
echo "<table border='1'>
       <tr>
       <th>Id</th>
       <th>Firstname</th>
       <th>Lastname</th>
       <th>Rank</th>
      </tr>";
    while($row = mysql_fetch_array($execqry))
    {
        echo "<tr>";
        echo "<td>" . $row['id'] . "</td>";
        echo "<td>" . $row['firstname'] . "</td>";
        echo "<td>" . $row['lastname'] . "</td>";
        echo "<td>" . $row['rank'] . "</td>";
        echo "</tr>";
        $Firstname = $row['firstname'];
        $Lastname = $row['lastname'];
        $Rank = $row['rank'];
    }
echo "</table>";
?>
<HTML>
    <FORM METHOD='POST' NAME ="form2" ACTION='editAccount_backend02.php'>
    <TABLE>
        <INPUT type="hidden" name="Id" value="<?php echo "$Id"?>">
        <TR>
            <TD>Firstname:</TD>
            <TD><INPUT TYPE = "TEXT" NAME="Firstname" VALUE ="<?php echo "$Firstname"?>"></TD>
        </TR>
      
        <TR>
            <TD>Lastname:</TD>
            <TD><INPUT TYPE= "TEXT" NAME="Lastname" VALUE="<?php echo "$Lastname"?>"></TD>
        </TR>
      
        <TR>
            <TD>Position:</TD>
            <TD><SELECT name="Position">
                  <option value="user">User</option>
                  <option value="supervisor">Supervisor</option>
            </SELECT></TD>
        </TR>
        <TR>
            <TD><DIV ALIGN='right'><input type="submit" value="Update"></DIV></TD>
        </TR>
    </TABLE>  
    </FORM>
</HTML>

[editAccount_backend02.php] 
<?php
    session_start();
    include("config.php");

    $Firstname = $_POST["Firstname"];
    $Lastname = $_POST["Lastname"];
    $Position = $_POST["Position"];
    $Password = $_POST["Password"];
    $Id = $_POST["Id"];

$query = "UPDATE `students`
      SET `firstname`= '$Firstname',
          `lastname` = '$Lastname',
          `rank` = '$Position'
      WHERE `id` = '$Id'";

$execqry = mysql_query($query);
include("accountpage.php");
echo "Account successfully updated.";
?>

[deleteAccount.php]

 <?php
    session_start();
    $firstname = $_SESSION['firstname'];
?>
<HTML>
<BODY>
    <DIV ALIGN='center'>
    <?php echo "Welcome $firstname."; ?>
  
    <TABLE>
    <TR>
        <td><a href="addAccount.php">Add an account</a></td>
        <td><a href="editAccount.php">Edit an account</a></td>
        <td><B>Delete an account</B></td>
        <td><a href="showAll.php">Show all accounts</a></td>
        <td><DIV ALIGN='right'><a href="index.php">logout</a></DIV><td>
    </TR>
    <H3>STUDENT RECORD</H3>
    </DIV>
    </TABLE>
    <FORM METHOD='POST' ACTION='deleteAccount_backend.php'>
        <H1>REMOVE A STUDENT INFO</H1>
        <TABLE>
            <TR>
            <TD>Enter ID:</TD>
            <TD><input type="text" name="Id" size='1'><input type="submit" value="Delete"></TD>
            </TR>
        </TABLE>
    </FORM>
</BODY>
</HTML>

[deleteAccount_backend.php]
<?php
    session_start();
    include("config.php");

    $Id = $_POST["Id"];

$query = "UPDATE `students`
      SET `is_active` = '0'
      WHERE    `id` = '$Id'";

$execqry = mysql_query($query);
include("accountpage.php");
echo "Account successfully deleted.";
?>


[showAll.php]

<?php
    session_start();
    $firstname = $_SESSION['firstname'];
?>
<HTML>
<BODY>
    <DIV ALIGN='center'>
    <?php echo "Welcome $firstname."; ?>
   
    <TABLE>
    <TR>
        <td><a href="addAccount.php">Add an account</a></td>
        <td><a href="editAccount.php">Edit an account</a></td>
        <td><a href="deleteAccount.php">Delete an account</a></td>
        <td><B>Show all accounts</B></td>
        <td><DIV ALIGN='right'><a href="index.php">logout</a></DIV><td>
    </TR>
    <H3>STUDENT RECORD</H3>
    </DIV>
    </TABLE>
    <H1>LIST OF ALL REGISTERED STUDENTS</H1>

</BODY>
</HTML>
<?php
    include("showAll_backend.php");
?>


[showAll_backend.php]
<?php
session_start();
include("config.php");

$query = "SELECT `id` , `firstname` , `lastname` , `rank`
      FROM `students`
      WHERE `rank` != 'administrator'
      AND `is_active` = 1
      ORDER BY `id` ASC";

$execqry = mysql_query($query);


echo "<table border='1'>
       <tr>
       <th>Id</th>
       <th>Firstname</th>
       <th>Lastname</th>
       <th>Rank</th>
      </tr>";
    while($row = mysql_fetch_array($execqry))
    {
        echo "<tr>";
        echo "<td>" . $row['id'] . "</td>";
        echo "<td>" . $row['firstname'] . "</td>";
        echo "<td>" . $row['lastname'] . "</td>";
        echo "<td>" . $row['rank'] . "</td>";
        echo "</tr>";
    }
echo "</table>";
?>

1 comment: