大家好,我是 php 新手,我尝试在 php 中处理文件,即将信息存储到不在数据库中的文本文件中。这基本上是将数据插入文本文件并删除文件中的数据,但我是努力删除文本文件中的最后一条记录是我的代码,任何人都非常感谢..
<html>
<head>
<title>
Registration
</title>
<style type="text/css">
table{font-family:calibri;color:black;font-size:11pt,font-style:normal;background-color:white;border-collapse:collapse;border:2px solid navy}
</style>
<script type="text/javascript" src="frnt.js">
</script>
</head>
<body bgcolor="aqua">
<form name="myform" action="writ.php" method="post" onsubmit="return validation()">
<table border="0px"align="center" cellpadding="5" cellspacing="5">
<tr>
<th colspan="2" align="center">Registration</th>
</tr>
<tr>
<td>UserName:</td>
<td><input type="text" name="nme" maxlength='20'>
</td>
</tr>
<tr>
<td>Email:</td>
<td><input type="text" name="mail" maxlength='20'>
</td>
</tr>
<tr>
<td>Mobile:</td>
<td><input type="text" name="mobile" maxlength='10'>
</td>
</tr>
<tr>
<td>Education:</td>
<td><input type="text" name="edu" maxlength='10'>
</td>
</tr>
<tr>
<td>College:</td>
<td><input type="text" name="clg" maxlength='15' >
</td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="Submit" name="submit"><input type="reset" value="Reset">
</td>
</tr>
</table>
</form>
</body>
</html>
这个页面重定向到 writ.php,这个代码是
<html>
<body>
<form method = "post">
<?php
$body = $_POST['nme'];
$body1 = $_POST['mail'];
$body2 = $_POST['mobile'];
$body3 = $_POST['edu'];
$body4 = $_POST['clg'];
$file_name = "test.txt";
$fp = fopen ($file_name, "a");
fwrite ($fp,"Name:".$body."\r");
fwrite ($fp,"Email:".$body1."\r");
fwrite ($fp,"Mobile:".$body2."\r");
fwrite ($fp,"Education:".$body3."\r");
fwrite ($fp,"College:".$body4."\r\r");
if(fwrite) {
echo "your data added successfully <br><br>";
echo "<a href='back .php'>Go back to view your page</a>";
}
?>
</form>
</body>
</html>
这是删除文本文件中的整个数据
<?php
$v=filesize("test.txt");
echo "<br />";
$file = fopen("test.txt", "a+");
$d=ftruncate($file,0) or die ('could not open');
if($d) {
echo "deleted the whole data's successfully<br><br>";
echo"<a href='frm.php'>Add data to a file </a><br><br>";
echo"<a href='test.txt'>View the file </a><br><br>";
}
fclose($file);
?>
最后我有这个编码的问题
<?php
$file="test.txt";
$str = fopen("test.txt","r");
$s = fgets($str);
$parts = explode("\r",$s);
$content = file_get_contents($file);
$c=count($parts);
$dd=$c-8;
for($i=$c;$i>$dd;$i--) {
$content = str_replace($parts[$i],'', $content);
/*$content = str_replace($parts[7],'', $content);
$content = str_replace($parts[8],'', $content);
$content = str_replace($parts[9],'', $content);
$content = str_replace($parts[10],'', $content);
*/
$fh = fopen($file, 'w');
$d=fwrite($fh, $content);
}
//unset($parts[$i],'');
if($d) {
echo"Last data deleted successfully <br><br>";
}
?>
在上面的编码中,当我在文本文件中多次输入相同的电子邮件 ID 时,我面临的问题是,但是当我要删除文件中的最后一条记录时,同名的电子邮件也被删除了!!!!以及如何解决这个问题。请帮忙!非常感激:)