title: PHP Form - Edit & Delete via Text File Db
body: <p>I pieced together the script below from various tutorials, examples, etc...</p>
  
  <p>Right now the script currently saves Id, Name, Url with a "|" delimiter to a text file Db like:</p>
  
  <pre><code>1|John|http://www.john.com|
  2|Mark|http://www.mark.com|
  3|Fred|http://www.fred.com|
  </code></pre>
  
  <p>But I'm having a hard time trying to make the "UPDATE" and "DELETE" buttons work.</p>
  
  <p>Can someone please post code which will:</p>
  
  <ul>
  <li>let me update/save any changed data for that row (for UPDATE button)</li>
  <li>let me delete that row (for DELETE button)</li>
  </ul>
  
  <p>PLEASE copy n paste the code below and try for yourself.
  I would like to keep the output format of the script below too.</p>
  
  <hr>
  
  <pre><code>&lt;?php
  
  $file = "data.txt";
  $name = $_POST['name'];
  $url = $_POST['url'];
  
  $data = file('data.txt');
    $i = 1;
      foreach ($data as $line) {
      $line = explode('|', $line);
    $i++;
  }
  
  if (isset($_POST['submits'])) {
   $fp = fopen($file, "a+");
     fwrite($fp, $i."|".$name."|".$url."|\n");
    fclose($fp);
  }
  
  
  ?&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;br&gt;
  &lt;form name="form1" action="thispage.php" method="POST"&gt;
  &lt;input type="text" name="name"&gt;
  &lt;input type="text" name="url"&gt;
  &lt;input type="submit" name="submits" value="ADD"&gt;&lt;br&gt;
  &lt;/form&gt;
  &lt;form name="form2" action="thispage.php" method="POST"&gt;
  &lt;?php
  
     $display = file("data.txt");
         for ($i=0; $i&lt;=count($display)-1; $i++) {
         $lines = explode("|",$display[$i]);
  
         print('&lt;input type="hidden" name="id" value="'.$lines[0].'"&gt;
                   &lt;input type="text" name="name" value="'.$lines[1].'"&gt;
                   &lt;input type="text" name="url" value="'.$lines[2].'"&gt;
                   &lt;input type="submit" name="update" value="UPDATE"&gt;
                   &lt;input type="submit" name="delete" value="DELETE"&gt;&lt;br&gt;');
         }
  
  ?&gt;
  &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>