title: Updating a table using php and sql server (DYNAMICALLY!)
body: <p>So I'm trying to create an interface for the user where they can insert, delete and update a table they have in a sql db through the php UX. I've got the basics working, however i've ran into a wall trying to maintain a dynamic feel to it. So let's say hypotethically speaking, a new column is added to the particular table they are trying to update from the sql side of things. Now I've got it so that it will display the new column added, BUT how do I include that new column in my UPDATE sql query. I've got designated variables for each column (ie. $user, $pass, $email) but my question is what if theirs a new column being added, how do we set up the code so that it will automagically add a new variable and maintain the dynamics? I'm going crazy and really need someone's help cause it sounds so simple. (YES I AM QUITE NEW TO THIS).</p>
  
  <p>Thank you for all your help guys! please let me know how I can clarify this question for anyone.</p>
  
  <p>BELOW IS MY WORKING CODE FOR UPDATING A TABLE THAT HAS FIXED COLUMNS. PLEASE ADVISE WHAT I NEED TO ADD OR CHANGE.</p>
  
  <pre><code>&lt;?php
  $result = sqlsrv_query($con,"SELECT * FROM table");
  $colresult = sqlsrv_query($con,"SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS       WHERE TABLE_NAME = 'table' AND TABLE_SCHEMA='schema'");
  $serverName = xxx"; 
  $connectionInfo = array("Database"=&gt;"db", "UID"=&gt;"user", "PWD"=&gt;"pass",  'ReturnDatesAsStrings'=&gt; true, "CharacterSet" =&gt; 'utf-8');
  $con = sqlsrv_connect( $serverName, $connectionInfo);
  $numfields = sqlsrv_num_fields($result);
  
  if( $con ) {
  echo "Connection established.&lt;br /&gt;";
  
  }
  
  else{
  echo "Connection could not be established.&lt;br /&gt;";
  die( print_r( sqlsrv_errors(), true));
  
  }
  
  
  
  
  if (isset($_POST['updatemarked']))
  {
  
  foreach($_POST["id"] AS $id){
  
      **$user = $_POST["username"][$id];
      $pass = $_POST["password"][$id];
      $email = $_POST["email"][$id];**
      if(!$user){
  
      }
      else{
          $sqlupdate= sqlsrv_query($con,"UPDATE table SET username = '$user' WHERE id = $id");
      }
      if(!$pass){
          //$pass = 'empty';//$row['password'];
      }
      else{
  
          $sqlupdate= sqlsrv_query($con,"UPDATE table SET password = '$pass' WHERE id = $id");
      }
      if(!$email){
          //$email = 'empty';//$row['email'];
      }
      else{
  
          $sqlupdate= sqlsrv_query($con,"UPDATE table SET email = '$email' WHERE id = $id");
      } 
  
  
  
  }
  }
  
  
  
  echo '
  &lt;form action="table.php" method="post"&gt;
  &lt;table table class="dynamicTable tableTools table table-striped table-bordered table-  condensed table-white"&gt;';
  
  echo '&lt;tr&gt;';
  echo '&lt;th width="50" align="center"&gt;&lt;span style=color:black&gt;&lt;strong&gt;Select(s)&lt;/strong&gt;    &lt;/span&gt;&lt;/th&gt;';
  while($row = sqlsrv_fetch($colresult)){
  $fieldname = sqlsrv_get_field($colresult, $i);
  
  echo "&lt;th&gt;".$fieldname."&lt;/th&gt;";
  }
  echo "&lt;/tr&gt;";
  
  
  while ($row = sqlsrv_fetch_array($result))
  {
  echo "&lt;tr&gt;";
  
      echo '&lt;td&gt;&lt;input type="checkbox" name="id[]" value="'.$row['id'].'" /&gt;&lt;/td&gt;';
      echo "&lt;td&gt;".$row['id']."&lt;/td&gt;";
      echo '&lt;td&gt;&lt;input type="text" name="username['.$row['id'].']"  placeholder="'.$row['username'].'"/&gt;&lt;/td&gt;';
      echo '&lt;td&gt;&lt;input type="text" name="password['.$row['id'].']" placeholder="'.$row['password'].'"/&gt;&lt;/td&gt;';
      echo '&lt;td&gt;&lt;input type="text" name="email['.$row['id'].']" placeholder="'.$row['email'].'"/&gt;&lt;/td&gt;';        
  echo "&lt;/tr&gt;";
  }
  echo'&lt;/table&gt;';
  echo'&lt;br /&gt;';
  echo '&lt;div align="center"&gt;';
  echo '&lt;input type="submit" name="updatemarked" value="Update Selected" /&gt;';
  echo '&lt;/div&gt;';
  echo '&lt;/form&gt;';
  
  ?&gt;
  </code></pre>