Jump to content

Write, Edit and Delete in a Text file


Sanjib Sinha

Recommended Posts

I want to add text to a file, getting it printed out in index.php and later want to edit/delete per row. I give codes page wise. first the form to take data:

add.php:

<?php
?>
<form method="post" action="add_data.php">
<fieldset>
<legend>Student List</legend>
Name : <input type="text" name="uname" /><br>
Content : <input type="text" name="age" />
<br>
<input type="submit" name="submit" value="send" />
</fieldset>
</form>

 

Next adding text to a file: add_data.php:

<?php
$id = 1;
if (file_exists("data.txt"))
{
$fp = fopen("data.txt", 'r');
$str = fread($fp, filesize("data.txt"));
$str_arr = explode("|", $str);
foreach ($str_arr as $rec)
{
	if ($rec)
	{
		$id++;
	}
}
}

$mode = (file_exists("data.txt"))? "a" : "w";

$fp = fopen("data.txt", $mode);
$line = $id . "--" . $_POST['uname'] . "--" . $_POST['age'] . "|";
$res = fwrite($fp, $line);
fclose($fp);

header("location:index.php");


?>

In the index.php, I'd like to fetch data and edit them per row:

<?php
$fp = fopen("data.txt", 'r');
$str = fread($fp, filesize("data.txt"));
$str_arr = explode("|", $str);
echo "<table border='1'>";
echo "<tr><td>ID</td><td>Name</td><td>Age</td><td>Option</td></tr>";

foreach ($str_arr as $rec)
{
	if ($rec)
	{
		$rec_arr = explode("--", $rec);
		echo "<tr>";
		foreach ($rec_arr as $col)
		{
			echo "<td>$col</td>";
		}
		print "<td><a href=\"edit.php?id=$id\">Edit</a>/<a href=\"delete.php?id=$id\">Delete</a></td></tr>";
	}
}
echo "</table>";

echo "<a href=\"add.php\">Add Data</a>";

?>

 

Next I want to Edit per row: edit.php:

<?php

$fp = fopen("data.txt", 'r');
$str = fread($fp, filesize("data.txt"));
$str_arr = explode("|", $str);

foreach ($str_arr as $rec)
{
if ($rec)
{
	$rec_arr = explode("--", $rec);		
	$id = $rec_arr[0];
	$name = $rec_arr[1];
	$age = $rec_arr[2];
	if ($id == $_REQUEST["id"])
	{
		echo "<form action='edit_data.php' method='post'>";
		echo "Name :<input type='text' name='uname' value='$name' /><br>";
		echo "Age: <input type='text' name='age' value=$age /><br>";
		echo "<input type='hidden' name='uid' value=$id />";
		echo "<input type='submit' value='Edit'>";
		echo "</form>";
	}		
}
}


?>

 

From edit.php to edit_data.php where I practically try to edit per row:

<?php

$fp = fopen("data.txt", 'r');
$str = fread($fp, filesize("data.txt"));
$str_arr = explode("|", $str);

foreach ($str_arr as $rec)
{
if ($rec)
{
	$rec_arr = explode("--", $rec);
	$id = $rec_arr[0];
	$name = $rec_arr[1];
	$age = $rec_arr[2];

	if ($id == $_POST['uid'])
	{
		$new_str = $id . "--" . $_POST['uname'] . "--" . $_POST['age'] . "|";
	}
	else 
	{
		$new_str = $id . "--" . $name . "--" . $age . "|"; 
	}
}
}

fclose($fp);

$fp = fopen("data.txt", 'w');
fwrite($fp, $new_str);
fclose($fp);


?>

 

The mechanism is simple. Taking datas from a form, explode them to an array and get them into col/row pattern to edit and delete them. But my problem is when I want to edit in index.php page, specially in this part:

print "<td><a href=\"edit.php?id=$id\">Edit</a>

It says, undefined index. But I try to catch this $id in the edit.php page, in this manner, as you see in my code:

foreach ($str_arr as $rec)
{
if ($rec)
{
	$rec_arr = explode("--", $rec);		
	$id = $rec_arr[0];
	$name = $rec_arr[1];
	$age = $rec_arr[2];
	if ($id == $_REQUEST["id"])
	{
		echo "<form action='edit_data.php' method='post'>";
		echo "Name :<input type='text' name='uname' value='$name' /><br>";
		echo "Age: <input type='text' name='age' value=$age /><br>";
		echo "<input type='hidden' name='uid' value=$id />";
		echo "<input type='submit' value='Edit'>";
		echo "</form>";
	}		
}
}

 

Have I done any mistake here? If anyone points out, I'll be obliged.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.