title: Editing data from MySQL via PHP
body: <p>I am running into a frustrating problem with a PHP script that's supposed to allow me to edit individual rows within my MySQL database.</p>
  
  <p>This is the file where all of the rows from the database are displayed; it works just like it's supposed to. </p>
  
  <pre><code>&lt;table cellpadding="10"&gt;
  &lt;tr&gt;
  &lt;td&gt;ID&lt;/td&gt;
  &lt;td&gt;First Name&lt;/td&gt;
  &lt;td&gt;Last Name&lt;/td&gt;
  &lt;td&gt;E-mail&lt;/td&gt;
  &lt;td&gt;Phone&lt;/td&gt;
  &lt;/tr&gt;
  
  &lt;?php
  
  $username="username here";
  $password="password here";
  $database="database name here";
  
  mysql_connect(localhost,$username,$password);
  @mysql_select_db($database) or die( "Unable to select database");
  $query="SELECT * FROM students";
  $result=mysql_query($query);
  mysql_close();
  
  while ($row=mysql_fetch_array($result)){
  echo ("&lt;tr&gt;&lt;td&gt;$row[id]&lt;/td&gt;");
  echo ("&lt;td&gt;$row[first]&lt;/td&gt;");
  echo ("&lt;td&gt;$row[last]&lt;/td&gt;");
  echo ("&lt;td&gt;$row[email]&lt;/td&gt;");
  echo ("&lt;td&gt;$row[phone]&lt;/td&gt;");
  echo ("&lt;td&gt;&lt;a href=\"StudentEdit.php?id=$row[id]\"&gt;Edit&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;");
  }
  echo "&lt;/table&gt;";
  
  ?&gt;
  </code></pre>
  
  <p>As you can see, each row has an "Edit" link that is supposed to allow the user to edit that individual student's data. Here, then, is StudentEdit.php:</p>
  
  <pre><code>&lt;?php
  
  $username="username";
  $password="password";
  $database="database";
  
  mysql_connect(localhost,$username,$password);
  
  $student_id = $_GET[id]; 
  $query = "SELECT * FROM students WHERE id = '$student_id'"; 
  $result = mysql_query($query);
  $row = mysql_fetch_array($result);
  mysql_close();
  ?&gt;
  
  &lt;form method="post" action="EditStudentData.php" /&gt;
  
  &lt;table&gt;
  
  &lt;tr&gt;
  &lt;td&gt;&lt;input type="hidden" name="id" value="&lt;? echo "$row[id]" ?&gt;"&gt;&lt;/td&gt;
  &lt;/tr&gt;
  
  &lt;tr&gt;
  &lt;td&gt;First Name:&lt;/td&gt;
  &lt;td&gt;&lt;input type="text" name="first" value="&lt;? echo "$row[first]" ?&gt;"&gt;&lt;/td&gt;
  &lt;/tr&gt;
  
  &lt;tr&gt;
  &lt;td&gt;Last Name:&lt;/td&gt;
  &lt;td&gt;&lt;input type="text" name="last" value="&lt;? echo "$row[last]" ?&gt;"&gt;&lt;/td&gt;
  &lt;/tr&gt;
  
  &lt;tr&gt;
  &lt;td&gt;Phone Number:&lt;/td&gt;
  &lt;td&gt;&lt;input type="text" name="phone" value="&lt;? echo "$row[phone]" ?&gt;"&gt;&lt;/td&gt;
  &lt;/tr&gt;
  
  &lt;tr&gt;
  &lt;td&gt;E-mail:&lt;/td&gt;
  &lt;td&gt;&lt;input type="text" name="email" value="&lt;?echo "$row[email]" ?&gt;"&gt;&lt;/td&gt;
  &lt;/tr&gt;
  
  &lt;/table&gt;
  
  &lt;/form&gt;
  </code></pre>
  
  <p>When I execute this, however, I get the following error message:</p>
  
  <p>Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home4/lukaspl1/public_html/StudentEdit.php on line 12</p>
  
  <p>Any ideas what's wrong, and how to fix it?</p>
  
  <p>Thank you in advance!</p>