起源于要整理通讯录,原先用的是文件调查,现在学了php,就自己试一下. 程序短小精悍,适于学习. 有两个文件,bj.html用于显示和采集表单信息.bj.php用于处理数据和反馈结果. 突出之处在于可以上传阅览头像. 其实还有一个test.php,用于调试,中间处理.从这个开始先看. 注释很详细,标签,函数不会的话右键搜索. 因为web目录很多,放在./1/文件夹下,在火狐设一个书签方便访问 部署时吧bj.html改成index.php
原先想着搭建一个ipv6免费上网的应用平台(现在知道了是oauth开放平台),就开始学php,php是最简单的了,可也学了一年才有第一个程序,慎入!
2014年通讯录
姓名
小名
性别
-
生日
电话
-
qq
email
-
老家
学校
年级
专业
公司
职务
地址
微博主页
人人主页
qq空间主页
-
-
头像
-
简介
复制代码
//保存图片文件 @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 " hello, ".$_POST["name"]."!
";//返回成功信息,而且是自己的头像
}
$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);
?>
复制代码
$arr1="name,nickname,gender,birthday,phone,qq,email,home,college,grade,major,company,position,address,weibolink,renrenlink,qqzonelink,profile";
$arr2=explode(",",$arr1); echo " ".'$arr2'." ";
for($i=0;$i $arr3[$i]='"$_POST[\''.$arr2[$i].'\']"'; } echo " ".'$arr3'." "; echo $arr3; $arr4=implode(",",$arr3); //'$_POST['name']','$_POST['nickname']','$_POST['gender']','$_POST['birthday']','$_POST['phone']','$_POST['qq']','$_POST['email']','$_POST['home']',........ echo " ".'$arr4'." "; echo $arr4; $arr5="'".$arr4."'"; echo " ".'$arr5'." "; 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 " ".'$insert:'." ";
$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:
复制代码
|