Hi all am new to the php ,I try out file handling in php , i.e., storing the information to the text files not in the database .which is basically insert the data to the text files and deleting the data in a file but i am struggling to delete the last records in the text file here is my code anyone much appreciated..
<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>
And this page redirects to the writ.php and this code is to be
<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>
And this is deleting whole data’s in the text file
<?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);
?>
Finally i have an problem with the this coding
<?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>";
}
?>
In the above coding i face the problem has when i enter the same email id to the multiple times its store in the text file but when i going to delete the last records in the file the same name of email also deleted !!!! And how to solve this .
Please help! Much appreciated:)
if you change
fopen("test.txt", "a")tofopen("test.text", "w")it will delete everything that is inside the txt.“w” means: Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it. (According to PHP.net fopen)