title: Update database values using javascript
body: <p>I have a php application for showing some pictures stored in a database. I want to use javascript to change photos order. Index.php page looks like:</p>
  
  <pre><code>$photos = array();
  $photos_query = "SELECT * FROM `photos` WHERE `id`=".$_SESSION['id']."ORDER BY `order`";
  $data = mysqli_query($dbc, $photos_query);
  while ($photos_row= mysqli_fetch_assoc($data)){
      $photos[] = array(
                  'photo_id' =&gt; $photos_row['photo_id'], 
                  'id' =&gt; $photos_row['id'], 
                  'photo_name' =&gt; $photos_row['photo_name'], 
                  'order' =&gt; $photos_row['photo_order']
              );
  } ... 
  &lt;div id="message"&gt;&lt;/div&gt;
  &lt;div id="showphotos"&gt;
      if (empty($photos)) {
          echo 'No photos';
      } else {
          echo '&lt;ul&gt;';
          foreach ($photos as $photo) {
              echo '&lt;li id="photoid_'.$photo['photo_id'].'"&gt;
             &lt;img src="uploads/', $photo['id'], '/', $photo['photo_id'],'"' .$photo['photo_name']. 'height="150" width="150"&gt;
             ...       &lt;/li&gt;';    
          } echo '&lt;/ul&gt;';
    }
  &lt;/div&gt;
  </code></pre>
  
  <p>This page calls move.js once user tries to change image's position:</p>
  
  <pre><code>$(document).ready(function(){   
  
      function slideOut(){
          setTimeout(function(){
              $("#message").slideUp("slow", function () {});
          }, 2000);
      };
  
      $("#message").hide();
  
      $(function() {
          $('ul').sortable({
              cursor: 'move',
              opacity: 0.7,
              revert: false,
              update: function(){
                  var neworder = $(this).sortable("serialize") + '&amp;update=update';
                  $.post("change_image_position.php", neworder, function (themessage) {
                      $("#message").html(themessage);
                      $("#message").slideDown('slow');
                      slideOut();
                  });
              }
          });
      });
  
  });
  </code></pre>
  
  <p>Finally change_image_position.php contains:</p>
  
  <pre><code>$array = $_POST['photoid'];
  if ($_POST['update'] == "update"){
      $count = 1;
      foreach ($array as $idval) {
          $query = "UPDATE photos SET order = " . $count . " WHERE photo_id = " . $idval;
          mysqli_query($query) or die('Error, insert query failed');
          $count ++;
      }
      echo 'All saved! refresh the page to see the changes';
  } 
  </code></pre>
  
  <p>I am facing two problems... First, order by statement(index.php) is not working and second, query for altering photos through database query(change_image_position.php) does not return any result. Thanks in advance for any help.</p>