I was designing a simple chat application which just inserts and retrieves data. I am using PHP and MySQL for it. I saw the tutorial on YouTube and trying to make the same. I am posting the code below, the problem is that new user is not getting inserted into the database.
Below is the database code :
-- phpMyAdmin SQL Dump
-- version 4.1.12
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Apr 20, 2014 at 01:24 AM
-- Server version: 5.6.16
-- PHP Version: 5.3.28
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
--
-- Database: `chat`
--
-- --------------------------------------------------------
--
-- Table structure for table `chats`
--
CREATE TABLE IF NOT EXISTS `chats` (
  `ChatId` int(11) NOT NULL,
  `ChatUserId` int(11) NOT NULL,
  `ChatText` text NOT NULL,
  PRIMARY KEY (`ChatId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- --------------------------------------------------------
--
-- Table structure for table `users`
--
CREATE TABLE IF NOT EXISTS `users` (
  `UserId` int(11) NOT NULL AUTO_INCREMENT,
  `UserName` varchar(50) NOT NULL,
  `UserMail` varchar(50) NOT NULL,
  `UserPassword` text NOT NULL,
  PRIMARY KEY (`UserId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
Now the index.php file which is used for both sign up and login :
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <script src="../JqueryLibrary/jquery-1.8.3.js" type="text/javascript"></script>
  <script src="../JqueryLibrary/jquery-1.8.3.min.js" type="text/javascript"></script>
  <title>Welcome to Chat App</title>
</head>
<body>
  <h2>LOGIN FORM</h2>
  <div id="LoginDiv">
    <form action="pages/UserLogin.php" method="post">
      <table>
        <tr>
          <td>Email:</td>
          <td><input name="UserMailLogin" type="email"></td>
        </tr>
        <tr>
          <td>Password</td>
          <td><input name="UserPasswordLogin" type="password"></td>
        </tr>
        <tr>
          <td></td>
          <td><input type="submit" value="LOG IN"></td>
        </tr>
        <?php
          if(isset($_GET['error'])){
        ?>
        <tr>
          <td></td>
          <td><span style="color:red">ERROR LOGIN</span></td>
        </tr>
        <?php
          }
        ?>
      </table>
    </form>
  </div><br>
  <br>
  <br>
  <h2>SIGN UP FORM</h2>
  <div id="SignUpDiv">
    <form action="pages/InsertUser.php" method="post">
      <table>
        <tr>
          <td>Your Name</td>
          <td><input name="UserName" type="text"></td>
        </tr>
        <tr>
          <td>Your Email:</td>
          <td><input name="UserMail" type="email"></td>
        </tr>
        <tr>
          <td>Password:</td>
          <td><input name="UserPassword" type="password"></td>
        </tr>
        <tr>
          <td></td>
          <td><input type="submit" value="Sign Up"></td>
        </tr>
        <?php
            if(isset($_GET['success'])){
        ?>
        <tr>
          <td></td>
          <td><span style="color:green">User Inserted</span></td>
        </tr>
        <?php
            }
        ?>
      </table>
    </form>
  </div>
</body>
</html>
The class.php which contains two classes user and chat.
<?php
    class user{
        private $UserId, $UserName, $UserMail, $UserPassword;
        public
        function getUserId(){
            return $this->UserId;
        }
        public
        function setUserId($UserId){
            $this->UserId = $UserId;
        }
        public
        function getUserName(){
            return $this->UserName;
        }
        public
        function setUserName($UserName){
            $this->Username = $UserName;
        }
        public
        function getUserMail(){
            return $this->UserMail;
        }
        public
        function setUserMail($UserMail){
            $this->UserMail = $UserMail;
        }
        public
        function getUserPassword(){
            return $this->UserPassword;
        }
        public
        function setUserPassword($UserPassword){
            $this->UserPassword = $UserPassword;
        }
        public
        function InsertUser(){
            include "conn.php";
            $req = $bdd->prepare("INSERT INTO users(Username,UserMail,UserPassword) VALUES (:UserName,:UserMail,:UserPassword");
            $req->execute(array('UserName' => $this->getUserName() ,'UserMail' => $this->getUserMail() ,'UserPassword' => $this->getUserPassword()));
        }
        public
        function UserLogin(){
            include "conn.php";
            $req = $bdd->prepare("SELECT * FROM users WHERE UserMail=:UserMail AND UserPassword=:UserPassword");
            $req->execute(array('UserMail' => $this->getUserMail() ,'UserPassword' => $this->getUserPassword()));
            if ($req->rowcount() == 0) {
                header("Location: ../index.php?error=1");
            } else {
                while ($data = $req->fetch()) {
                    $this->setUserId($data['UserId']);
                    $this->setUserName($data['UserName']);
                    $this->setUserPassword($data['UserPassword']);
                    $this->setUserMail($data['UserMail']);
                    header("Location: Home.php");
                    return true;
                }
            }
        }
    }
    class chat{
        private $ChatId, $ChatUserId, $ChatText;
        public
        function getchatId(){
            return $this->ChatId;
        }
        public
        function setChatId($ChatId){
            $this->ChatId = $ChatId;
        }
        public
        function getChatUserId(){
            return $this->ChatUserId;
        }
        public
        function setChatUserId($ChatUserId){
            $this->ChatUserId = $ChatUserId;
        }
        public
        function getChatText(){
            return $this->ChatText;
        }
        public
        function setChatText($ChatText){
            $this->ChatText = $ChatText;
        }
        public
        function InsertChatMessage(){
            include "conn.php";
            $req = $bdd->prepare("INSERT INTO chats(ChatUserId,ChatText)VALUES(:ChatUserId,:ChatText)");
            $req->execute(array('ChatUserId' => $this->getChatUserId() ,'ChatText' => $this->getChatText() ,));
        }
        public
        function DisplayMessage(){
            include "conn.php";
            $ChatReq = $bdd->prepare("SELECT * from chats ORDER BY ChatId DESC");
            $ChatReq->execute();
            while ($DataChat = $ChatReq->fetch()) {
                $UserReq = $bdd->prepare("SELECT * FROM users WHERE UserId=:UserId");
                $UserReq->execute(array('UserId' => $DataChat['ChatUserId']));
                $DataUser = $UserReq->fetch();
                ?>
     <span class="UserNameS"><?php
 echo $DataUser['UserName']; ?></span>says :<br/>
     <span class="ChatMessagesS"><?php
 echo $DataChat['ChatText']; ?></span><br/>
     <?php
            }
        }
    }
And there is this script for database connection using PDO :
<?php
     try{
         $bdd=new PDO("mysql:host=localhost;dbname=chat","root","demo123");  
     }
   catch(Exception $e)
     {
         die("ERROR:".$e->getMesssge());
     }
And the insertUser.php script is as follows :
<?php
  include "classes.php";
  $user=new user();
  if(isset($_POST['UserName']) && isset($_POST['UserMail']) && isset($_POST['UserPassword'])){
        $user->setUserName($_POST['UserName']);
        $user->setUserMail($_POST['UserMail']);
        $user->setUserPassword(sha1($_POST['UserPassword']));
        $user->InsertUser();
        header ("Location: ../index.php?success=1");
  }
Now when I am registering as a new user then it notifies that the user is inserted but when I check the database users in phpMyAdmin then the table is still blank. The database user and password is correct. And there is no exceptions thrown and errors encountered. I am not getting why it is not working.
 
    