You are viewing our Forum Archives. To view or take place in current topics click here.
[PHP] Need Help With A Delete Button
Posted:

[PHP] Need Help With A Delete ButtonPosted:

-ScHmIdTy
  • Powerhouse
Status: Offline
Joined: Mar 10, 201113Year Member
Posts: 470
Reputation Power: 18
Status: Offline
Joined: Mar 10, 201113Year Member
Posts: 470
Reputation Power: 18
Hello,
So today I have made this form which edits a text file. this form contains a submit box for banning, which puts text into a text file. and a submit box for unbanning, which is what i need help on. how would I delete text from a text file? Any help is appreciated

Heres my code:
<!-- coded by ScHmIdTy, for jonnyli -->
<?php
$fn = "test.txt";
$file = fopen($fn, "a+");
$size = filesize($fn);

if($_POST['addition']) fwrite($file,", " . $_POST['addition']);



$text = fread($file, $size);
fclose($file);
?>
<form action="<?=$PHP_SELF?>" method="post">
<textarea><?=$text?></textarea><br/>
<input type="text" value="Ban" id="Ban" name="addition" onclick="document.getElementById('Ban').value='';return false;"/>
<input type="submit"/>
<input name="text" value="Un-Ban" id="Un-Ban" name="clear"  onclick="document.getElementById('Un-Ban').value='';return false;"/>
<input type="submit"/>
</form>


Thanks,
ScHmIdTy
#2. Posted:
Zion
  • TTG Addict
Status: Offline
Joined: Jul 03, 201014Year Member
Posts: 2,120
Reputation Power: 95
Status: Offline
Joined: Jul 03, 201014Year Member
Posts: 2,120
Reputation Power: 95
Have you ever looked into creating a MySQL database, and not using a text file? In almost every situation that would be a much better idea. Then, to delete it, you'd just do a mysql delete query and delete the entry.
#3. Posted:
Nic
  • Retired Staff
Status: Offline
Joined: Jun 08, 201014Year Member
Posts: 2,466
Reputation Power: 1070
Motto: I've been watching you all day.
Motto: I've been watching you all day.
Status: Offline
Joined: Jun 08, 201014Year Member
Posts: 2,466
Reputation Power: 1070
Motto: I've been watching you all day.
As suggested above, using a MySQL database would be a better option. It will be easier to retrieve, insert, update and delete information. Especially when files are starting to get big.

Anyway, on your current situation, you could retrieve the file, use the str_replace() function to look for a pattern and replace it with something else (or nothing, thus removing it).

Setting up a MySQL database is far from difficult. Same goes for using it in your script. With Object Oriented PHP you can easily prepare and execute queries protected against sql-injections. If you need help, let me know.
Jump to:
You are viewing our Forum Archives. To view or take place in current topics click here.