title: How to select a row(s) from mysql table using checkbox (PHP)
body: <p>Hi I have a page that displays all of the contents of my table. Alongisde each row of the table I also have a column containing a checkbox. When the user selects one or more rows by ticking the checkbox and pressing the submit button, I want just those rows to appear in a table on the next page (buy.php). I know it is basically a select statement per row that is selected. But I dont know how to approach this. Can anybody help? Thanks</p>
  
  <pre><code>        &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"&gt;
  &lt;head&gt;
      &lt;meta http-equiv="content-type" content="text/html; charset=utf-8" /&gt;
      &lt;title&gt;Shopping&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;form action='buy.php' method='post'&gt;
  &lt;input type='submit' value='Submit' /&gt;
  &lt;/form&gt; 
  &lt;h1&gt;Buy&lt;/h1&gt;
  &lt;?php // Script 12.7 - sopping.php
  
  $db = mysql_connect('localhost', '#####', '#####');
  mysql_select_db('shopping', $db);
  
  $query = 'SELECT * FROM Items';
  
  if ($r = mysql_query($query, $db)) { 
      print "&lt;form&gt;
      &lt;table&gt;";
      while ($row = mysql_fetch_array($r)) {
          print 
          "&lt;tr&gt;
          &lt;td&gt;{$row['ID']}&lt;/td&gt;
          &lt;td&gt;{$row['Name']}&lt;/td&gt;
          &lt;td&gt;{$row['Cost']}&lt;/td&gt;
          &lt;td&gt;&lt;input type='checkbox' name='buy[{$row['ID']}] value='buy' /&gt;&lt;/td&gt;
          &lt;/tr&gt;";
      }
      print "&lt;/table&gt;
      &lt;/form&gt;";
  
  } else { 
      print '&lt;p style="color: blue"&gt;Error!&lt;/p&gt;';
  } 
  
  mysql_close($db); // Close the connection.
  
  ?&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>EDIT: I tried the suggestion below and I did not get a table. Just the title of the page appeard:</p>
  
  <pre><code>            &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"&gt;
  &lt;head&gt;
      &lt;meta http-equiv="content-type" content="text/html; charset=utf-8" /&gt;
      &lt;title&gt;Confirmation&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  
  &lt;h1&gt;Order Details&lt;/h1&gt;
  &lt;?php // Script 12.7 - buy.php
  
  $db = mysql_connect('localhost', '#####', '#####');
  mysql_select_db('shopping', $db);
  
  foreach($_POST['buy'] as $item) {
  
  $query = 'SELECT * FROM Items WHERE ID = $item';
  
  if ($r = mysql_query($query, $db)) { 
      print "&lt;table&gt;";
      while ($row = mysql_fetch_array($r)) {
          print 
          "&lt;tr&gt;
          &lt;td&gt;{$row['ID']}&lt;/td&gt;
          &lt;td&gt;{$row['Name']}&lt;/td&gt;
          &lt;td&gt;{$row['Cost']}&lt;/td&gt;
          &lt;/tr&gt;";
      }
      print "&lt;/table&gt;";
  
  } else { 
      print '&lt;p style="color: blue"&gt;Error!&lt;/p&gt;';
  } 
  }
  
  mysql_close($db);
  
  ?&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>