Reply to comment

Multi-User Web Phonebook

File hierarchy in Netbeans IDE
PHP Tutorial Hello World Application

Entity-Relationship Diagram
ERD Diagram Multi-User PhoneBook

 

phonebook.sql
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
-- Database: `phonebookDB`

CREATE TABLE IF NOT EXISTS `account` (
  `user_name` varchar(50) NOT NULL,
  `password` varchar(50) NOT NULL,
  PRIMARY KEY (`user_name`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;


CREATE TABLE IF NOT EXISTS `profile` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `user_name` varchar(50) NOT NULL,
  `name` varchar(100) NOT NULL,
  `address` varchar(100) NOT NULL,
  `phone_number` varchar(50) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `user_name` (`user_name`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=9 ;

ALTER TABLE `profile`
  ADD CONSTRAINT `profile_ibfk_1` FOREIGN KEY (`user_name`) REFERENCES `account` (`user_name`) ON DELETE CASCADE ON UPDATE CASCADE;
AddProfile.php
<?php
   session_start();

   include_once('../Bean/Profile.php');
   include_once('../DB/ProfileConn.php');

    if( isset($_SESSION['userName'])){
        $userName = $_SESSION['userName'];
        $name = $_POST['name'];
        $address = $_POST['address'];
        $phoneNumber = $_POST['phoneNumber'];
        //id set to zero since it is not used
        $profile = new Profile(0, $userName, $name, $address, $phoneNumber);
        $conn = new ProfileConn();
        $conn->Add($profile);
        header( 'Location: DisplayProfile.php' );
        
    } else{
        header( 'Location: ../index.html' ) ;   
    } 
?>
CreateAccount.php
<?php

    include_once('../Bean/Account.php');
    include_once('../DB/AccountConn.php');

    $userName = $_POST['userName'];
    $password = $_POST['password'];

    $account = new Account($userName, $password);
    $conn = new AccountConn();
    $conn->Create($account);

    header( 'Location: ../index.html' ) ;
?>
DeleteProfile.php
<?php
   session_start();

   include_once('../Bean/Profile.php');
   include_once('../DB/ProfileConn.php');
    if( isset($_SESSION['userName'])){
        $userName = $_SESSION['userName'];
        $id = $_REQUEST['id'];
        
        $conn = new ProfileConn();
        $conn->Delete($userName, $id);
        header( 'Location: DisplayProfile.php' );
        
    } else{
        header( 'Location: ../index.html' ) ;   
    } 
?>
DisplayProfile.php
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title></title>

    <link rel="stylesheet" type="text/css" href="../CssFolder/DisplayPhoneBook.css" />
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  </head>
  <body>
      
    <?php
        session_start();

        include_once('../Bean/Profile.php');
        include_once('../DB/ProfileConn.php');

        if( isset($_SESSION['userName'])){
            $conn = new ProfileConn();
            $arr = $conn->Display($_SESSION['userName']);

echo <<<here
    <table border="1">
    <tbody>
        <tr><th>NAME</th><th>ADDRESS</th><th>PHONE NUMBER</th><th>       </th></tr>
here;
          
        foreach ($arr as $val){
            $id = $val->getId();
            $name = $val->getName();
            $address = $val->getAddress();
            $phoneNumber = $val->getPhoneNumber();
            
            echo "<tr>";
            echo "<td>$name</td>";
            echo "<td>$address</td>";
            echo "<td>$phoneNumber</td>";
            echo "<td>[<a href='ViewProfile.php?id=$id&name=$name&address=$address&phoneNumber=$phoneNumber'>u</a>]
                [<a href='DeleteProfile.php?id=$id'>x</a>]</td>";
            echo "</tr>";
        }
        
echo <<<here
     </tbody>
     </table>
here;
        } else{
            header( 'Location: ../index.html' ) ;
        }
    ?>
      
      <a href="../addProfile.html">ADD</a>     
      <a href="DisplayProfile.php">DISPLAY</a>    
      <a href="Logout.php">LOG-OUT</a>     
      
    </body>
</html>
Login.php
<?php
    include_once('../Bean/Account.php');
    include_once('../DB/AccountConn.php');

    $userName = $_POST['userName'];
    $password = $_POST['password'];

    $account = new Account($userName, $password);
    $conn = new AccountConn();
    $count = $conn->CheckAccount($account);

    if ($count == 1){
        session_start();
        $_SESSION['userName'] = $userName;
        echo $_SESSION['userName'];
        header( 'Location: DisplayProfile.php' ) ;
    }
    else{
        header( 'Location: ../index.html' ) ;
    }
?>
Logout.php
<?php
    session_start();
    session_destroy();

    header( 'Location: ../index.html' ) ;
?>
UpdateProfile.php
<?php
    session_start();

    include_once('../Bean/Profile.php');
    include_once('../DB/ProfileConn.php');

    if( isset($_SESSION['userName'])){
        $id = $_POST['id'];
        $userName = $_SESSION['userName'];
        $name = $_POST['name'];
        $address = $_POST['address'];
        $phoneNumber = $_POST['phoneNumber'];
        $profile = new Profile($id, $userName, $name, $address, $phoneNumber);
        $conn = new ProfileConn();
        $conn->Update($profile);
        header( 'Location: DisplayProfile.php' );

    } else{
        header( 'Location: ../index.html' ) ;   
    }
?>
ViewProfile.php
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
    </head>
    <body>
        <?php
            $id = $_GET['id'];
            $name = $_GET['name'];
            $address = $_GET['address'];
            $phoneNumber = $_GET['phoneNumber'];
        ?>
        
        <form action="UpdateProfile.php" method="post">
            <input type="hidden" name="id" value="<?echo $id?>"/>
            <label>Name:</label><input type="text" name="name" value="<?echo $name?>" /><br/>
            <label>Address:</label><input type="text" name="address" value="<?echo $address?>" /><br/>
            <label>Phone Number</label><input type="text" name="phoneNumber" value="<?echo $phoneNumber?>" /><br/>
            <input type="submit" value="update"/>
        </form>
    </body>
</html>
Account.php
<?php

class Account {

    private $userName;
    private $password;
    
    function __construct($userName, $password) {
        $this->userName = $userName;
        $this->password = $password;
    }

    public function getUserName() {
        return $this->userName;
    }

    public function setUserName($userName) {
        $this->userName = $userName;
    }

    public function getPassword() {
        return $this->password;
    }

    public function setPassword($password) {
        $this->password = $password;
    }

}

?>
Profile.php
<?php

class Profile {
    private $id;
    private $userName;
    private $name;
    private $address;
    private $phoneNumber;
    
    function __construct($id, $userName, $name, $address, $phoneNumber) {
        $this->id = $id;
        $this->userName = $userName;
        $this->name = $name;
        $this->address = $address;
        $this->phoneNumber = $phoneNumber;
    }
    
    public function getId() {
        return $this->id;
    }

    public function setId($id) {
        $this->id = $id;
    }

    public function getUserName() {
        return $this->userName;
    }

    public function setUserName($userName) {
        $this->userName = $userName;
    }

    public function getName() {
        return $this->name;
    }

    public function setName($name) {
        $this->name = $name;
    }

    public function getAddress() {
        return $this->address;
    }

    public function setAddress($address) {
        $this->address = $address;
    }

    public function getPhoneNumber() {
        return $this->phoneNumber;
    }

    public function setPhoneNumber($phoneNumber) {
        $this->phoneNumber = $phoneNumber;
    }
    
}

?>
AccountConn.php
<?php

include_once('Connection.php');
include_once('../Bean/Account.php');

class AccountConn extends Connection{
    
    public function Create(Account $account){
        $userName = $account->getUserName();
        $password = $account->getPassword();
   
        $query = "INSERT INTO account VALUES('$userName', '$password')";
        $stmt = mysqli_prepare(parent::getConn(), $query);
        $stmt -> execute();
        
        parent::close();
    }
    
    
    public function CheckAccount(Account $account){
        $userName = $account->getUserName();
        $password = $account->getPassword();
   
        $query = "SELECT COUNT(*) FROM account WHERE user_name = '$userName' and password = '$password' ";
        $stmt = mysqli_prepare(parent::getConn(), $query);
        $stmt -> execute();
        $stmt->bind_result($count);
        while ($stmt->fetch()) {
           $count = $count;
        }
        parent::close();
        return $count;
    }

}

?>
Connection.php
<?php

class Connection {
    private $conn;

    public function getConn() {
       return $this->getConnection();
    }

    public function getConnection(){
        $this->conn = mysqli_connect('localhost', 'phonebook', 'phonebook', 'phonebookDB');
        if (!$this->conn) {
            die('Could not connect to MySQL: ' . mysqli_connect_error());
        }

        return $this->conn;
    }


    public function close(){
        mysqli_close($this->conn);
    }
}
?>
ProfileConn.php
<?php

include_once('Connection.php');
include_once('../Bean/Profile.php');

class ProfileConn extends Connection{
    
    public function Add(Profile $profile){
        $userName = $profile->getUserName();
        $name = $profile->getName();
        $address = $profile->getAddress();
        $phoneNumber = $profile->getPhoneNumber();
       
        $query = "INSERT INTO profile(user_name, name, address, phone_number)
            VALUES('$userName', '$name', '$address', '$phoneNumber')";
        
        $stmt = mysqli_prepare(parent::getConn(), $query);
        $stmt -> execute();
        
        parent::close();
    }

    
    public function Display($userName){
        $query = "SELECT * FROM profile WHERE user_name = '$userName'";
        $stmt = mysqli_prepare(parent::getConn(), $query);
        $stmt->execute();

        $stmt->bind_result($id, $userName, $name, $address, $phoneNumber);
        $profileArray = array();
        
        while ($stmt->fetch()) {
            $profile = new Profile($id, $userName, $name, $address, $phoneNumber);
            $profileArray[] = $profile;
        }

        $stmt->close();
        return $profileArray;
    }
    
    public function Update(Profile $profile){
        $id = $profile->getId();
        $userName = $profile->getUserName();
        $name = $profile->getName();
        $address = $profile->getAddress();
        $phoneNumber = $profile->getPhoneNumber();
        
        $query = "UPDATE profile set name = '$name', address = '$address', 
            phone_number = '$phoneNumber'  WHERE id = '$id' AND user_name = '$userName' ";
        $stmt = mysqli_prepare(parent::getConn(), $query);
        $stmt -> execute();
    }
    
    public function Delete($userName, $id){
        $query = "DELETE FROM profile WHERE user_name = '$userName' and id='$id' ";
        $stmt = mysqli_prepare(parent::getConn(), $query);
        $stmt -> execute();
    }
    
    
}
?>
addProfile.html
<!DOCTYPE html>
<html>
    <head>
        <title></title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    </head>
    <body>
        <form action="Action/AddProfile.php" method="post">
            <label>Name:</label><input type="text" name="name" /><br/>
            <label>Address:</label><input type="text" name="address" /><br/>
            <label>Phone Number</label><input type="text" name="phoneNumber" /><br/>
            <input type="submit" value="add"/>
        </form>
        
    </body>
</html>
createAccount.html
<!DOCTYPE html>
<html>
    <head>
        <title></title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    </head>
    <body>
        <form action="Action/CreateAccount.php" method="post">
            <label>User Name:</label><input type="text" name="userName"/><br/>
            <label>Password:</label><input type="password" name="password"/><br/>
            <input type="submit" value="Create Account"/>
        </form>
    </body>
</html>
index.html
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
    </head>
    <body>
        <h1>Welcome to Multi-User Web PhoneBook</h1>
        <form action="Action/Login.php" method="post">
            <label>User Name:</label><input type="text" name="userName"/><br/>
            <label>Password:</label><input type="password" name="password"/><br/>
            <input type="submit" value="Login"/>
        </form>
        
        Create Account <a href="createAccount.html">here</a>.
    </body>
</html>

Reply

The content of this field is kept private and will not be shown publicly.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.