
正文
php采集文章存入数据库 php提交数据到数据库
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
如何用Php语言把网页上的文章信息存入mysql数据库的表中
首先,php采集文章存入数据库你是不是要做采集.如果是.你要会PHP的正则表达式,去获取页面文章的标题,以及对应的内容php采集文章存入数据库,然后分别放到两个变量中。再通过SQL 写到数据库.
相关问答
Q1: php怎么读取txt文本内容存入mysql数据库
第一步php采集文章存入数据库,读取txt的文件。假设为a.txt
$content = file_get_content('a.txt'); //读取文件内容存入变量。
第二步,存入数据库
mysql_query("insert 表名 (字段名) values('".$content."'));
Psphp采集文章存入数据库:文件是上传的,上传后的临时文件名是php采集文章存入数据库:$_FILE['tmp_name']
Q2: 抓取网页数据怎么保存到数据库 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']}'"); //是否已经采集了
if(mysql_num_rows($rs))
{
echo "跳过:{$url}br";
continue;
}
$basedir='/u_file/pro/img/'.date('H/');
$save_dir=Build_dir($basedir); //创建目录函数
$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/')); //把远程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";
}
}
Q3: 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数据
Q4: php保存数据到数据库
需要转义一下。有几个办法:
1,最简单,把一些危险字符过滤掉,比如英文的单引号,双引号等;
2,使用addslashes在php中进行转义,自己处理;
3,可以使用一些现成的处理方法,比如uchome自带的getstr。这样的好处就是对各种可能出现的情况处理比较好。
php采集文章存入数据库的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于php提交数据到数据库、php采集文章存入数据库的信息别忘了在本站进行查找喔。






