title: Update and exchange values of two rows of a single column on selection of position nos from a drop down list
body: <p>I am working on a system where I need to upload image files from the back end admin panel. </p>
  
  <p>In the display page of admin panel here I have also implemented a system to change the position of image by selection of position nos from a dropdown list. </p>
  
  <p>In the display page I am using two sql queries, one will fetch and display records row by row and another will fetch the id and position ids into the dropdown list and use selected=selected to by default display the position ids of each row. </p>
  
  <p>Till here all are working fine. Now I need to implement a system where on selection of the position id from the dropdown list of any row the existing position id of the row should be interchange with another row and the record should be updated to the database table. </p>
  
  <p>Suppose if some one selects the position 3 for the first row the position of first row should be updated to 3 where id is 1 and the positon 1 should be updated to the row whose id is 3. </p>
  
  <p>I am just stuck here how to run the query to implement this. </p>
  
  <p>I am showing the php codes below. Here I got the selected position exactly what I have selected but when I pass the id value I only got the id of the last row no. </p>
  
  <p>I don't know why it is happening here. </p>
  
  <p>Please help me.</p>
  
  <pre><code>  &lt;?php
  session_start();
  include('../configuration.php');
  ?&gt;
  &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"&gt;
  &lt;head&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;
  &lt;title&gt;&lt;?php echo PROJECT_TITLE;?&gt;&lt;/title&gt;
  
  &lt;!--style for pagination--&gt;
  &lt;link href="pagination/pagination.css" rel="stylesheet" type="text/css" /&gt;
  &lt;link href="pagination/B_blue.css" rel="stylesheet" type="text/css" /&gt;
  &lt;!--style for pagination--&gt;
  
  &lt;link rel="stylesheet" type="text/css" href="style.css" /&gt;
  &lt;script type="text/javascript" src="clockp.js"&gt;&lt;/script&gt;
  &lt;script type="text/javascript" src="clockh.js"&gt;&lt;/script&gt; 
  &lt;script type="text/javascript" src="jquery.min.js"&gt;&lt;/script&gt;
  &lt;script type="text/javascript" src="ddaccordion.js"&gt;&lt;/script&gt;
  
  &lt;!--jquery for delete &amp; save in archive pop up--&gt;
  &lt;script type="text/javascript" src="jconfirmaction.jquery.js"&gt;&lt;/script&gt;
  &lt;script type="text/javascript" src="jconfirmaction_arcv.js"&gt;&lt;/script&gt;
  
  &lt;script type="text/javascript"&gt;
  
      $(document).ready(function() {
          $('.ask').jConfirmAction();
      });
      $(document).ready(function() {
          $('.arc').jConfirmAction1();
      });
      &lt;/script&gt;
  
  &lt;script language="javascript" type="text/javascript" src="niceforms.js"&gt;&lt;/script&gt;
  &lt;script type="text/javascript"&gt;
  ddaccordion.init({
      headerclass: "submenuheader", //Shared CSS class name of headers group
      contentclass: "submenu", //Shared CSS class name of contents group
      revealtype: "click", //Reveal content when user clicks or onmouseover the header? Valid value: "click", "clickgo", or "mouseover"
      mouseoverdelay: 200, //if revealtype="mouseover", set delay in milliseconds before header expands onMouseover
      collapseprev: true, //Collapse previous content (so only one open at any time)? true/false 
      defaultexpanded: [], //index of content(s) open by default [index1, index2, etc] [] denotes no content
      onemustopen: false, //Specify whether at least one header should be open always (so never all headers closed)
      animatedefault: false, //Should contents open by default be animated into view?
      persiststate: true, //persist state of opened contents within browser session?
      toggleclass: ["", ""], //Two CSS classes to be applied to the header when it's collapsed and expanded, respectively ["class1", "class2"]
      togglehtml: ["suffix", "&lt;img src='images/plus.gif' class='statusicon' /&gt;", "&lt;img src='images/minus.gif' class='statusicon' /&gt;"], //Additional HTML added to the header when it's collapsed and expanded, respectively  ["position", "html1", "html2"] (see docs)
      animatespeed: "fast", //speed of animation: integer in milliseconds (ie: 200), or keywords "fast", "normal", or "slow"
      oninit:function(headers, expandedindices){ //custom code to run when headers have initalized
          //do nothing
      },
      onopenclose:function(header, index, state, isuseractivated){ //custom code to run whenever a header is opened or closed
          //do nothing
      }
  })
  &lt;/script&gt;
  
  &lt;style type="text/css"&gt;
  .foto_style img {
      width:100px;
      height:75px;
  }
  &lt;/style&gt;
  &lt;link rel="stylesheet" type="text/css" media="all" href="niceforms-default.css" /&gt;
  
  &lt;/head&gt;
  &lt;body&gt;
  &lt;div id="main_container"&gt;
  
      &lt;div class="header"&gt;    
      &lt;div class="right_header"&gt;Welcome &lt;?php echo stripslashes($res-&gt;fields["name"]); ?&gt;. | &lt;a href="http://localhost/ssn/gallery_cme_clinical.php" target="_blank"&gt;Visit site&lt;/a&gt; | &lt;a href="logout.php" class="logout"&gt;Logout&lt;/a&gt;&lt;/div&gt;
      &lt;div id="clock_a"&gt;&lt;/div&gt;
      &lt;/div&gt;
  
      &lt;div class="main_content"&gt;
  
                      &lt;div&gt;
                      &lt;h1&gt;WELCOME TO THE ADMINISTRATION PANEL&lt;/h1&gt;
                      &lt;/div&gt; 
  
  
  
  
      &lt;div class="center_content"&gt;  
  
  
  
      &lt;div class="left_content"&gt;
              &lt;?php include("includes/leftcontents.php"); ?&gt;
      &lt;/div&gt;
  
      &lt;div class="right_content"&gt;            
  
      &lt;h2&gt;MANAGE PHOTO GALLERY - CME ON CLINICAL PATHIOLOGY &amp; RESEARCH METHODOLOGY:&lt;/h2&gt; 
  
  &lt;table id="rounded-corner" summary="Table of Assamese Dishes"&gt;
  
      &lt;thead&gt;
  
          &lt;tr&gt;
              &lt;th scope="col" class="rounded-company" colspan="2"&gt;&lt;a href="cme_clinical_add.php" style="text-decoration:none; color:#000000;"&gt;ADD NEW IMAGE:&lt;/a&gt;&lt;/th&gt;
              &lt;th scope="col" class="rounded-q4" colspan="5"&gt;&amp;nbsp;&lt;/th&gt;
          &lt;/tr&gt;
  
          &lt;tr&gt;
              &lt;th scope="col" class="rounded" colspan="3"&gt;
              &lt;form name="reg" id="reg"&gt;
              &lt;input type="text" name="searchtxt" id="searchtxt" style="width:250px;" value="" /&gt;&lt;/th&gt;
              &lt;th scope="col" class="rounded" colspan="5"&gt;&lt;input type="submit" name="btn" id="btn" value="search" onclick="funsrc();" /&gt;
              &lt;/th&gt;
          &lt;/tr&gt;
          &lt;/thead&gt;
          &lt;/form&gt;
  
      &lt;thead&gt;
          &lt;tr&gt;
              &lt;th scope="col" class="rounded" style="color:#000000;"&gt;Image.&lt;/th&gt;
              &lt;th scope="col" class="rounded" style="color:#000000;"&gt;Image Desc.&lt;/th&gt;
              &lt;th scope="col" class="rounded" style="color:#000000;"&gt;Uploaded On.&lt;/th&gt;
              &lt;th scope="col" class="rounded" style="color:#000000;"&gt;Position.&lt;/th&gt;
              &lt;th scope="col" class="rounded" style="color:#000000;"&gt;Edit&lt;/th&gt;
              &lt;th scope="col" class="rounded" style="color:#000000;"&gt;Delete&lt;/th&gt;
              &lt;th scope="col" class="rounded" style="color:#000000;"&gt;Send to Archive&lt;/th&gt;
          &lt;/tr&gt;
      &lt;/thead&gt;
          &lt;tbody&gt;
          &lt;tr&gt;
  
          &lt;?php
          $query = mysql_query("select * from cme_clinical_table order by position");
          if(mysql_num_rows($query)&gt;0)
          {
              while($data = mysql_fetch_array($query)){
  
  
          ?&gt;
              &lt;td class="foto_style"&gt;&lt;img src="&lt;?php echo $data["image_thumb"];?&gt;" width="100" height="50" /&gt;&lt;/td&gt;
              &lt;td&gt;&lt;?php echo stripslashes($data["image_dtls"]); ?&gt;&lt;/td&gt;
              &lt;td&gt;&lt;?php echo stripslashes($data["created_date"]); ?&gt;&lt;/td&gt;
  
  
      &lt;td align="center"&gt;
      &lt;?php
      $sql="Select * from  cme_clinical_table order by position";
      $row = mysql_query($sql);
      $total  = mysql_num_rows($row);
      ?&gt;
      &lt;form action="" enctype="multipart/form-data" name="pos_chng" id="pos_chng" method="post"&gt;
       &lt;select name="cnange_pos" id="cnange_pos" style="width:60px;" &gt;
      &lt;?php if($total&gt;0) {
      while($res = mysql_fetch_array($row))
      {
      ?&gt;
      &lt;option value="&lt;?php echo $data["id"]; $id = $data["id"];?&gt;" &lt;?php if(($data["position"]==$res["position"])){ echo 'selected="selected"';}?&gt; &gt;&lt;?php echo $res["position"];?&gt;&lt;/option&gt;
      &lt;?php 
      }}
      //}?&gt;
      &lt;/select&gt;&amp;nbsp;&lt;input type="submit" name="sub" id="sub" value="save" /&gt;
      &lt;/form&gt;
      &lt;/td&gt;
  
  
              &lt;td align="center"&gt;&lt;a href="cme_clinical_edit.php?id=&lt;?php echo $data["id"];?&gt;"&gt;&lt;img src="images/user_edit.png" alt="" title="" border="0" /&gt;&lt;/a&gt;&lt;/td&gt;
              &lt;td align="center"&gt;&lt;a href="display_cme_clinical.php?act=del&amp;id=&lt;?php echo $data["id"];?&gt;" class="ask"&gt;&lt;img src="images/trash.png" alt="" title="" border="0" /&gt;&lt;/a&gt;&lt;/td&gt;
              &lt;td align="center"&gt;&lt;a href="display_cme_clinical.php?action=send_arcv&amp;id=&lt;?php echo $data["id"];?&gt;" class="arc"&gt;&lt;img src="images/trash.png" alt="" title="" border="0" /&gt;&lt;/a&gt;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;?php }}else{?&gt;
  
      &lt;/tbody&gt;
          &lt;tbody&gt;
          &lt;tr&gt;&lt;td height="30" colspan="7" align="center"&gt;&lt;b&gt;No Contents found.&lt;/b&gt;&lt;/td&gt;&lt;/tr&gt;
          &lt;/tbody&gt;
          &lt;?php } ?&gt;
  
  
      &lt;tfoot&gt;
          &lt;tr&gt;
              &lt;td colspan="6" class="rounded-foot-left"&gt;
              &lt;!-- PAGINATION STARTS --&gt;
  
  
          &lt;!-- PAGINATION STARTS --&gt;
  &lt;/td&gt;
              &lt;td class="rounded-foot-right"&gt;&amp;nbsp;&lt;/td&gt;
  
          &lt;/tr&gt;
      &lt;/tfoot&gt;
  
  &lt;/table&gt;
  
  
  
       &lt;/div&gt;&lt;!-- end of right content--&gt;
  
       &lt;/div&gt;&lt;!--end of center content --&gt;               
  
  
  
  
      &lt;div class="clear"&gt;&lt;/div&gt;
      &lt;/div&gt; &lt;!--end of main content--&gt;
  
  
  
      &lt;div class="footer"&gt;
  
  
      &lt;/div&gt;
  
  &lt;/div&gt;      
  &lt;/body&gt;
  &lt;/html&gt;
  
  &lt;?php
  include("includes/common_function.php");
  if(isset($_POST['sub']))
  {
      $change_pos = $_REQUEST['cnange_pos'];
  
      $query = mysql_query("update cme_clinical_table
       set position = 
       case 
       when id = '".$id."' then '".$change_pos."'
       when id = '".$change_pos."' then '".$id."'
       end
       WHERE id  in ('".$id."' , '".$change_pos."')");
  
  }
  ?&gt;
  </code></pre>