您的位置:首页 > 编程语言 > PHP开发

php简单实现通讯录采集,我的第一个php,适合新手

2014-09-10 16:41 375 查看
起源于要整理通讯录,原先用的是文件调查,现在学了php,就自己试一下.
程序短小精悍,适于学习.
有两个文件,bj.html用于显示和采集表单信息.bj.php用于处理数据和反馈结果.
突出之处在于可以上传阅览头像.
其实还有一个test.php,用于调试,中间处理.从这个开始先看.
注释很详细,标签,函数不会的话右键搜索.
因为web目录很多,放在./1/文件夹下,在火狐设一个书签方便访问
部署时吧bj.html改成index.php

原先想着搭建一个ipv6免费上网的应用平台(现在知道了是oauth开放平台),就开始学php,php是最简单的了,可也学了一年才有第一个程序,慎入!
​1. [文件] bj.html ~ 2KB
<!-- 原先想着搭建一个ipv6免费上网的应用平台,就开始学php,php是最简单的了,可也学了一年才有第一个程序,慎入! -->

<!-- 这里尽量简洁的代码,全都是键入的.没有多余
使用了一些html5功能,没有用日期,电话等功能,没必要,而且输入麻烦,tel标签还没内容. -->

<!doctype html><!-- html5 声明 chrome较好 -->
<html>
<head>
<meta http-equiv="Content-type" content="text/html;charset=utf-8">
<title>2014年通讯录</title>
</head>

<body>
<h3>2014年通讯录</h3>
<form enctype="multipart/form-data" action='bj.php' method="post"><!-- 必填 -->
姓名<input type="text" name="name" />
小名<input type="text" name="nickname" /><br>
性别<input type="text" name="gender" placeholder="男/女" />
生日<input type="text" name="birthday" placeholder="比如:1995-5-5"/><p><!-- html5 新标签 -->

电话<input type="number" name="phone" />
qq  <input type="number" name="qq" /><!-- 就单个页面来说,手动调一下就行了 -->
email<input type="email" name="email" />
老家<input type="text" name="home" placeholder="比如:板岩中学" /><p>

学校<input type="text" name="college" />
年级<input type="text" name="grade" placeholder="比如:本科2013级"/>
专业<input type="text" name="major" /> <br>
公司<input type="text" name="company" />
职务<input type="text" name="position" />
地址<input type="text" name="address" />
<p><!-- 本来要填账号,为了方便点击和输入,使用url -->
微博主页   <input type="url" name="weibolink" size=70 placeholder="比如:http://weibo.com/2025922571/profile下同" /> <br>
人人主页   <input type="url" name="renrenlink" size=70 /><br>
qq空间主页 <input type="url" name="qqzonelink" size=70 /> <br>
<p></p><!-- 特色功能,有图有真相,还有个简介做备注 -->
头像<input type="file" name="photo" /><p>
简介<textarea name="profile" > </textarea><p>
<input type="submit" name="submit" value="ok!" />

</form>
</body>
</html>

<!-- laohur@gmail.com以防联系 -->
2. [文件] bj.php ~ 3KB
<meta http-equiv="Content-type" content="script/php;charset=utf-8">
<?php
//保存图片文件
@header('Content-type: text/html;charset=UTF-8');<!-- 声明编码 -->

//上传图片
$filepath=$_FILES["photo"]["tmp_name"];
$filename=explode('.',$_FILES['photo']['name']);
$filename[0]=$_POST['name'];
$name=implode('.',$filename);

$uploaded="./photos/".$name;
if(file_exists($filepath)){
move_uploaded_file($filepath,$uploaded);
//图片链接插入数据库,方便点击和下载查看,没必要吧图片本身上传到数据库,编码还解码,
$_POST['photo']="http://localhost/1/c/photos/".$name;
echo "<br>hello,<br>".$_POST["name"]."!<br><img src='".$uploaded."' <p>";//返回成功信息,而且是自己的头像

}

<!-- 这个字符串是最初开始有的,设计时键入的. 然后在后面以数组形式插入数据库时,中间经过了很多次在test.php上实验,从$arr到$arr5,中间不停滴var_dump($arr),最终挑了一个.没必要吧处理脚本笑出来.我费了好大劲,发现太麻烦了,而这也是一个中间件过程,没必要,只要有结果就好,本地手动处理完毕就好,中间怎么处理的,办法就很多了 -->

