PHP OOP概念:

时间:2017-01-26 15:17:25

标签: php mysql oop mysqli

我是PHP的新手。我对PHP构造函数有些怀疑。我用过2节课。一个类包含构造函数,另一个类包含插入函数。所以我想使用构造函数下声明的变量来使用mysqli编写插入查询。但我不知道如何访问它。任何人都可以帮我解决这个问题。

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 个答案:

答案 0 :(得分:0)

我建议Connection类中的一个静态方法返回连接句柄。那就是那个需要在那个班级中的所有东西

需要连接的in类调用getCOnn()方法并存储作为自身属性返回的连接。

您还应该开始使用参数化和绑定查询,以保护自己免受SQL注入。

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";
    }
}
?>