Deleting a text line from a .txt file.

Hi,

I just entered into php before a few months. And haven’t got into MySQL till now. I wanted to have good practice on php and it’s file writing and reading before i get into MySQL…

Well, Until a few days every thing was correct. I made some small software also based on php and HTML. Such as some scripts, Emailing, Contacting us and shoutbox etc…

Well, Not I need help. Here I want to delete a line from a text file… THE code will say more clearly…

This is a simple project. In which I have used 3 files… test.php, testdb.txt(FOR DATABASE) and admin.php

Here are the codes, once you have red the code. I would able to describe what I want exactly,

code of test.php

<?php

$db = "testdb.txt";

$form = "<form method="post" action="test.php">
Entry Number ; <input name="entry" type="text" size="20">
<input name="submit" type="submit" value="submit">
</form>";

$entry_num = $_POST["entry"];
$entry_to_write = "<br>It's entry number $entry_num.";

if($_POST["submit"] == submit)
{

$f_handle = fopen($db, 'a');
fwrite($f_handle,$entry_num."n");
fclose($f_handle);

echo "<b>Entry Entered into system</b>";
$file = file("testdb.txt");

array_reverse($file);
foreach($file as $line)
echo "$line<br>";
}
else
{
echo $form;
}
?>

code of admin.php

<?php

$db = "testdb.txt";
$del_form = "<form method="post" action="admin.php">
Line to delete >>> <input name"del" type="text" size="10">
<input name="del_now" type="submit" value="del_now">
</form>";

$login_form = "<form method="post" action="admin.php">
Line to delete >>> <input name"pass" type="text" size="10">
<input name="del_now" type="submit" value="pass">
</form>";
if($_POST["pass"] == 123)
{
echo $del_form;

$file = file("testdb.txt");
$line_to_del = $_POST["del_now"];

unset($file[$line_to_del-1]);

array_reverse($file);
foreach($file as $line);
echo $line;
}
else
{
echo $login_form;
}
?>

Now, the problem is, Almost the file writing and reading in reverse order is perfect… But the admin.php is making problem while i want to delete a line from testdb.txt

for example, if 10 people have inserted the entry… so each entry is set up as a line… So when i want to delete entry four… I would delete line four… But I am not able to delete any line…

Please help me…

Thanks…

FAISAL!

Why are you using array_reverse()? Does it serve a certain purpose? Your foreach() statement ends in a semi-colon, not an opening curly brace. You’re not writing the new value of $file back to the file that you retrieved its original content from.

Well, I use array reverse to read the file in reverse order…

And by the way that was the mistake…

<?php
$db = "testdb.txt";
$del_form = "<form method="post" action="admin.php">
Line to delete >>> <input name"del" type="text" size="10">
<input name="del_now" type="submit" value="del_now">
</form>";

$login_form = "<form method="post" action="admin.php">
Line to delete >>> <input name"pass" type="text" size="10">
<input name="del_now" type="submit" value="pass">
</form>";
if($_POST["pass"] == 123)
{
echo $del_form;

$file = file("testdb.txt");
$line_to_del = $_POST["del_now"];

unset($file[$line_to_del-1]);

array_reverse($file);
foreach($file as $line)
echo $line;
}
else
{
echo $login_form;
}
?></blockquote>

And the foreach didn’t stops with semicolon… The semicolon is Ending to end the if statement…

Please help…

Okay, I see what the problem is now: your logic is flawed: first you enter a password, allowing the loop to enter the if statement, but no line to delete. You’ll get the form to delete lines though. Then you submit that form, but no password is passed, so the if statement is skipped and the else statement is triggered, throwing you back into the login form. Correct?

Sponsor our Newsletter | Privacy Policy | Terms of Service