$arr1="name,nickname,gender,birthday,phone,qq,email,home,college,grade,major,company,position,address,weibolink,renrenlink,qqzonelink,photo,profile";

//没想好怎么防止sql注入,就用pdo_prepare

$db=new PDO("mysql:host=localhost;dbname=test","root","") or die(print_r($db->errorInfo(),true));

//下面是尝试的代码,因为mysql乱码,试了下这个,最终不行.只好换了mariadb,整个世界就清净了.可以删掉,用以参考.
mysqli_query("set names ’utf8’ ");
mysqli_query("set character_set_client=utf8");
mysqli_query("set character_set_results=utf8");

//这是唯一一个自动处理脚本,最初想着把运算都放在这里其实这个是变量静态的,被当地处理完毕再放上来未尝不可,因为简单,实现了,本地把 '?'.'?,'*18 的结果贴出来就可以了.
$s="?";
for($j=0;$j<18;$j++){
$s.=",?";
}

$add="insert into bj ({$arr1}) values ({$s})";
$q=$stmt=$db->prepare($add);

//这个就是本地吧文本处理完毕再放上来,看着很难输入,中间也出错了很多次,

$array=array($_POST["name"],$_POST["nickname"],$_POST["gender"],$_POST["birthday"],$_POST["phone"],$_POST["qq"],$_POST["email"],$_POST["home"],$_POST["college"],$_POST["grade"],$_POST["major"],$_POST["company"],$_POST["position"],$_POST["address"],$_POST["weibolink"],$_POST["renrenlink"],$_POST["qqzonelink"],$_POST["photo"],$_POST["profile"]);
$stmt->execute($array);

?>

<!-- laohur@gmail.com方便联系 -->
3. [文件] test.php ~ 6KB
<!--
bj.html 用于显示和填写表单
bj.php用于处理表单
test.php 用于调试,中间处理. 以及说明

html.javascript.css因为各自表现不同而分开,各自负责内容,动态,美观.
mvc也一样,model负责数据处理,数据库,view负责展示,config负责把他们组织起来.

bj.html和bj.php分工也一样,一个输入表单,一个处理

环境:用xmpp或者lamp,adminer.phpmyadmin和mysql不好支持utf8,改为mariadb和adminer.
adminer可视化建表.用它导出来语句是

SET NAMES utf8;
SET foreign_key_checks = 0;
SET time_zone = '+00:00';
SET sql_mode = 'NO_AUTO_VALUE_ON_ZERO';

