Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Aug 2007
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Text file database editor

    I'm struggling with creating a form which will display the current data from a text file so I can modify it. Basically what I want is a form which will allow me to change,delete and create records that look like this in the file.txt file:
    1,yes,yes,no
    2,no,no,yes
    etc

    I have a form that will add them but now I need to update/change and delete records as well. It is:
    Code:
    <?php
    
    if(isset($_POST['add']))
    {
        $new_entry = $_POST['id'];
        $new_entry .= ',' . implode(',',$_POST['option']) . "\n";
    
        echo 'Adding: ' . $new_entry;
    
        // write new entry to file
        $handle = fopen('file.txt', 'a'); // open
        fwrite($handle, $new_entry);      // write
        fclose($handle);                  // close
    }
    
    
    ?>
    <form action="#" method="post">
    <table border="0" cellpadding="5" cellspacing="0">
      <tr>
        <th colspan="4">Add New</th>
      </tr>
      <tr>
        <th>ID</th>
        <th colspan="3">Options</th>
      </tr>
      <tr>
        <td><input type="text" name="id" size="10" /></td>
        <td><select name="option[]">
          <option value="yes">Yes</option>
          <option value="no">No</option>
        </select></td>
        <td><select name="option[]">
          <option value="yes">Yes</option>
          <option value="no">No</option>
        </select></td>
        <td><select name="option[]">
          <option value="yes">Yes</option>
          <option value="no">No</option>
        </select></td>
      </tr>
      <tr>
        <td colspan="4"><input type="submit" name="add" value="Add" /></td>
      </tr>
    </table>
    </form>

    Any help would be greatly appreciated.

    Thanks a lot!
    Chris

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Look into opening the file using the "a+" mode. That will open the file for reading and writing. You'll then need to read through the file until you get to the record to change; then do an fwrite() on that record.

    You may want to consider using a relational database such as MySQL instead-- it makes updating/deleting/inserting (not to mention the many other advantages) a lot easier.

  • #3
    New to the CF scene
    Join Date
    Aug 2007
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for your advise, I think that mysql should be the way to go with this as well but unfortunately I'm even more lost with that... Are there any examples you can point me to get me started?

    Thanks,
    Chris

  • #4
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    Google for 'php mysql tutorial'.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •