title: Fetch MYSQL row by id and edit and update in form?
body: <p>I have a MYSQL table with edit link and delete button on each row. The edit link goes to edit_movie.php which has a form. My problem is that the form is not showing any data to be updated/edited. How do I fetch the id from the row clicked on or what function to write to pick it up. The form is just blank, no fields are filled out.</p>
  
  <p>Could someone help out I´d be very happy! Been sitting with this problem for 3 days now and just can´t get it to work. I'm a PHP beginner...</p>
  
  <p>I know I'm using mysql functions which are outdated and I need to lookover SQL injection which I will once I get this working.</p>
  
  <p><strong>Table info:</strong> </p>
  
  <p>Table 1 name: Movies<br>
  Fields: id (primary key, AI), title, release_year, genre_id, director </p>
  
  <p>Table 2 name: Categories<br>
  Fields: genre_id (primary key, AI), genre  </p>
  
  <p>They have a foreign relation between genre_id.</p>
  
  <p><strong>Here is the code in index.php file</strong></p>
  
  <pre><code>    &lt;?php
  
  require('movie.inc.php');
  
  if ( isset($_GET['delete']) &amp;&amp; isset($_GET['id']) ){
     if ( delete_movie_by_id($_GET['id']) ){ //it's 100% safe
         die('Movie has been removed. Refresh the page now'); // or the like
     } else {
        echo 'Sorry movie could not be deleted'; // could not - handle here
     }
  }
      include 'add_movie.php';
  ?&gt;
  &lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
  
  &lt;title&gt;My movie library&lt;/title&gt;
  &lt;meta charset="utf-8" /&gt;
  &lt;link rel="stylesheet" href="mall.css" /&gt;
  
  &lt;/head&gt;
  &lt;body&gt;
  
  &lt;table&gt;
   &lt;tr&gt;
    &lt;th&gt;Title&lt;/th&gt;
    &lt;th&gt;Release year&lt;/th&gt;
    &lt;th&gt;Genre&lt;/th&gt;&lt;th&gt;Director&lt;/th&gt;
    &lt;th&gt;Update&lt;/th&gt;
    &lt;th&gt;Delete&lt;/th&gt;
   &lt;/tr&gt;
  
    &lt;?php  foreach (get_all_movies() as $index =&gt; $row) : ?&gt;
     &lt;tr&gt;
       &lt;td&gt;&lt;?php echo $row['title'];?&gt;&lt;/td&gt;
       &lt;td&gt;&lt;?php echo $row['release_year']; ?&gt;&lt;/td&gt;
       &lt;td&gt;&lt;?php echo $row['genre'];?&gt;&lt;/td&gt;
       &lt;td&gt;&lt;?php echo $row['director'];?&gt;&lt;/td&gt;
       &lt;td&gt;&lt;a href='&lt;?php printf('edit_movie.php?edit=%s', $row['id']);?&gt;'&gt;Edit&lt;/a&gt;&lt;/td&gt;
       &lt;td&gt;
        &lt;form action="index.php" method="GET"&gt;
                &lt;input type="hidden" name="delete" value="yes" /&gt;
                &lt;input type="hidden" name="id" value="&lt;?php echo $row['id'];?&gt;" /&gt; 
                &lt;input type="submit" value="Delete" /&gt;
        &lt;/form&gt;
        &lt;/td&gt;
      &lt;/tr&gt;
      &lt;?php endforeach; ?&gt;
      &lt;/table&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p><strong>Here is the edit_movie.php code:</strong></p>
  
  <pre><code> &lt;?php
  
      require 'connect.inc.php';
      require_once('movie.inc.php');
  
      ?&gt;
  
      &lt;!DOCTYPE html&gt;
  
      &lt;html&gt;
  
      &lt;head&gt;
  
      &lt;title&gt;My movie library&lt;/title&gt;
      &lt;meta charset="utf-8" /&gt;
      &lt;link rel="stylesheet" href="mall.css" /&gt;
  
      &lt;/head&gt;
  
      &lt;body&gt;
  
      &lt;h1&gt;Edit movie&lt;/h1&gt;
      &lt;div id="form_column"&gt;
      &lt;form action="edit_movie.php" method="post"&gt;
      &lt;input type="hidden" name="id" value="&lt;?php if (isset($row["id"])) ?&gt;" /&gt; &lt;br&gt;
      Title:&lt;br&gt; &lt;input type="text" name="title" value="&lt;?php if (isset($row["title"])) { echo $row["title"];} ?&gt;" /&gt; &lt;br&gt;
      Release Year:&lt;br&gt; &lt;input type="text" name="release_year" value="&lt;?php if (isset($row["release_year"])) { echo $row["release_year"];} ?&gt;" /&gt; &lt;br&gt;
      Director:&lt;br&gt; &lt;input type="text" name="director" value="&lt;?php if (isset($row["director"])) { echo $row["director"];} ?&gt;" /&gt; &lt;br&gt;&lt;br&gt;
      Select genre:
      &lt;br&gt;
      &lt;br&gt; &lt;input type="radio" name="genre_id" value="1" checked /&gt;Action&lt;br&gt;
      &lt;br&gt; &lt;input type="radio" name="genre_id" value="2" /&gt;Comedy&lt;br&gt;
      &lt;br&gt; &lt;input type="radio" name="genre_id" value="3" /&gt;Drama&lt;br&gt;
      &lt;br&gt; &lt;input type="radio" name="genre_id" value="4" /&gt;Horror&lt;br&gt;
      &lt;br&gt; &lt;input type="radio" name="genre_id" value="5" /&gt;Romance&lt;br&gt;
      &lt;br&gt; &lt;input type="radio" name="genre_id" value="6" /&gt;Thriller&lt;br&gt;&lt;br&gt;
      &lt;input type="submit" value="Update movie" /&gt;
      &lt;/form&gt;
      &lt;/div&gt;
  
  
  
      &lt;/body&gt;
  
      &lt;/html&gt;
  </code></pre>
  
  <p><strong>And here is the movie.inc.php file</strong></p>
  
  <pre><code>&lt;?php
  
  require_once('connect.inc.php');
  
  
  function get_all_movies(){
  
     $query = "SELECT * FROM movies m INNER JOIN categories c ON m.genre_id = c.genre_id";
  
     $result = mysql_query($query);
  
     if ( ! $result ){ 
       return false;
     } else {
       $return = array();
  
       while ($row = mysql_fetch_assoc($result)){
  
          $return[] = array('director' =&gt; $row['director'], 'genre' =&gt; $row['genre'], 'release_year' =&gt; $row['release_year'], 'title' =&gt; $row['title'], 'id' =&gt; $row['id']); 
       }
         return $return;
     }
  }
  
  
  function delete_movie_by_id($id){
     return mysql_unbuffered_query(sprintf("DELETE FROM `movies` WHERE id='%s' LIMIT 1", mysql_real_escape_string($id)));
  }
  
  if ( isset($_POST['delete'], $_POST['id']) ){
  
     delete_movie_by_id($_POST['id']); 
  }
  
  
  ?&gt;
  </code></pre>