可能重复:
Inserting values into database using object oriented programming
我对PHP中面向对象的概念很新。我一直在尝试使用面向对象的概念将我的表单的值插入到数据库中。我没有收到任何错误消息,但值没有在数据库上更新。
请仔细看看代码,让我知道我应该做些什么改变才能让它发挥作用。
Input.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>
database.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";
?>
答案 0 :(得分:0)
尝试更改
$this->table="Insert into table(name,number) values('$_POST[name]','$_POST[number]')";
类似
$this->table="Insert into sometable(name,number) values('$_POST[name]','$_POST[number]')";
确保您的数据库也已更新。
看起来表不是表的有效名称。这是一个SQL关键字。
您没有调用任何mysql_db_query函数或同等函数。
编辑:
不推荐使用mysql_db_query。使用给定链接中的替代方案。