title: Seats booking system
body: <p>So, i made a seat booking system where there are checkboxes in every seat and the user select this boxes and the seats are considered reserved. </p>
  
  <p>But now i want make the user be able to give each marked seat a name. How can i do that?</p>
  
  <p>seats.php</p>
  
  <pre><code>&lt;?php include("login.php"); ?&gt;
  &lt;html&gt;
  &lt;head&gt;
      &lt;title&gt;Tickets&lt;/title&gt;
      &lt;style&gt;
          * {
              font-size: 11px;
              font-family: arial;
          }
      &lt;/style&gt;
      &lt;script&gt;
  
          function reserveSeats() {
  
              var selectedList = getSelectedList('Reserve Seats');
  
              if (selectedList) {
                  if (confirm('Do you want to reserve 
                                 selected seat/s ' 
                               + selectedList + '?')) {
                      document.forms[0].oldStatusCode.value=0;
                      document.forms[0].newStatusCode.value=1;
                      document.forms[0].action='bookseats.php';
                      document.forms[0].submit();
                  } else {
                      clearSelection();
                  }
              }
          }
  
  
          function cancelSeats() {
  
              var selectedList = getSelectedList('Cancel Reservation');
  
              if (selectedList) {
                  if (confirm('Do you want to cancel reserved seat/s ' + selectedList + '?')) {
                      document.forms[0].oldStatusCode.value=1;
                      document.forms[0].newStatusCode.value=0;
                      document.forms[0].action='bookseats.php';
                      document.forms[0].submit();
                  } else {
                      clearSelection();
                  }
              }
          }
  
  
          function confirmSeats() {
  
              var selectedList = getSelectedList('Confirm Reservation');
  
              if (selectedList) {
                  if (confirm('Do you want to confirm reserved seat/s ' + selectedList + '?')) {
                      document.forms[0].oldStatusCode.value=1;
                      document.forms[0].newStatusCode.value=2;
                      document.forms[0].action='bookseats.php';
                      document.forms[0].submit();
                  } else {
                      clearSelection();
                  }
              }
          }
  
  
          function getSelectedList(actionSelected) {
  
              // get selected list
              var obj = document.forms[0].elements;
              var selectedList = '';
              for (var i = 0; i &lt; obj.length; i++) {
                  if (obj[i].checked &amp;&amp; obj[i].name == 'seats[]') {
                      selectedList += obj[i].value + ', ';
                  }
              }
  
              // no selection error
              if (selectedList == '') {
                  alert('Please select a seat before clicking ' + actionSelected);
                  return false;
              } else {
                  return selectedList;
              }
  
          }
  
          function clearSelection() {
              var obj = document.forms[0].elements;
              for (var i = 0; i &lt; obj.length; i++) {
                  if (obj[i].checked) {
                      obj[i].checked = false;
                  }
              }
          }
  
  
          function refreshView() {
              clearSelection();
              document.forms[0].action='&lt;?php echo $_SERVER['PHP_SELF']; ?&gt;';
              document.forms[0].submit();
          }
  
      &lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;table&gt;
  &lt;tr&gt;&lt;td width="100%" align="center"&gt;
  &lt;form action="&lt;?php echo $_SERVER['PHP_SELF']; ?&gt;" method="post"&gt;
  
  &lt;input type="hidden" name="oldStatusCode" value=""/&gt;
  &lt;input type="hidden" name="newStatusCode" value=""/&gt;
  
  &lt;table width='100%' border='0'&gt;
      &lt;tr&gt;&lt;td align='center'&gt;
          &lt;input type='button' value='Refresh View' onclick='refreshView();'/&gt;
      &lt;/td&gt;&lt;/tr&gt;
  &lt;/table&gt;
  &lt;/td&gt;&lt;/tr&gt;
  &lt;tr&gt;&lt;td width="100%" align="center"&gt;
  &lt;table width='100%' border='0'&gt;
      &lt;tr&gt;&lt;td align='center'&gt;
          &lt;input type='button' value='Reserve Seats' onclick='reserveSeats()'/&gt;
          &amp;nbsp;&lt;input type='button' value='Confirm Reservation' onclick='confirmSeats()'/&gt;
          &amp;nbsp;&lt;input type='button' value='Cancel Reservation' onclick='cancelSeats()'/&gt;
      &lt;/td&gt;&lt;/tr&gt;
  &lt;/table&gt;
  &lt;/td&gt;&lt;/tr&gt;
  &lt;tr&gt;&lt;td width="100%" align="center"&gt;
  &lt;table width='100%' border='0'&gt;
      &lt;tr&gt;&lt;td align='center'&gt;
          &lt;input type='button' value='Clear Selection' onclick='clearSelection()'/&gt;&lt;/td&gt;
      &lt;/tr&gt;
  &lt;/table&gt;
  &lt;/td&gt;&lt;/tr&gt;
  &lt;tr&gt;&lt;td width="100%" align="center"&gt;
  &lt;?php
  /*
   * Created on Mar 17, 2007
   * Author: dayg
   */
  
  $linkID = @ mysql_connect("localhost", "tickets", "tickets") or die("Could not connect to MySQL server");
  @ mysql_select_db("tickets") or die("Could not select database");
  /* Create and execute query. */
  $query = "SELECT * from seats order by rowId, columnId desc";
  $result = mysql_query($query);
  $prevRowId = null;
  $seatColor = null;
  $tableRow = false;
  //echo $result;
  echo "&lt;table width='100%' border='0' cellpadding='3' cellspacing='3'&gt;";
  while (list($rowId, $columnId, $status, $updatedby) = mysql_fetch_row($result))
  {
      if ($prevRowId != $rowId) {
          if ($rowId != 'A') {
              echo "&lt;/tr&gt;&lt;/table&gt;&lt;/td&gt;";
              echo "\n&lt;/tr&gt;";
          }
          $prevRowId = $rowId;
          echo "\n&lt;tr&gt;&lt;td align='center'&gt;&lt;table border='1' cellpadding='8' cellspacing='8'&gt;&lt;tr&gt;";
      } else {
          $tableRow = false;
      }
      if ($status == 0) {
          $seatColor = "lightgreen";
      } else if ($status == 1 &amp;&amp; $updatedby == 'user1') {
          $seatColor = "FFCC99";
      } else if ($status == 1 &amp;&amp; $updatedby == 'user2') {
          $seatColor = "FFCCFF";
      } else if ($status == 2 &amp;&amp; $updatedby == 'user1') {
          $seatColor = "FF9999";
      } else if ($status == 2 &amp;&amp; $updatedby == 'user2') {
          $seatColor = "CC66FF";
      } else {
          $seatColor = "red";
      }
  
      echo "\n&lt;td bgcolor='$seatColor' align='center'&gt;";
      echo "$rowId$columnId";
      if ($status == 0 || ($status == 1 &amp;&amp; $updatedby == $_SERVER['PHP_AUTH_USER'])) {
          echo "&lt;input type='checkbox' name='seats[]' value='$rowId$columnId'&gt;&lt;/checkbox&gt;";
      }
      echo "&lt;/td&gt;";
          if (($rowId == 'A' &amp;&amp; $columnId == 7) 
              || ($rowId == 'B' &amp;&amp; $columnId == 9) 
              || ($rowId == 'C' &amp;&amp; $columnId == 9) 
              || ($rowId == 'D' &amp;&amp; $columnId == 10) 
              || ($rowId == 'E' &amp;&amp; $columnId == 8) 
              || ($rowId == 'F' &amp;&amp; $columnId == 5) 
              || ($rowId == 'G' &amp;&amp; $columnId == 13) 
              || ($rowId == 'H' &amp;&amp; $columnId == 14) 
              || ($rowId == 'I' &amp;&amp; $columnId == 14) 
              || ($rowId == 'J' &amp;&amp; $columnId == 12) 
              || ($rowId == 'K' &amp;&amp; $columnId == 14) 
              || ($rowId == 'L' &amp;&amp; $columnId == 13) 
              || ($rowId == 'M' &amp;&amp; $columnId == 9)) {
              // This fragment is for adding a blank cell which represent the "center aisle"
              echo "&lt;td&gt;&amp;nbsp;&lt;/td&gt;";
          }
  }
  
  echo "&lt;/tr&gt;&lt;/table&gt;&lt;/td&gt;";
  echo "&lt;/tr&gt;";
  echo "&lt;/table&gt;";
  
  /* Close connection to database server. */
  mysql_close();
  ?&gt;
  &lt;/td&gt;&lt;/tr&gt;
  &lt;tr&gt;&lt;td&gt;&amp;nbsp;&lt;/td&gt;&lt;/tr&gt;
  &lt;tr&gt;&lt;td width="100%" align="center"&gt;
      &lt;table border="1" cellspacing="8" cellpadding="8"&gt;
          &lt;tr&gt;
              &lt;td bgcolor='lightgreen'&gt;Available&lt;/td&gt;
              &lt;td bgcolor='FFCC99'&gt;Reserved user1&lt;/td&gt;
              &lt;td bgcolor='FF9999'&gt;Confirmed user1&lt;/td&gt;
              &lt;td bgcolor='FFCCFF'&gt;Reserved user2&lt;/td&gt;
              &lt;td bgcolor='CC66FF'&gt;Confirmed user2&lt;/td&gt;
          &lt;/tr&gt;
      &lt;/table&gt;
  &lt;/td&gt;&lt;/tr&gt;
  &lt;tr&gt;&lt;td&gt;&amp;nbsp;&lt;/td&gt;&lt;/tr&gt;
  &lt;tr&gt;&lt;td width="100%" align="center"&gt;
    &lt;a href="seatplan.jpg" target="new"&gt;View Layout&lt;/a&gt; 
  &lt;/td&gt;&lt;/tr&gt;
  &lt;/table&gt;
  &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>bookseats.php</p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
      &lt;title&gt;Book Seats&lt;/title&gt;
      &lt;style&gt;
          * {
              font-size: 14px;
              font-family: arial;
          }
      &lt;/style&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;?php include("login.php"); ?&gt;
  &lt;center&gt;
  &lt;br/&gt;
  &lt;br/&gt;
  &lt;br/&gt;
  &lt;?php
      if (isset($_POST['seats']))
      {
          $user = $_SERVER['PHP_AUTH_USER'];
  
          $newStatusCode = $_POST['newStatusCode'];
          $oldStatusCode = $_POST['oldStatusCode'];
  
          // open database connection
          $linkID = @ mysql_connect("localhost", "tickets", "tickets") or die("Could not connect to MySQL server");
          @ mysql_select_db("tickets") or die("Could not select database");
  
          // prepare select statement
          $selectQuery = "SELECT rowId, columnId from seats where (";
          $count = 0;
          foreach($_POST['seats'] AS $seat) {
              if ($count &gt; 0) {
                  $selectQuery .= " || ";
              }
              $selectQuery .= " ( rowId = '" . substr($seat, 0, 1) . "'";
              $selectQuery .= " and columnId = " . substr($seat, 1) . " ) ";
              $count++;
          }
          $selectQuery .= " ) and status = $oldStatusCode";
          if ($oldStatusCode == 1) {
              $selectQuery .= " and updatedby = '$user'";
          }
  
          //echo $selectQuery;
  
          // execute select statement
          $result = mysql_query($selectQuery);
          //echo $result;
  
          $selectedSeats = mysql_num_rows($result);
          //echo "&lt;br/&gt;" . $selectedSeats;
  
          if ($selectedSeats != $count) {
              $problem = "&lt;h3&gt;There was a problem executing your request. No seat/s were updated.&lt;/h3&gt;";
              $problem .= "Possible problems are:";
              $problem .= "&lt;ul&gt;";
              $problem .= "&lt;li&gt;Another process was able to book the same seat while you were still browsing.&lt;/li&gt;";
              $problem .= "&lt;li&gt;You were trying to Confirm an unreserved Seat.&lt;/li&gt;";
              $problem .= "&lt;li&gt;You were trying to Cancel an unreserved Seat.&lt;/li&gt;";
              $problem .= "&lt;li&gt;You were trying to Reserve a reserved Seat.&lt;/li&gt;";
              $problem .= "&lt;li&gt;There was a problem connecting to the database.&lt;/li&gt;";
              $problem .= "&lt;/ul&gt;";
              $problem .= "&lt;a href='seats.php'&gt;View Seat Plan&lt;/a&gt;";
              die ($problem);
          }
  
          // prepare update statement
          $newStatusCode = $_POST['newStatusCode'];
          $oldStatusCode = $_POST['oldStatusCode'];
  
          $updateQuery = "UPDATE seats set status=$newStatusCode, updatedby='$user' where ( ";
          $count = 0;
          foreach($_POST['seats'] AS $seat) {
              if ($count &gt; 0) {
                  $updateQuery .= " || ";
              }
              $updateQuery .= " ( rowId = '" . substr($seat, 0, 1) . "'";
              $updateQuery .= " and columnId = " . substr($seat, 1) . " ) ";
              $count++;
          }
          $updateQuery .= " ) and status = $oldStatusCode";
          if ($oldStatusCode == 1) {
              $updateQuery .= " and updatedby = '$user'";
          }
  
          // perform update
          $result = mysql_query($updateQuery);
          $updatedSeats = mysql_affected_rows();
  
          if ($result &amp;&amp; $updatedSeats == $count) {
              //$mysql-&gt;commit();
              echo "&lt;h3&gt;";
              echo "You have successfully updated $updatedSeats seat/s: ";
              echo "[";
              foreach($_POST['seats'] AS $seat) {
                  $rowId = substr($seat, 0, 1);
                  $columnId = substr($seat, 1);
                  echo $rowId . $columnId . ", "; 
              }
              echo "]";
              echo "...&lt;/h3&gt;";
          } else {
              //$mysql-&gt;rollback();
              echo "&lt;h3&gt;There was a problem executing your request. No seat/s were updated.&lt;/h3&gt;";
              echo "Possible problems are:";
              echo "&lt;ul&gt;";
              echo "&lt;li&gt;Another process was able to book the same seat while you were still browsing.&lt;/li&gt;";
              echo "&lt;li&gt;You were trying to Confirm an unreserved Seat.&lt;/li&gt;";
              echo "&lt;li&gt;You were trying to Cancel an unreserved Seat.&lt;/li&gt;";
              echo "&lt;li&gt;You were trying to Reserve a reserved Seat.&lt;/li&gt;";
              echo "&lt;li&gt;There was a problem connecting to the database.&lt;/li&gt;";
              echo "&lt;/ul&gt;";
          }
  
          echo "&lt;a href='seats.php'&gt;View Seat Plan&lt;/a&gt;";
  
          // Enable the autocommit feature
          //$mysqldb-&gt;autocommit(TRUE);
  
          // Recuperate the query resources
          //$result-&gt;free();
  
          mysql_close();
      }
  ?&gt;
  &lt;/center&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p><strong>EDIT:</strong></p>
  
  <p>I tried the following: 
  When the user submit the checkboxes, this is included:</p>
  
  <p>name.php</p>
  
  <pre><code>&lt;?php
  //$mysql-&gt;commit();
  session_start();
  $_SESSION['rowId'] = $_POST['rowId'];
  $_SESSION['columnId'] = $_POST['columnId'];
              echo "&lt;h3&gt;";
              echo "Please enter the name for each seat:&lt;br&gt;&lt;p&gt;&amp;nbsp&lt;/p&gt;";
              echo "";
              foreach($_POST['seats'] AS $seat) {
                  $rowId = substr($seat, 0, 1);
                  $columnId = substr($seat, 1);
                  echo $rowId . $columnId . '&lt;/br&gt;&lt;form method="post" name="input" action="pt2.php" &gt;
  &lt;input name="name" type="text"/&gt;&lt;/br&gt;';
              }
  ?&gt;
  
  &lt;input type="submit" name="Submit" value="insert" /&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>pt2.php:</p>
  
  <pre><code>&lt;?php
  // Connect to MySQL
  mysql_connect("localhost", "root", "root") or die("Connection Failed");
  mysql_select_db("tickets")or die("Connection Failed");
  
  $name = $_POST['name'];
  session_start();
  $rowId = $_SESSION['rowId'];
  $columnId = $_SESSION['columnId'];
  
  $result = mysql_query("UPDATE seats SET updatedby='".$name."' WHERE rowId='".$rowId."' AND columnId='".$columnId."'") 
  or die(mysql_error());  
  
  ?&gt;
  </code></pre>
  
  <p>If the user selects only one seat, this code works like a charm. But when there's two or more, the $columnId and the $rowId doesn't change as it should, and only the name of the last checked seat is changed.</p>