<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。
<?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);
?>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句