...

View Full Version : Text file database editor



schris403
11-06-2007, 03:11 AM
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:


<?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

Fumigator
11-06-2007, 06:42 AM
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.

schris403
11-06-2007, 03:06 PM
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

aedrin
11-06-2007, 04:50 PM
Google for 'php mysql tutorial'.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum