189 8069 5689

php配置信息写入数据库 php配置数据库连接

怎么用php把html表单内容写入数据库

1:首先要使用PHP的超全局变量 $_GET 和 $_POST 用于收集表单数据(form-data)

成都创新互联公司是一家专业提供赫山企业网站建设,专注与成都网站设计、网站建设H5高端网站建设、小程序制作等业务。10年已为赫山众多企业、政府机构等服务。创新互联专业的建站公司优惠进行中。

2:然后使用INSERT INTO 语句用于向数据库表中插入新记录。

具体示例:

(1)首先创建了一个名为 "Persons" 的表,有三个列:"Firstname", "Lastname" 以及 "Age"。

?php

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

if (!$con)

{

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

}

mysql_select_db("my_db", $con);

mysql_query("INSERT INTO Persons (FirstName, LastName, Age)

VALUES ('Peter', 'Griffin', '35')");

mysql_query("INSERT INTO Persons (FirstName, LastName, Age)

VALUES ('Glenn', 'Quagmire', '33')");

mysql_close($con);

?

(2)其次创建一个 HTML 表单,这个表单可把新记录插入 "Persons" 表。

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

(3)接着当用户点击上例中 HTML 表单中的提交按钮时,表单数据被发送到 "insert.php"。"insert.php" 文件连接数据库,并通过

$_POST 变量从表单取回值。然后,mysql_query() 函数执行 INSERT INTO 语句,一条新的记录会添加到数据库表中。

?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如何将当前登录用户的信息获取写入数据库

添加新闻的时候是有表单的吧 那么 就在表单form里加上一个 input type="hidden" name=“username” value="$username"

其中 value 记录的是你当前用户名 比如 value=“管理员”

然后在你接收表单传值的页面 比如 save.php 里接收 具体是 post 还是 get 看你 form 李 method 的写法。。。比如 $username=$_POST['name'];

然后把$username存到数据库里

sql 语句 insert into news_table set username='$username'.............

具体的代码 得看你的系统 框架什么的 但是原理就这么个原理 表单传值 接收 数据存储

php 接收到之后post数据写入数据库

form表单demo:task.html

fieldset id="setFiled"

legend发布任务/legend

form action="registr.php" method="post" id="steForm"

label任务类型:/labelbr

input type="text" name="type"  id="taskType" placeholder="请选择任务类型"/br

label酬nbsp;nbsp;金:/labelbr

input type="number" name="money" id="forMoney" min="1" max="1000"/label元/labelbr

label截止时间:/labelbr

input type="datetime" name="time" id="timeSubmit"/span data-year="" data-month="" data-date="" id="showDate"/spanbr

label详细描述:/labelbr

textarea maxlength="512" name="textAray" id="msgArea"/textareabr

input type="submit" name="subMit" id="forSub" value="点击发布" /

/form

扩展资料

php接收POST数据的三种方式

1、$_POST 方式接受数据

$_POST 方式是由通过HTTP的POST方法传递过来的数据组成的数组,是一个自动全局变量。

注:只能接收Content-Type:application/x-www-form-urlencode提交的数据。也就是只能接收表单过来的数据。

2、GLOBLES[‘HTTP_RAW_POST_DATA’]

如果访问原始POST数据不是php能够识别的文档类型,比如:text/xml 或者soap等等,可以用$GLOBLES[‘HTTP_RAW_POST_DATA’]来接收,$HTTP_RAW_POST_DATA变量包含有原始POST数据。此变量仅在碰到未识别的MIME数据时产生。

注:$HTTP_RAW_POST_DATA对于enctype=”multipart/form-data”表单数据不可用,也就是说使用$HTTP_RAW_POST_DATA无法接受网页表单post过来的数据。

3、file_get_contents(“php://input”);

如果访问原始POST数据,更好的方法是使用file_get_content(“php://input”);对于未指定Content-Type的POST数据,可以使用该方法读取POST原始数据,包括二进制流也可以和$HTTP_RAW_POST_DATA比起来。它带来的生存眼里更小,并且不需要任何特殊的php.ini设置。

注:php://input不能用于 enctype=”multipart/form-data”

例如:$postStr = file_get_contents("php://input"); //获取POST数据

php如何写入数据库

数组吧,直接把数组转字符串啊

implode() 函数返回由数组元素组合成的字符串。(适合一维数组)

$arr = array('Hello', 'World', 'I', 'love', 'Shanghai');

1 echo implode(" ",$arr);//加空格

the result : Hello World I love Shanghai

2 echo implode(",",$arr);//加逗号

the result : Hello,World,I,love,Shanghai

转换数组为字符串后插入数据库就可以了。


文章标题:php配置信息写入数据库 php配置数据库连接
网站网址:http://cdxtjz.cn/article/ddgdosc.html

其他资讯