php向数据库增加 php数据库修改语句

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

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

创新互联长期为上1000家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为乌鲁木齐企业提供专业的成都网站设计、成都网站制作,乌鲁木齐网站改版等技术服务。拥有10多年丰富建站经验和众多成功案例,为您定制开发。

现在,我们创建一个 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代码怎么往数据库里自定义插入数据

现在,我们创建一个

HTML

表单,这个表单可把新记录插入

"Persons"

表。

这是这个

HTML

表单:

123456789101112

htmlbody

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

语句,一条新的记录会添加到数据库表中。

php网站怎么添加mysql数据

首先创建

一个HTML页面userinfo_add.php,在里面输入表单,文本框,输入需要提交的到数据库的信息:

账号

姓名

年龄

页面运行结果:

创建一个PHP文件(userinfo_insert.php),用来处理页面请求的,就是具体往数据库添加数据的代码:

先获取页面数据

//通过post获取页面提交数据信息

$userId

=

$_POST[userId];

$userName

=

$_POST[userName];

$userAge

=

$_POST[userAge];

接下来,在连接数据库

‘test’

//地址

$url

=

"127.0.0.1";

//账号

$user

=

"root";

//密码

$password

=

"root";

//连接

$con

=

mysql_connect($url,$user,$password);

//设置编码机

mysql_query("set

names

'utf8'");

//连接数据库

mysql_select_db("test");

编写SQL,执行SQL添加数据

$sql

=

"insert

into

user_info

(user_id,user_name,user_age)

values('$userId','$userName','$userAge')";

if

(!mysql_query($sql,$con))

{

die('Error:

'

.

mysql_error());

}

echo

"添加一条记录";

//关闭连接

mysql_close($con)

运行结果前:

运行结果后:

完整代码:

PHP加数据库

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

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

这是这个 HTML 表单:

1

2

3

4

5

6

7

8

9

10

11

12

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" 页面的代码:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

?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向数据库增加 php数据库修改语句
标题URL:http://myzitong.com/article/hpgpjh.html