分类:数据库 发布时间:2017-05-30 10:39:44 阅读: 作者:郑祥景
<?php $filename = 'DATA.TXT'; //文档 if(!empty($_POST['name']) && !empty($_POST['nianling'])){ $file = fopen($filename, "a+"); $name = $_POST['name']; $nianling = $_POST['nianling']; $arr = array(); while(!feof($file)){ //把文件按行读出 $hang = fgets($file); $arr[] = explode("|", $hang);//分割每行数据 } for ($i=0;$i<count($arr);$i++){ if($arr[$i][0] ==$_POST['name']){ //进行比较是否重复 $chongfu = 1; break; } } if($chongfu != 1){ $txt = "$name|$nianling \n"; //写入txt文本的语句,用'|'分隔; fwrite($file, $txt); //写入 fclose($filename); //关闭文档 echo "成功插入<br><br>"; }else{ echo '名字重复啦!<br><br>'; } }else { echo '请填写完整<br><br>'; fclose($filename); } //读取列表开始 $shuchu = file($filename); foreach ($shuchu as $row){ echo $row."<br><br>"; } //读取列表结束 ?> <html> <body> <form action='' method="post"> 您的姓名:<input type="text" name="name"><br><br> 您的年龄:<input type="text" name="nianling"><br><br> <input type="submit"> </form> </body> </html>
编辑:郑祥景