php自动写入数据库 php自带数据库

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

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

措勤网站建设公司成都创新互联,措勤网站设计制作,有大型网站制作公司丰富经验。已为措勤上千余家提供企业网站建设服务。企业网站搭建\成都外贸网站建设公司要多少钱,请找那个售后服务好的措勤做网站的公司定做!

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 接收到之后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连接mysql,如何用php代码将数据写入数据库?》??

连接:mysql_connect("主机","用户","密码");

mysql_select_db("数据库名");

写入数据:mysql_query("insert

into

表名

(字段1,字段2)

values

("数据1","数据2")");

你的问题太含糊,只能给你这个答复,不明白再追问。

请教一个问题 关于PHp 写入数据库是自动更新的 add方法

问题说的不详细,说说本人的理解吧

同时在数据插入时允许更新操作:

add($data='',$options=array(),$replace=false)

其中add方法增加$replace参数(是否添加数据时允许覆盖),true表示覆盖,默认为false

那位大神之道php 怎么写 我登陆成功的时候可以自动将名字写入到数据库check表去

$username=$_SEEION['username'];

$sql="insert into check values(null,'$username');";

$mysqli-query($sql);

把上面的两条语句放在你的

if($_POST['act']=="login"){

} 里

这样每登录一次就插入一次


分享题目:php自动写入数据库 php自带数据库
文章地址:http://myzitong.com/article/hhpjcs.html