单击Submit按钮时如何使用php oops概念将文本框值插入数据库mysql 姓名: 编号:
在回答你的问题之前(我这里没有看到任何问题),我想指出 一些事实,单击Submit按钮时如何使用php oops概念将文本框值插入数据库mysql 姓名: 编号:,php,html,mysql,Php,Html,Mysql,在回答你的问题之前(我这里没有看到任何问题),我想指出 一些事实, 请不要在同一个页面中使用类和它的对象,OOPS的理念就是要给代码带来可重用性,那么在同一个脚本中同时使用类和它的对象有什么意义呢?将该类保存在单独的php文件中,然后使用 <html> <body> <form action="database.php" method="post"> Name : <input type ="text" name = "Name"/> Numb
<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();
?>
然后,您可以在具有类似要求的任何脚本中使用该类include'filename.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();
}
}
}
这是html表单
<?php
include'class.db.php';
$name=new Database();
$name->connection();
$name->databaseconnection();
$name->tablename();
?>
姓名:
编号:
这个包含类的php文件名为db.php
<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 OOPS概念的新成员。单击提交按钮时,如何使用PHP OOPS概念将文本框值插入数据库mysql请定义“PHP OOPS概念”,我从未听说过。使用函数(面向对象编程)在phpSo中,您试图使用OOP在PHP中编写一个数据库类,它允许您在MySQL数据库中插入一个textbox值。对的如果正确-请编辑您的问题:1-问题是什么?它不起作用吗?出现错误了吗?你想让我们告诉你什么?2-你的代码是什么?你试过什么?3-你在用什么?PHP?MySQL?MySQLi?什么版本?不要在这里猛击你的代码,并大声喊allcaps“请帮助我,我第一次添加查询时,我是PHP OOPS概念的新成员”。请提出一个问题,我们会回答你的。这是一个问答网站,不是一个代码转储位置。感谢您的回复。但是数据库没有连接。将错误作为致命错误获取:在第3行的C:\xampp\htdocs\database1.php中找不到类“database”。。我编辑了相同的类名和函数,修改后执行,nw无错误,但当我单击按钮时,文件本身正在下载..请告诉我如何添加submit按钮和weather以在submit type中添加onclick关键字,后跟函数名。请参见前面的代码:)单击submit按钮时如何使用php oops概念将文本框值插入数据库mysql,如果您对此有任何代码,请在此处输入:)@user2924931您是从localhost运行此代码的吗?“文件正在下载”是什么意思?如果文件被下载了,您是如何得到错误的?非常感谢..我对php oops的概念非常陌生,谢谢您的帮助:)
<?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();
}
}
}
?>
<?php
include'db.php';
$name=$_POST['Name'];
$num=$_POST['Number'];
$n=new db();
$n->connect();
$n->insert($name,$num);
?>