-1
<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();
?>
4

2 回答 2

1

这是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

<?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”

<?php
    include'db.php';
    $name=$_POST['Name'];
    $num=$_POST['Number'];
    $n=new db();
    $n->connect();
    $n->insert($name,$num);
?>
于 2013-10-27T16:41:55.903 回答
0

在回答你的问题之前(我在这里没有看到任何问题)我想指出一些事实,

  1. 请不要在同一个页面中使用类和它的对象,OOPS 的想法是为代码带来可重用性,那么在同一个脚本中同时使用类和它的对象有什么意义呢?将类保存在单独的 php 文件中,然后使用将类包含在您的 php 脚本中

    include'filename.php'
    

    然后您可以在任何具有类似要求的脚本中使用该类

  2. 不要在函数中设置类变量,在构造函数中为变量提供值,这样您就不必调用单独的函数来为类变量设置值,因为每次创建对象时都会调用构造函数

  3. 指定类变量的访问控制模式,“public”或“private”或“protected”这是修改后的类文件,名为class.db.php

        class Database
         {
    
    var $host;
    var $user;
    
    var $pass;
    var $data;
    
    var $con;
    var $table;
    
    var $db;
     function Database()
    
      {
       $this->host="localhost";
    
        $this->user="root";
        $this->pass="";
    
        $this->data="employeedatabase";
       }
    
    public function connection()
    {
    
        $this->con = mysql_connect($this->host,$this->user,$this->pass);
    if(!$this->con)
        {
        echo mysql_error();
        }
    }
    
    public function tablename()
    {
    
        $this->table=mysql_query("INSERT INTO employee(name,number) VALUES ('".$_POST['name']."','".$_POST['number']."')");
    if(!$this->table)
        {
        echo mysql_error();
        }
    else
        {
          echo "success";
        }
    
    }
    public function databaseconnection()
    {
            $this->db=mysql_select_db($this->data,$this->con);
        if(!$this->db)
           {
    
            echo mysql_error();
           }
    
        }
    }
    

这是 database.php 的脚本

<?php
include'class.db.php';
$name=new Database();
$name->connection();
$name->databaseconnection();
$name->tablename();
?> 
于 2013-10-27T14:26:05.337 回答