DROP TABLE IF EXISTS `bj`;
CREATE TABLE `bj` (
`id` int(4) NOT NULL AUTO_INCREMENT,
`name` varchar(10) COLLATE utf8_unicode_ci NOT NULL,
`nickname` varchar(20) COLLATE utf8_unicode_ci DEFAULT NULL,
`gender` varchar(4) COLLATE utf8_unicode_ci DEFAULT NULL,
`birthday` varchar(20) COLLATE utf8_unicode_ci DEFAULT NULL,
`phone` int(20) DEFAULT NULL,
`qq` int(20) DEFAULT NULL,
`email` varchar(30) COLLATE utf8_unicode_ci DEFAULT NULL,
`home` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
`college` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
`grade` varchar(10) COLLATE utf8_unicode_ci DEFAULT NULL,
`major` varchar(20) COLLATE utf8_unicode_ci DEFAULT NULL,
`company` varchar(30) COLLATE utf8_unicode_ci DEFAULT NULL,
`position` varchar(20) COLLATE utf8_unicode_ci DEFAULT NULL,
`address` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
`weibolink` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
`renrenlink` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
`qqzonelink` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
`photo` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
`profile` varchar(500) COLLATE utf8_unicode_ci DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

将上文粘贴到adminer,sql语句执行.排序用utf8_unicode_ci.mysql怎么弄也是乱码,phpmyadmin也是,只好换掉.

按照https://downloads.mariadb.org/mariadb/repositories/#distro=Debian&distro_release=wheezy&version=10.0&mirror=yamagata-university 把mysql换成mariadb,数据不变.
adminer更简单,也安全.下载adminer.php放上去就可以了.

sudo apt-get install python-software-properties sudo apt-key adv --recv-keys --keyserver keyserver.ubuntu.com 0xcbcb082a1bb943db sudo add-apt-repository 'deb http://ftp.yz.yamagata-u.ac.jp/pub/dbms/mariadb/repo/10.0/debian wheezy main' http://www.huiyi8.com/hunsha/lifu/​ Once the key is imported and the repository added you can install MariaDB with:
婚纱礼服欣赏
sudo apt-get update sudo apt-get install mariadb-server

在test.php中调试时,每一个变量前后都使用了

echo "<br>".'$arr:'."<br>";
var_dump($arr2);

类似的语句

还有一些字符串处理等中间工作也是在这里完成的.
中间处理在本地处理好之后把结果放上去就好了,不用上传过程.

这里每一句都花了不少的心血.

独立程序,编写时不会的上网查函数,不要抄袭已有成果.

-->

<?php

$arr1="name,nickname,gender,birthday,phone,qq,email,home,college,grade,major,company,position,address,weibolink,renrenlink,qqzonelink,profile";

$arr2=explode(",",$arr1);
echo "<br>".'$arr2'."<br>";

for($i=0;$i<count($arr2);$i++){
$arr3[$i]='"$_POST[\''.$arr2[$i].'\']"';
}
echo "<br>".'$arr3'."<br>";
echo $arr3;
$arr4=implode(",",$arr3);
//'$_POST['name']','$_POST['nickname']','$_POST['gender']','$_POST['birthday']','$_POST['phone']','$_POST['qq']','$_POST['email']','$_POST['home']',........
echo "<br>".'$arr4'."<br>";
echo $arr4;
$arr5="'".$arr4."'";
echo "<br>".'$arr5'."<br>";
echo $arr5;

$db=new PDO("mysql:host=localhost;dbname=test","root","");
//$r1=$db->exec("insert into bj(name,nickname,gender,birthday,phone,qq,email,home,college,grade,major,company,position,address,weibolink,renrenlink,qqzonelink,profile) values ($arr3)");
echo '$db:';

$insert="insert into bj({$arr1}) values ({$arr4})";
echo "<br>".'$insert:'."<br>";

$s="?";
for($j=0;$j<18;$j++){
$s.=",?";
}

$add="insert into bj ({$arr1}) values ($s)";
$stmt=$db->prepare($add);
$stmt->execute(array($arr4));

?>

$sql = " insert into `tbl` values(' " . $_POST['name'] . " ', ' " . $_POST['title'] . " ' )";
$sql = " insert into `tbl` values('{$_POST['name']}', '{$_POST['title']}')"

'INSERT INTO '.$table.' ('.$cols.') VALUES ('.$vals.')');

$stmt = $dbh->prepare("SELECT * FROM REGISTRY where name = ?");
if ($stmt->execute(array($_GET['name']))) {
while ($row = $stmt->fetch()) {
print_r($row);
}
}

$array=("{$_POST['name']}","{$_POST['nickname']}","{$_POST['gender']}","{$_POST['birthday']}","{$_POST['phone']}","{$_POST['qq']}","{$_POST['email']}","{$_POST['home']}","{$_POST['college']}","{$_POST['grade']}","{$_POST['major']}","{$_POST['company']}","{$_POST['position']}","{$_POST['address']}","{$_POST['weibolink']}","{$_POST['renrenlink']}","{$_POST['qqzonelink']}","{$_POST['profile']}");

"$_POST['name']","$_POST['nickname']","$_POST['gender']","$_POST['birthday']","$_POST['phone']","$_POST['qq']","$_POST['email']","$_POST['home']","$_POST['college']","$_POST['grade']","$_POST['major']","$_POST['company']","$_POST['position']","$_POST['address']","$_POST['weibolink']","$_POST['renrenlink']","$_POST['qqzonelink']","$_POST['profile']"
$arr5
'"$_POST['name']","$_POST['nickname']","$_POST['gender']","$_POST['birthday']","$_POST['phone']","$_POST['qq']","$_POST['email']","$_POST['home']","$_POST['college']","$_POST['grade']","$_POST['major']","$_POST['company']","$_POST['position']","$_POST['address']","$_POST['weibolink']","$_POST['renrenlink']","$_POST['qqzonelink']","$_POST['profile']"'$db:
$insert:

<!-- laohur@gmail.com谨防联系 -->
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: