# Help: SIMPLE PHP FORM WITH NO MYSQL!

**URL:** https://forum.kirupa.com/t/help-simple-php-form-with-no-mysql/262199
**Category:** programming
**Created:** [June 4, 2008, 2:52am UTC](https://forum.kirupa.com/t/help-simple-php-form-with-no-mysql/262199 "2008-06-04T02:52:40Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![benghee](https://avatars.discourse-cdn.com/v4/letter/b/9dc877/32.png) [@benghee](https://forum.kirupa.com/u/benghee)
#### Post date: [June 4, 2008, 2:52am UTC](https://forum.kirupa.com/t/help-simple-php-form-with-no-mysql/262199/1 "2008-06-04T02:52:40Z")

</div>

Hi all,

I’m a newbie in php. Does anyone could guide me in making a very simple php form that have insert, edit, delete features WITHOUT using any Mysql database. All the output will read from a textfile. Thank you.

---

<div class="post-metadata">

### Author: ![Raydred](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/raydred/32/65_2.png) [@Raydred](https://forum.kirupa.com/u/Raydred)
#### Post date: [June 4, 2008, 3:34am UTC](https://forum.kirupa.com/t/help-simple-php-form-with-no-mysql/262199/2 "2008-06-04T03:34:21Z")

</div>

I found this article. I didnt read thru it 100% but it might point you in the right direction. It uses XML, which would be the best format for what you want to do.

[http://xtech06.usefulinc.com/schedule/paper/19](http://xtech06.usefulinc.com/schedule/paper/19)

---

<div class="post-metadata">

### Author: ![benghee](https://avatars.discourse-cdn.com/v4/letter/b/9dc877/32.png) [@benghee](https://forum.kirupa.com/u/benghee)
#### Post date: [June 4, 2008, 4:28am UTC](https://forum.kirupa.com/t/help-simple-php-form-with-no-mysql/262199/3 "2008-06-04T04:28:49Z")

</div>

[quote=Raydred;2336141]I found this article. I didnt read thru it 100% but it might point you in the right direction. It uses XML, which would be the best format for what you want to do.

[http://xtech06.usefulinc.com/schedule/paper/19[/quote]](http://xtech06.usefulinc.com/schedule/paper/19%5B/quote%5D)

Thanks, Raydred. But I need even more simple way to do it by not using XML. Just simply txt file. Something like file\_put\_contents, str\_replace()…that write data from a form into a txt file and display the data out of it.

---

<div class="post-metadata">

### Author: ![benghee](https://avatars.discourse-cdn.com/v4/letter/b/9dc877/32.png) [@benghee](https://forum.kirupa.com/u/benghee)
#### Post date: [June 5, 2008, 2:14am UTC](https://forum.kirupa.com/t/help-simple-php-form-with-no-mysql/262199/4 "2008-06-05T02:14:27Z")

</div>

I found a method is called flat file database. I’m now made the listing and create new page which running smooth but I’m stuck in delete and edit.php. Does anyone can take a look at my code below. Thanks. 🙂

$selName = $\_GET[‘Name’];  
$delAdd = $\_GET[‘Add’];  
$file = “reseller” . $delAdd . $selName .".txt";  
$news = file($file);  
$cnt = 0;  
foreach ($news as $key =\> $line) {  
if ($key!= $selName) { $result[] = $line; }  
$cnt+1;  
}  
if ($key!= 0) {  
$fh = fopen($file, “w+”);  
foreach ($result as $joint) {  
fwrite($fh, $joint);  
}  
fclose($fh);  
}  
elseif ($key == 0) {  
$fh = fopen($file, “w+”);  
fwrite($fh, “”);  
fclose($fh);  
}

---

<div class="post-metadata">

### Author: ![k77](https://avatars.discourse-cdn.com/v4/letter/k/c67d28/32.png) [@k77](https://forum.kirupa.com/u/k77)
#### Post date: [June 5, 2008, 4:57pm UTC](https://forum.kirupa.com/t/help-simple-php-form-with-no-mysql/262199/5 "2008-06-05T16:57:48Z")

</div>

What errors?

I understand that you want the .txt in the root where the file is located? Add.Name.txt

Also you could try adding it to another directory, meaning chmod to 0755

---

<div class="post-metadata">

### Author: ![benghee](https://avatars.discourse-cdn.com/v4/letter/b/9dc877/32.png) [@benghee](https://forum.kirupa.com/u/benghee)
#### Post date: [June 9, 2008, 8:37am UTC](https://forum.kirupa.com/t/help-simple-php-form-with-no-mysql/262199/6 "2008-06-09T08:37:41Z")

</div>

My problem is everytime i edit the form data, it wont overwrite the existing one but it will append to a new next line. Does anyone here can guide me tru this process? Thanks for many help. Here is my code so far:

[FONT=verdana][SIZE=2][COLOR=#000000]$any = “@,@”; [/COLOR][/SIZE][/FONT]  
[SIZE=2][FONT=verdana][COLOR=#000000]$filename=“mydata.txt”; //sets file to edit [/COLOR][/FONT][/SIZE]  
[SIZE=2][FONT=verdana][COLOR=#000000]$handle = fopen($filename, ‘r+’); //File handle for $filename [/COLOR][/FONT][/SIZE]  
[FONT=verdana][SIZE=2][COLOR=#000000]if(isset($\_POST[‘Submit’])) { [/COLOR][/SIZE][/FONT]  
[SIZE=2][FONT=verdana][COLOR=#000000]{ [/COLOR][/FONT][/SIZE]  
[SIZE=2][FONT=verdana][COLOR=#000000]while(1) { [/COLOR][/FONT][/SIZE]  
[SIZE=2][FONT=verdana][COLOR=#000000]$line = fgets($handle); //read line from file content [/COLOR][/FONT][/SIZE]  
[SIZE=2][FONT=verdana][COLOR=#000000]if($line == null)break; //if end of file reached then stop reading [/COLOR][/FONT][/SIZE]  
[SIZE=2][FONT=verdana][COLOR=#000000]anymore [/COLOR][/FONT][/SIZE]  
[SIZE=2][FONT=verdana][COLOR=#000000]$newcontents = trim(addslashes($\_POST[‘Name’])).$any.addslashes($\_POST[‘Address’]).$any.addslashes($\_POST[‘Area’])."  
"; [/COLOR][/FONT][/SIZE]  
[SIZE=2][FONT=verdana][COLOR=#000000]$str.= $line; //set file content to a string [/COLOR][/FONT][/SIZE]  
[SIZE=2][FONT=verdana][COLOR=#000000]} [/COLOR][/FONT][/SIZE]  
[SIZE=2][FONT=verdana][COLOR=#000000]} [/COLOR][/FONT][/SIZE]

[SIZE=2][FONT=verdana][COLOR=#000000]$str.= $newcontents; //append new updated contents to file content [/COLOR][/FONT][/SIZE]  
[SIZE=2][FONT=verdana][COLOR=#000000]rewind($handle); //set pointer back to beginning. [/COLOR][/FONT][/SIZE]  
[SIZE=2][FONT=verdana][COLOR=#000000]ftruncate($handle, filesize($filename)); //delete everything in the file. [/COLOR][/FONT][/SIZE]  
[SIZE=2][FONT=verdana][COLOR=#000000]fwrite($handle, $str);//write everything back to file with the updated contents. [/COLOR][/FONT][/SIZE]  
[SIZE=2][FONT=verdana][COLOR=#000000]echo ‘\<script\>alert(“Reseller Updated !”);opener.location.reload();\</script\>’; [/COLOR][/FONT][/SIZE]  
[SIZE=2][FONT=verdana][COLOR=#000000]echo ‘\<script\>window.close();\</script\>’; exit(); [/COLOR][/FONT][/SIZE]  
[SIZE=2][FONT=verdana][COLOR=#000000]fclose($handle); [/COLOR][/FONT][/SIZE][FONT=verdana][SIZE=2][COLOR=#000000]} [/COLOR][/SIZE][/FONT]

---

<div class="post-metadata">

### Author: ![borrob](https://avatars.discourse-cdn.com/v4/letter/b/a87d85/32.png) [@borrob](https://forum.kirupa.com/u/borrob)
#### Post date: [June 9, 2008, 10:20am UTC](https://forum.kirupa.com/t/help-simple-php-form-with-no-mysql/262199/7 "2008-06-09T10:20:52Z")

</div>

had to rewrite a lot to get it in an acceptable state. I didn’t test it but it should work…

```auto

$any = "@,@"; 
if(isset($_POST['Submit'])) 
{ 
    $filename="mydata.txt"; //sets file to edit 
    $handle = fopen($filename, 'r+'); //File handle for $filename 
    $newcontent = trim( addslashes($_POST['Name'])) . $any.addslashes($_POST['Address']) . $any.addslashes($_POST['Area']) . "
"; 
    $current_content = '';
    while( !feof( $handle ) ) 
    { 
        $line = fgets( $handle );
        // if eof ( end of file then the last line shouldn't be written back!
        if( !feof( $handle ) )
        {
            $current_content .= $line; //set file content to a string 
        }
    } 
    fclose( $handle );

    $current_content .= $newcontent; //append new updated contents to file content 
    $handle = fopen($filename, 'w+'); //File handle for $filename writing 
    fwrite($handle, $current_content); //write everything back to file with the updated contents. 
    fclose($handle); 
    echo '<script>alert("Reseller Updated !"); opener.location.reload(); </script>'; 
    echo '<script>window.close();</script>'; 
}

```

---

<div class="post-metadata">

### Author: ![benghee](https://avatars.discourse-cdn.com/v4/letter/b/9dc877/32.png) [@benghee](https://forum.kirupa.com/u/benghee)
#### Post date: [June 10, 2008, 2:58am UTC](https://forum.kirupa.com/t/help-simple-php-form-with-no-mysql/262199/8 "2008-06-10T02:58:27Z")

</div>

Oh, I’m sorry Borrob…i do a complete test run now, and found out the previous problem is still exsist tat the data is append to a new entry instead of editing the existing one. So after i edit the form content and click ‘submit’, it will append a new line. May i know what causing this? thanks for look again for me.

---

<div class="post-metadata">

### Author: ![borrob](https://avatars.discourse-cdn.com/v4/letter/b/a87d85/32.png) [@borrob](https://forum.kirupa.com/u/borrob)
#### Post date: [June 10, 2008, 1:43pm UTC](https://forum.kirupa.com/t/help-simple-php-form-with-no-mysql/262199/9 "2008-06-10T13:43:51Z")

</div>

The reason it is still appending the last line is because you have an additional “  
” in your data file.  
So when it takes the last line away it takes an empty line away.  
You can change this by not appending the “  
” to the new content…  
Or if the last line is empty take the line before that away.

---

<div class="post-metadata">

### Author: ![benghee](https://avatars.discourse-cdn.com/v4/letter/b/9dc877/32.png) [@benghee](https://forum.kirupa.com/u/benghee)
#### Post date: [June 11, 2008, 7:03am UTC](https://forum.kirupa.com/t/help-simple-php-form-with-no-mysql/262199/10 "2008-06-11T07:03:40Z")

</div>

[quote=borrob;2340024]The reason it is still appending the last line is because you have an additional “  
” in your data file.  
So when it takes the last line away it takes an empty line away.  
You can change this by not appending the “  
” to the new content…  
Or if the last line is empty take the line before that away.[/quote]

Thanks, borrob! It works perfectly. All the data is editable and no extra line append in database. Thank you once again for helping me out 😃 😃
