title: MySQL insert row from HTML table into db table
body: <p>I am using a <code>for</code> loop to construct a HTML table from the contents of a MySQL table select query. I have a link on the end of each row to copy that row into another table.</p>
  
  <p>I'm unsure how to get the data from the table row for the MySQL insert query - I have marked the place where I'm struggling with <code>XXX</code>.</p>
  
  <pre><code>&lt;?php
  
  mysql_select_db("cardatabase");
  
  $link = mysql_connect("localhost", "root", "password");
  $query = "SELECT * from cars";
  $result = mysql_query($query);
  
  if($_GET['rent']) {
      $rent = "INSERT INTO rentedcars VALUES('XXX','XXX','XXX','XXX','XXX','XXX','XXX','XXX','XXX','XXX')";
      mysql_query($rent);
      echo "&lt;meta http-equiv='refresh' content='0;url=rent.php'/&gt;";
  }
  
  echo "&lt;table&gt;";
  echo "&lt;tr&gt;&lt;td&gt;ID&lt;/td&gt;&lt;td&gt;Make&lt;/td&gt;&lt;td&gt;Model&lt;/td&gt;&lt;td&gt;Fuel Type&lt;/td&gt;&lt;td&gt;Transmission&lt;/td&gt;&lt;td&gt;Engine Size&lt;/td&gt;&lt;td&gt;Doors&lt;/td&gt;&lt;td&gt;Amount&lt;/td&gt;&lt;td&gt;Available&lt;/td&gt;&lt;td&gt;Date Added&lt;/td&gt;&lt;td&gt;Remove&lt;/td&gt;&lt;/tr&gt;";
  
  for ($i = 0; $i &lt; mysql_num_rows($result); $i++) {
      $row = mysql_fetch_object($result);
      echo "&lt;tr&gt;
          &lt;td&gt;$row-&gt;ID&lt;/td&gt;
          &lt;td&gt;$row-&gt;CARMAKE&lt;/td&gt;
          &lt;td&gt;$row-&gt;CARMODEL&lt;/td&gt;
          &lt;td&gt;$row-&gt;FUELTYPE&lt;/td&gt;
          &lt;td&gt;$row-&gt;TRANSMISSION&lt;/td&gt;
          &lt;td&gt;$row-&gt;ENGINESIZE&lt;/td&gt;
          &lt;td&gt;$row-&gt;DOORS&lt;/td&gt;
          &lt;td&gt;$row-&gt;AMOUNT&lt;/td&gt;
          &lt;td&gt;$row-&gt;AVAILABLE&lt;/td&gt;
          &lt;td&gt;$row-&gt;DATEADDED&lt;/td&gt;
          &lt;td&gt;&lt;a href='?rent=$row-&gt;ID'&gt;Rent&lt;/a&gt;&lt;/td&gt;
          &lt;/tr&gt;";
  }
  echo "&lt;/table&gt;";
  </code></pre>
  
  <p><em>edit (updated code):</em></p>
  
  <pre><code>&lt;?php
  
  mysql_select_db ("cardatabase");
  
  $link   = mysql_connect ("localhost", "root", "password");
  $query  = "SELECT * from cars";
  $result = mysql_query ($query);
  
  if($_GET['rent']) {
      $query_car = sprintf("SELECT * from cars WHERE ID=%s",$_GET['rent']);
      $rslt      = mysql_query($query_car);
      $car       = mysql_fetch_object ($rslt);
      $rent      = "INSERT INTO rentedcars VALUES('$car-&gt;ID','$car-&gt;CARMAKE','$car-&gt;CARMODEL','$car-&gt;FUELTYPE','$car-&gt;TRANSMISSION','$car-&gt;ENGINESIZE','$car-&gt;DOORS','$car-&gt;AMOUNT','$car-&gt;AVAILABLE','$car-&gt;DATEADDED')";
      mysql_query($rent);
      echo "&lt;meta http-equiv='refresh' content='0;url=rent.php'/&gt;";
  }
  
  echo "&lt;table&gt;";
  echo "&lt;tr&gt;";
  echo "&lt;td&gt;ID&lt;/td&gt;&lt;td&gt;Make&lt;/td&gt;&lt;td&gt;Model&lt;/td&gt;&lt;td&gt;Fuel Type&lt;/td&gt;&lt;td&gt;Transmission&lt;/td&gt;&lt;td&gt;Engine Size&lt;/td&gt;&lt;td&gt;Doors&lt;/td&gt;&lt;td&gt;Amount&lt;/td&gt;&lt;td&gt;Available&lt;/td&gt;&lt;td&gt;Date Added&lt;/td&gt;&lt;td&gt;Remove&lt;/td&gt;";
  echo "&lt;/tr&gt;";
  
  while ($row = mysql_fetch_object($result)) {
      echo "&lt;tr&gt;
          &lt;td&gt;$row-&gt;ID&lt;/td&gt;
          &lt;td&gt;$row-&gt;CARMAKE&lt;/td&gt;
          &lt;td&gt;$row-&gt;CARMODEL&lt;/td&gt;
          &lt;td&gt;$row-&gt;FUELTYPE&lt;/td&gt;
          &lt;td&gt;$row-&gt;TRANSMISSION&lt;/td&gt;
          &lt;td&gt;$row-&gt;ENGINESIZE&lt;/td&gt;
          &lt;td&gt;$row-&gt;DOORS&lt;/td&gt;
          &lt;td&gt;$row-&gt;AMOUNT&lt;/td&gt;
          &lt;td&gt;$row-&gt;AVAILABLE&lt;/td&gt;
          &lt;td&gt;$row-&gt;DATEADDED&lt;/td&gt;
          &lt;td&gt;&lt;a href='?rent=$row-&gt;ID'&gt;Rent&lt;/a&gt;&lt;/td&gt;
          &lt;/tr&gt;";
  }
  echo "&lt;/table&gt;";
  </code></pre>