可能重复:
使用面向对象编程将值插入数据库
我对 PHP 中面向对象的概念非常陌生。我一直在尝试使用面向对象的概念将表单的值插入数据库。我没有收到任何错误消息,但数据库中的值没有得到更新。
请看一下代码,让我知道我应该做哪些更改才能使其正常工作。
输入.php
<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"/>
</form>
</body>
</html>
数据库.php
<?php
class Database
{
var $host;
var $user;
var $pass;
var $data;
var $con;
var $table;
var $db;
public function hostname()
{
$this->host="localhost";
}
public function username()
{
$this->user="cgiadmin";
}
public function password()
{
$this->pass="cgi";
}
public function databasename()
{
$this->data="j2";
}
public function connection()
{
$this->con="mysql_connect($this->host,$this->user,$this->pass)";
}
public function tablename()
{
$this->table="Insert into table(name,number) values('$_POST[name]','$_POST[number]')";
}
public function databaseconnection()
{
$this->db="mysql_select_db($this->data,$this->con)";
}
public function mysql_close()
{
}
}
$name=new Database;
$name->connection();
$name->databaseconnection();
$name->tablename();
echo "thanks for taking the survey";
$name->mysql_close();
?>
请让我知道需要做的更改。谢谢你 。
编辑
我做了一些改变,但它似乎仍然没有用。请帮忙
<?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="cgiadmin";
$this->pass="cgi";
$this->data="j2";
}
public function connection()
{
$this->con="mysql_connect($this->host,$this->user,$this->pass)";
}
public function tablename()
{
$this->table="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->connection();
$name->databaseconnection();
$name->tablename();
echo "thanks for taking the survey";
?>