php抓取數(shù)據(jù)到數(shù)據(jù)庫 php獲取數(shù)據(jù)庫

php 接收到之后post數(shù)據(jù)寫入數(shù)據(jù)庫

form表單demo:task.html

公司主營業(yè)務(wù):網(wǎng)站制作、網(wǎng)站設(shè)計、移動網(wǎng)站開發(fā)等業(yè)務(wù)。幫助企業(yè)客戶真正實現(xiàn)互聯(lián)網(wǎng)宣傳,提高企業(yè)的競爭能力。成都創(chuàng)新互聯(lián)是一支青春激揚(yáng)、勤奮敬業(yè)、活力青春激揚(yáng)、勤奮敬業(yè)、活力澎湃、和諧高效的團(tuán)隊。公司秉承以“開放、自由、嚴(yán)謹(jǐn)、自律”為核心的企業(yè)文化,感謝他們對我們的高要求,感謝他們從不同領(lǐng)域給我們帶來的挑戰(zhàn),讓我們激情的團(tuán)隊有機(jī)會用頭腦與智慧不斷的給客戶帶來驚喜。成都創(chuàng)新互聯(lián)推出北鎮(zhèn)免費(fèi)做網(wǎng)站回饋大家。

fieldset id="setFiled"

legend發(fā)布任務(wù)/legend

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

label任務(wù)類型:/labelbr

input type="text" name="type"? id="taskType" placeholder="請選擇任務(wù)類型"/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詳細(xì)描述:/labelbr

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

input type="submit" name="subMit" id="forSub" value="點擊發(fā)布" /

/form

擴(kuò)展資料

php接收POST數(shù)據(jù)的三種方式

1、$_POST 方式接受數(shù)據(jù)

$_POST 方式是由通過HTTP的POST方法傳遞過來的數(shù)據(jù)組成的數(shù)組,是一個自動全局變量。

注:只能接收Content-Type:application/x-www-form-urlencode提交的數(shù)據(jù)。也就是只能接收表單過來的數(shù)據(jù)。

2、GLOBLES[‘HTTP_RAW_POST_DATA’]

如果訪問原始POST數(shù)據(jù)不是php能夠識別的文檔類型,比如:text/xml 或者soap等等,可以用$GLOBLES[‘HTTP_RAW_POST_DATA’]來接收,$HTTP_RAW_POST_DATA變量包含有原始POST數(shù)據(jù)。此變量僅在碰到未識別的MIME數(shù)據(jù)時產(chǎn)生。

注:$HTTP_RAW_POST_DATA對于enctype=”multipart/form-data”表單數(shù)據(jù)不可用,也就是說使用$HTTP_RAW_POST_DATA無法接受網(wǎng)頁表單post過來的數(shù)據(jù)。

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

