-5

I am new to PHP. I have some doubts regarding PHP constructor. I have used 2 classes. One class contains constructor, Another class has insertion function. So I want to use the variable declared under constructor in order to write the insert query using mysqli. But I don't know how to access it. Can anyone pls help me with this one.

OOPDB.php
                <?php
            class Connection
            {
                //public $conn;
            public function __construct()
            {   
                $conn=mysqli_connect("localhost","root","Hatheem06","Emp");
                if(!$conn)
                    {
                        echo "DB not connected";
                    }
                else
                    {
                        echo "DB connected Successfully"."<br>";
                    }
            }
?>
FormDB.php
                <?php
            include ("OOPDB.php");
            $obj=new Connection();

            class User
            {
                public function insertion($name,$Uname,$Pswrd,$Age,$Email)

                {   
                    /*$sql=$conn->query("INSERT INTO Employee(Name,Username,Password,Age,Email)VALUES('$name','$Uname','$Pswrd','$Age','$Email')");
                    return $sql;*/
                    $ret=mysqli_query($conn,"insert into Employee(Name,Username,Password,Age,Email) values('$name','$Uname','$Pswrd','$Age','Email')");
                    return $ret;

                }
            }
            $Object=new User();


            if (isset($_POST['submit']))
                {       
                    $name=$_POST['Name'];
                    $Uname=$_POST['UName'];
                    $Pswrd=$_POST['pswd'];
                    $Age=$_POST['Age'];
                    $Email=$_POST['Email'];
                    $result=$Object->insertion($name,$Uname,$Pswrd,$Age,$Email);
                    if($result)
                    {
                    echo "Registration Successful";
                    }
                    else
                    {
                        echo "Not registered";
                    }

                }


            ?>
            <html>
                <head><h1 align="center">Employee Details</h1>

                    <title> Employee </title>
                        <link rel="stylesheet" type="text/css" href="Style.css">
                </head>
                <body>
                <div class="dtabb">
                    <form name="name" method="POST">
                        <table class="Etab">
                            <tr><td>Enter Your Name</td>
                                <td><input type="text" name="Name" ></td>
                            </tr>
                            <tr>
                                <td>Enter User Name</td>
                                <td><input type="text" name="UName" ></td>
                            </tr>
                            <tr>
                                <td>Enter password</td>
                                <td><input type="password" name="pswd"></td>
                            </tr>
                            <tr>
                                <td>Enter Your Age</td>
                                <td><input type="text" name="Age" ></td>
                            </tr>
                            <tr>
                                <td>Enter Mail ID of the Employee</td>
                                <td><input type="text" name="Email" ></td>
                            </tr>
                            <tr>
                                <td colspan="2"><center><input type="submit" name="submit" value="submit"/></center>
                                </td>
                            </tr>
                        </table>
                    </form>
                    </div>
                    </body>
                    </html>

1 Answers1

0

I would suggest a static method in the Connection class that returns the connection handle. And thats all that need to be in that class

The in classes that need to connect call the getCOnn() method and store the connection returned as a property of themselves.

You also shoudl start using parameterised and bound queries, to protect yourself from SQL Injection.

OOPDB.php

<?php
class Connection
{
    private $conn = NULL;

    public static function getConn {  
        if (self::conn !== NULL) {
            return self::conn;
        }

        $conn=mysqli_connect("localhost","root","Hatheem06","Emp");

        if (!$conn) {
            echo "Error: Unable to connect to MySQL." . PHP_EOL;
            echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
            echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
            exit;
        } else {
            self::conn = $conn;
            return $conn;
        }
    }
}
?>

FormDB.php

<?php
include ("OOPDB.php");

class User
{
    private $conn;

    public function __construct() {
        $this->conn = Connection::getConn();
    }

    public function insertion($name,$Uname,$Pswrd,$Age,$Email) {   
        $sql = "insert into Employee
                        (Name,Username,Password,Age,Email) 
                values(?,?,?,?,?)");

        $stmt = $this->conn->prepare($sql);
        if ( ! $stmt ) {
            echo $stmt->error;
            return false;
        }
        $stmt->bind_param('sssis', $name,
                                    $Uname,
                                    $Pswrd,
                                    $Age,
                                    $Email
                            );
        $result = $stmt->execute();
        if ( ! $result ) {
            echo $stmt->error;
            return false;
        }
        return true;
    }
}        


$Object=new User();


if (isset($_POST['submit'])) {       
    $name=$_POST['Name'];
    $Uname=$_POST['UName'];
    $Pswrd=$_POST['pswd'];
    $Age=$_POST['Age'];
    $Email=$_POST['Email'];

    $result=$Object->insertion($name,$Uname,$Pswrd,$Age,$Email);
    if($result) {
        echo "Registration Successful";
    } else {
        echo "Not registered";
    }
}
?>
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149