php怎样用面向对象的方法将表单值插入数据库?那个方法要怎么写?
展开全部
一个简单的例子
<?php
include 'db_content.php'; //数据库连接
class db
{
public function insert($username)
{
$sql = "insert into user (id,username) values (null, $username)";
mysql_query($sql);
}
}
if (isset($_POST['sub']))
{
$db = new db();
$db->insert($_POST['username']);
}
?>
<form action="" method="post">
<input type="text" name="username" value="">
<input type="submit" name="sub" value="提交">
</form>
<?php
include 'db_content.php'; //数据库连接
class db
{
public function insert($username)
{
$sql = "insert into user (id,username) values (null, $username)";
mysql_query($sql);
}
}
if (isset($_POST['sub']))
{
$db = new db();
$db->insert($_POST['username']);
}
?>
<form action="" method="post">
<input type="text" name="username" value="">
<input type="submit" name="sub" value="提交">
</form>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询