php实现变化数据库 php中数据库怎么设计

PHP在网站上实现跟数据库添加数据

把来自表单的数据插入数据库

创新互联从2013年创立,是专业互联网技术服务公司,拥有项目做网站、网站设计网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元北流做网站,已为上家服务,为北流各地企业和个人服务,联系电话:18980820575

现在,我们创建一个 HTML 表单,这个表单可把新记录插入 "Persons" 表。

这是这个 HTML 表单:

html

body

form action="insert.php" method="post"

Firstname: input type="text" name="firstname" /

Lastname: input type="text" name="lastname" /

Age: input type="text" name="age" /

input type="submit" /

/form

/body

/html

当用户点击上例中 HTML 表单中的提交按钮时,表单数据被发送到 "insert.php"。"insert.php" 文件连接数据库,并通过 $_POST 变量从表单取回值。然后,mysql_query() 函数执行 INSERT INTO 语句,一条新的记录会添加到数据库表中。

下面是 "insert.php" 页面的代码:

?php

$con = mysql_connect("localhost","peter","abc123");

if (!$con)

{

die('Could not connect: ' . mysql_error());

}

mysql_select_db("my_db", $con);

$sql="INSERT INTO Persons (FirstName, LastName, Age)

VALUES

('$_POST[firstname]','$_POST[lastname]','$_POST[age]')";

if (!mysql_query($sql,$con))

{

die('Error: ' . mysql_error());

}

echo "1 record added";

mysql_close($con)

?

如何用PHP代码实现MySQL数据库的增删改查

?php

$con = mysql_connect("localhost:3306","root","");

if (!$con) {

die('Could not connect: ' . mysql_error());

}

mysql_select_db("test", $con);

$result = mysql_query("SELECT * FROM user");

echo "table border='1'

tr

thUsername/th

thPassword/th

/tr";

while($row = mysql_fetch_array($result)) {

echo "tr";

echo "td" . $row['username'] . "/td";

echo "td" . $row['password'] . "/td";

echo "/tr";

}

echo "/table";

mysql_close($con);

?

从服务器中获取用户所有信息(SQL SELECT语句)并以表格形式出现

?php

$con = mysql_connect("localhost","root","");

if (!$con) {

die('Could not connect: ' . mysql_error());

}

mysql_select_db("test", $con);

mysql_query("DELETE FROM user WHERE username = '$_POST[username]'");

mysql_close($con);

?

删除该用户所有信息delete.php

?php

$con = mysql_connect("localhost:3306","root","");

if (!$con) {

die('Could not connect: ' . mysql_error());

}

mysql_select_db("test", $con);

$sql = "INSERT INTO user (username,password)

VALUES

('$_POST[username]','$_POST[password]')";

if (!mysql_query($sql,$con)) {

die('Error: ' . mysql_error());

}

echo "1 record added";

mysql_close($con);

?

注册一个新用户insert.php

?php

$con = mysql_connect("localhost","root","");

if (!$con) {

die('Could not connect: ' . mysql_error());

}

mysql_select_db("test", $con);

mysql_query("UPDATE user SET password = '$_POST[password]' WHERE username = '$_POST[username]'");

mysql_close($con);

?

修改一个用户密码update.php

html

head

titleFORM/title

/head

body

br /

h1Insert:/h1

form action="insert.php" method="post"

username:input type="name" name="username"/

br /

password:input type="password" name="password"/

input type="submit" value="submit"/

/form

br /hr /br /

h1Delete/h1

form action="delete.php" method="post"

username:input type="name" name="username" /

br /

Are you sure?input type="submit" value="sure" /

/form

br /hr /br /

h1Update/h1

form action="update.php" method="post"

username:input type="name" name="username"/

br /

You want to change your password into:input type="password" name="password"/

input type="submit" value="submit"/

/form

br /hr /br /

/body

/html

以上三个功能的提交源Operate.html

PHP如何实现一个高效的数据库

你做好程序以后,把数据库导出成sql文件

1、连接数据库

2、读取这个sql文件里的sql语句,并执行

3、生成一个数据库连接参数的php文件

?php

$con = mysql_connect("localhost","peter","abc123");

if (!$con)

{

die('Could not connect: ' . mysql_error());

}

if (mysql_query("CREATE DATABASE my_db",$con))

{

echo "Database created";

}

else

{

echo "Error creating database: " . mysql_error();

}

mysql_close($con);

?

?php

class ReadSql {

//数据库连接

protected $connect = null;

//数据库对象

protected $db = null;

//sql文件

public $sqlFile = "";

//sql语句集

public $sqlArr = array();

public function __construct($host, $user, $pw, $db_name) {

$host = empty($host) ? C("DB_HOST") : $host;

$user = empty($user) ? C("DB_USER") : $user;

$pw = empty($pw) ? C("DB_PWD") : $pw;

$db_name = empty($db_name) ? C("DB_NAME") : $db_name;

//连接数据库

$this-connect = mysql_connect($host, $user, $pw) or die("Could not connect: " . mysql_error());

$this-db = mysql_select_db($db_name, $this-connect) or die("Yon can not select the table:" . mysql_error());

}

//导入sql文件

public function Import($url) {

$this-sqlFile = file_get_contents($url);

if (!$this-sqlFile) {

exit("打开文件错误");

} else {

$this-GetSqlArr();

if ($this-Runsql()) {

return true;

}

}

}

//获取sql语句数组

public function GetSqlArr() {

//去除注释

$str = $this-sqlFile;

$str = preg_replace('/--.*/i', '', $str);

$str = preg_replace('/\/\*.*\*\/(\;)?/i', '', $str);

//去除空格 创建数组

$str = explode(";\n", $str);

foreach ($str as $v) {

$v = trim($v);

if (empty($v)) {

continue;

} else {

$this-sqlArr[] = $v;

}

}

}

//执行sql文件

public function RunSql() {

foreach ($this-sqlArr as $k = $v) {

if (!mysql_query($v)) {

exit("sql语句错误:第" . $k . "行" . mysql_error());

}

}

return true;

}

}

//范例:

header("Content-type:text/html;charset=utf-8");

$sql = new ReadSql("localhost", "root", "", "log_db");

$rst = $sql-Import("./log_db.sql");

if ($rst) {

echo "Success!";

}

?


网站名称:php实现变化数据库 php中数据库怎么设计
转载来于:http://scyanting.com/article/ddgsejp.html