Jump to content

Deleting data from a file


spillage

Recommended Posts

You will have to forgive me because all this is very new to me.

 

I have a .txt file recieving data from a form page and another page where I can view the data in a table. What I am trying to do is create a drop down menu to delete or save the data and a submit button on the table page. If delete is submited then I need to data deleted but I'm using unlink and this deletes the entire file and then brings up error because the is no file to pull in when the page is refreshed. If there is no drop down menu and no data on the file then "no data a present" is shown.

 

Thinking of using  if (file_exists("file name")) echo '$a' else echo 'no data' . But $a would need to be enire code for page and I cannot work out how this would work.

 

Sorry if this makes no sense.

 

Thanks Mark

Link to comment
https://forums.phpfreaks.com/topic/100477-deleting-data-from-a-file/
Share on other sites

Many thanks rhodesa

 

could anyone just check my code as all that happens now when the page is opened is all the data is deleted. Think I need a good couple of drinks as driving me mad. Not even sure I need to $_POST. Was fine using options that post to another page but now the option box is on the same page I seem to have lost the plot and cant get my head around it.

 

Many thanks

 

Mark

 

 

<tr>

<td><h3> Would you like to keep orders ?</h3></td>

  <td><select name="delete">

      <option value = "a"> Yes</option>

        <option value = "b">No</option>

     

      </select>

  </td>

 

</tr>

 

 

<tr bgcolor="#4169e1">

  <td colspan="2" align="center"><input  type="submit" value="Submit"></td>

</tr>

</form>

 

 

 

</html>

<?php

 

$delete= $_POST["delete"];

$removefile = fclose(fopen("$DOOCUMENT_ROOT/ordering.txt",'w'));

  if ($delete == "b")

    {

            echo $removefile;

    }

 

That will always remove the file. Move the fopen/fclose inside the IF statement:

 

<?php
$delete= $_POST["delete"];
   if ($delete == "b")
     {
       $removefile = fclose(fopen("$DOOCUMENT_ROOT/ordering.txt",'w'));
       print 'Emptied';
     }
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.