如果訪問原始POST數(shù)據(jù),更好的方法是使用file_get_content(“php://input”);對于未指定Content-Type的POST數(shù)據(jù),可以使用該方法讀取POST原始數(shù)據(jù),包括二進(jìn)制流也可以和$HTTP_RAW_POST_DATA比起來。它帶來的生存眼里更小,并且不需要任何特殊的php.ini設(shè)置。

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

例如:$postStr = file_get_contents("php://input"); //獲取POST數(shù)據(jù)

如何利用php讀取txt文件再將數(shù)據(jù)插入到數(shù)據(jù)庫

serial_number.txt的示例內(nèi)容:

serial_number.txt:

DM00001A11 0116,

SN00002A11 0116,

AB00003A11 0116,

PV00004A11 0116,

OC00005A11 0116,

IX00006A11 0116,

創(chuàng)建數(shù)據(jù)表:

create table serial_number(

id int primary key auto_increment not null,

serial_number varchar(50) not null

)ENGINE=InnoDB DEFAULT CHARSET=utf8;

php代碼如下:

$conn = mysql_connect('127.0.0.1','root','') or die("Invalid query: " . mysql_error());

mysql_select_db('test', $conn) or die("Invalid query: " . mysql_error());

$content = file_get_contents("serial_number.txt");

$contents= explode(",",$content);//explode()函數(shù)以","為標(biāo)識符進(jìn)行拆分

foreach ($contents as $k = $v)//遍歷循環(huán)

{

$id = $k;

$serial_number = $v;

mysql_query("insert into serial_number (`id`,`serial_number`)

VALUES('$id','$serial_number')");

}

備注:方法有很多種,我這里是在拆分txt文件為數(shù)組后,然后遍歷循環(huán)得到的數(shù)組,每循環(huán)一次,往數(shù)據(jù)庫中插入一次。

再給大家分享一個支持大文件導(dǎo)入的

?php

/**

* $splitChar 字段分隔符

* $file 數(shù)據(jù)文件文件名

* $table 數(shù)據(jù)庫表名

* $conn 數(shù)據(jù)庫連接

* $fields 數(shù)據(jù)對應(yīng)的列名

* $insertType 插入操作類型,包括INSERT,REPLACE

*/

function loadTxtDataIntoDatabase($splitChar,$file,$table,$conn,$fields=array(),$insertType='INSERT'){

if(empty($fields)) $head = "{$insertType} INTO `{$table}` VALUES('";

else $head = "{$insertType} INTO `{$table}`(`".implode('`,`',$fields)."`) VALUES('"; //數(shù)據(jù)頭

$end = "')";

$sqldata = trim(file_get_contents($file));

if(preg_replace('/\s*/i','',$splitChar) == '') {

$splitChar = '/(\w+)(\s+)/i';

$replace = "$1','";

$specialFunc = 'preg_replace';

}else {

$splitChar = $splitChar;

$replace = "','";

$specialFunc = 'str_replace';

}

//處理數(shù)據(jù)體,二者順序不可換,否則空格或Tab分隔符時出錯

$sqldata = preg_replace('/(\s*)(\n+)(\s*)/i','\'),(\'',$sqldata); //替換換行

$sqldata = $specialFunc($splitChar,$replace,$sqldata); //替換分隔符

$query = $head.$sqldata.$end; //數(shù)據(jù)拼接

if(mysql_query($query,$conn)) return array(true);

else {

return array(false,mysql_error($conn),mysql_errno($conn));

}

}

//調(diào)用示例1

require 'db.php';

$splitChar = '|'; //豎線

$file = 'sqldata1.txt';

$fields = array('id','parentid','name');

$table = 'cengji';

$result = loadTxtDataIntoDatabase($splitChar,$file,$table,$conn,$fields);

if (array_shift($result)){

echo 'Success!br/';

}else {

echo 'Failed!--Error:'.array_shift($result).'br/';

}

/*sqlda ta1.txt

1|0|A

2|1|B

3|1|C

4|2|D

-- cengji

CREATE TABLE `cengji` (

`id` int(11) NOT NULL AUTO_INCREMENT,

`parentid` int(11) NOT NULL,

`name` varchar(255) DEFAULT NULL,

PRIMARY KEY (`id`),

UNIQUE KEY `parentid_name_unique` (`parentid`,`name`) USING BTREE

) ENGINE=InnoDB AUTO_INCREMENT=1602 DEFAULT CHARSET=utf8

*/

//調(diào)用示例2

require 'db.php';

$splitChar = ' '; //空格

$file = 'sqldata2.txt';

$fields = array('id','make','model','year');

$table = 'cars';

$result = loadTxtDataIntoDatabase($splitChar,$file,$table,$conn,$fields);

if (array_shift($result)){

echo 'Success!br/';

}else {

echo 'Failed!--Error:'.array_shift($result).'br/';

}

/* sqldata2.txt

11 Aston DB19 2009

12 Aston DB29 2009

13 Aston DB39 2009

-- cars

CREATE TABLE `cars` (

`id` int(11) NOT NULL AUTO_INCREMENT,

`make` varchar(16) NOT NULL,

`model` varchar(16) DEFAULT NULL,

`year` varchar(16) DEFAULT NULL,

PRIMARY KEY (`id`)

) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=utf8

*/

//調(diào)用示例3

require 'db.php';

$splitChar = ' '; //Tab

$file = 'sqldata3.txt';

$fields = array('id','make','model','year');

$table = 'cars';

$insertType = 'REPLACE';

$result = loadTxtDataIntoDatabase($splitChar,$file,$table,$conn,$fields,$insertType);

if (array_shift($result)){

echo 'Success!br/';

}else {

echo 'Failed!--Error:'.array_shift($result).'br/';

}

/* sqldata3.txt

11 Aston DB19 2009

12 Aston DB29 2009

13 Aston DB39 2009

*/

//調(diào)用示例3

require 'db.php';

$splitChar = ' '; //Tab

$file = 'sqldata3.txt';

$fields = array('id','value');

$table = 'notExist'; //不存在表

$result = loadTxtDataIntoDatabase($splitChar,$file,$table,$conn,$fields);

if (array_shift($result)){

echo 'Success!br/';

}else {

echo 'Failed!--Error:'.array_shift($result).'br/';

}

//附:db.php

/* //注釋這一行可全部釋放

?

?php

static $connect = null;

static $table = 'jilian';

if(!isset($connect)) {

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

if(!$connect) {

$connect = mysql_connect("localhost","Zjmainstay","");

}

if(!$connect) {

die('Can not connect to database.Fatal error handle by /test/db.php');

}

mysql_select_db("test",$connect);

mysql_query("SET NAMES utf8",$connect);

$conn = $connect;

$db = $connect;

}

?

//*/

.

-- 數(shù)據(jù)表結(jié)構(gòu):

-- 100000_insert,1000000_insert

CREATE TABLE `100000_insert` (

`id` int(11) NOT NULL AUTO_INCREMENT,

`parentid` int(11) NOT NULL,

`name` varchar(255) DEFAULT NULL,

PRIMARY KEY (`id`)

) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8

100000 (10萬)行插入:Insert 100000_line_data use 2.5534288883209 seconds

1000000(100萬)行插入:Insert 1000000_line_data use 19.677318811417 seconds

//可能報錯:MySQL server has gone away

//解決:修改my.ini/my.cnf max_allowed_packet=20M

php怎么把數(shù)據(jù)導(dǎo)入數(shù)據(jù)庫

需要PHP基礎(chǔ)知識和數(shù)據(jù)庫基礎(chǔ)知識。

以SQL為例。使用PHP MySQL 函數(shù)可以編輯數(shù)據(jù)庫。

mysql_connect() 函數(shù)打開MySQL 連接。舉例

?php

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

if (!$con)

{

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

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

?

mysql_connect()三個參數(shù)分別是服務(wù)器名,連接賬號,連接密碼。

連接之后,可以使用mysql_select_db()設(shè)置要處理的數(shù)據(jù)庫,后面則是用數(shù)據(jù)庫語句處理數(shù)據(jù)。SQL語法簡介網(wǎng)頁鏈接

抓取網(wǎng)頁數(shù)據(jù)怎么保存到數(shù)據(jù)庫 php

給一個例子你看看吧.

if($pro_list_contents=@file_get_contents(''))

{

preg_match_all("/td width=\"50%\" valign=\"top\"(.*)td width=\"10\"img src=\"images\/spacer.gif\"/isU", $pro_list_contents, $pro_list_contents_ary);

for($i=0; $icount($pro_list_contents_ary[1]); $i++)

{

preg_match_all("/a href=\"(.*)\"img src=\"(.*)\".*span(.*)\/span/isU", $pro_list_contents_ary[1][$i], $url_img_price);

$url=addslashes($url_img_price[1][0]);

$img=str_replace(' ', '20%', trim(''.$url_img_price[2][0]));

$price=(float)str_replace('$', '', $url_img_price[3][0]);

preg_match_all("/a class=\"ml1\" href=\".*\"(.*)\/a/isU", $pro_list_contents_ary[1][$i], $proname_ary);

$proname=addslashes($proname_ary[1][0]);

include("inc/db_connections.php");

$rs=mysql_query("select * from pro where Url='$url' and CateId='{$cate_row['CateId']}'"); //是否已經(jīng)采集了

if(mysql_num_rows($rs))

{

echo "跳過:{$url}br";

continue;

}

$basedir='/u_file/pro/img/'.date('H/');

$save_dir=Build_dir($basedir); //創(chuàng)建目錄函數(shù)

$ext_name = GetFileExtName( $img ); //取得圖片后輟名

$SaveName = date( 'mdHis' ) . rand( 10000, 99999 ) . '.' . $ext_name;

if( $get_file=@file_get_contents( $img ) )

{

$fp = @fopen( $save_dir . $SaveName, 'w' );

@fwrite( $fp, $get_file );

@fclose( $fp );

@chmod( $save_dir . $SaveName, 0777 );

@copy( $save_dir . $SaveName, $save_dir . 'small_'.$SaveName );

$imgpath=$basedir.'small_'.$SaveName;

}

else

{

$imgpath='';

}

if($pro_intro_contents=@file_get_contents($url))

{

preg_match_all("/\/h1(.*)\/td\/tr/isU", $pro_intro_contents, $pro_intro_contents_ary);

$p_contents=addslashes(str_replace('src="', 'src="', $pro_intro_contents_ary[1][0]));

$p_contents=SaveRemoteImg($p_contents, '/u_file/pro/intro/'.date('H/')); //把遠(yuǎn)程html代碼里的圖片保存到本地

}

$t=time();

mysql_query("insert into pro(CateId, ProName, PicPath_0, S_PicPath_0, Price_0, Contents, AddTime, Url) values('{$cate_row['CateId']}', '$proname', '$imgpath', '$img', '$price', '$p_contents', '$t', '$url')");

echo $url.$img.$cate."br\r\n";

}

}

新聞名稱:php抓取數(shù)據(jù)到數(shù)據(jù)庫 php獲取數(shù)據(jù)庫
網(wǎng)頁路徑:http://bm7419.com/article30/dohpsso.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站改版網(wǎng)站設(shè)計公司、做網(wǎng)站、網(wǎng)站策劃、Google、企業(yè)網(wǎng)站制作

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)