当点击提交按钮时,如何使用php oops概念将文本框值插入数据库mysql

问题描述:

<html>
<body>

<form action="database.php" method="post">
Name : <input type ="text" name = "Name"/>

Number  :<input type ="text" name = "Number"/>
<input type ="submit" value = "submit" name="submit"/>

</form>
</body>

</html>
<?php

class Database
{

    var $host;
    var $user;

    var $pass;
    var $data;

    var $con;
    var $table;

    var $db;
     public function controls()

    {
       $this->host="localhost";

        $this->user="root";
        $this->pass="";

        $this->data="employeedatabase";
       }

    public function connection()
    {

        $this->con = mysql_connect($this->host,$this->user,$this->pass);
    }

    public function tablename()
    {

        $this->table=mysql_query("INSERT INTO employee(name,number) VALUES ('".$_POST[name]."','".$_POST[number]."')");

    }
public function databaseconnection()

    {
        $this->db=mysql_select_db($this->data,$this->con);

    }
}

$name=new Database();
$name->controls();

$name->connection();
if(!($name->con))

{
    echo 'Error: ' . mysql_error();

}
$name->databaseconnection();

$name->tablename();
?>


这是html表单

<body>
<form action="process.php" method="post">
Name : <input type ="text" name = "Name"/>

Number  :<input type ="text" name = "Number"/>
<input type ="submit" value = "submit" name="submit"/>

</form>
</body>

这个包含该类的php文件名为db.php

This php file containing the class is named db.php

<?php
class db
{
    public $host;
    public $user;
    public $pass;
    public $data;
    public $con;
    public $table;
    function db()
    {
        $this->host="localhost";
        $this->user="usern";
        $this->pass="passwrd";
        $this->data="dbname";   
    }   
    public function connect()
    {
        $this->con=mysql_connect($this->host,$this->user,$this->pass);
        if(!$this->con)
        {
            echo mysql_error();
        }
        $sel=mysql_select_db($this->data, $this->con);
        if(!$sel)
        {
            echo mysql_error();
        }
    }
    public function insert($name,$number)
    {
        $sql=mysql_query("INSERT INTO tablename(name, number) VALUES('$name', '$number')");
        if(!$sql)
        {
            echo mysql_error();
        }
    }
}
?>

这个脚本适用于您在html表单的action属性中指定的php文件已命名为process.php

This script is for the php file which you specify in the "action" attribute of your html form i have named it "process.php"

<?php
    include'db.php';
    $name=$_POST['Name'];
    $num=$_POST['Number'];
    $n=new db();
    $n->connect();
    $n->insert($name,$num);
?>