php实现添加数据库代码,php实现添加数据库代码命令

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

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

成都创新互联是一家集网站建设,莫力达企业网站建设,莫力达品牌网站建设,网站定制,莫力达网站建设报价,网络营销,网络优化,莫力达网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。

现在,我们创建一个 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 表单:

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代码怎么往数据库里自定义插入数据

现在,我们创建一个

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怎么把数据导入数据库

需要PHP基础知识和数据库基础知识。

以SQL为例。使用PHP MySQL 函数可以编辑数据库。

mysql_connect() 函数打开MySQL 连接。举例

?php

$con = mysql_connect("localhost","mysql_user","mysql_pwd");

if (!$con)

{

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

}// 一些代码...mysql_close($con);

?

mysql_connect()三个参数分别是服务器名,连接账号,连接密码。

连接之后,可以使用mysql_select_db()设置要处理的数据库,后面则是用数据库语句处理数据。SQL语法简介网页链接

网页题目:php实现添加数据库代码,php实现添加数据库代码命令
文章网址:https://www.cdcxhl.com/article40/hspoho.html

成都网站建设公司_创新互联,为您提供网站改版品牌网站设计软件开发网站策划商城网站全网营销推广

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

微信小程序开发