title: Updating Tables MySql/PHP - inserting variable name instead of values
body: <p>i've searched too much about it, but, i'm not even rookie in PHP scripting... so I need     help.</p>
  <p>i'm developing a homemade system to home automation, based on PHP/MySql, and, i need a backend to update the database tables and make things really happen in live.</p>
  <p>so, i found the code i need, and, after some adaptation, here it is.</p>
  <pre><code>&lt;?php
  $username = &quot;root&quot;;
  $password = &quot;&quot;;
  $hostname = &quot;localhost&quot;; 
  $database = &quot;domonet&quot;;
  $tabela = &quot;AtuadorAnalogico&quot;;
  $valor1 = &quot;AA_id&quot;;
  $valor2 = &quot;AA_nome&quot;;
  $valor3 = &quot;AA_valor&quot;;
  
  //connection to the database
  
  $dbhandle = mysql_connect($hostname, $username, $password) 
    or die(&quot;Unable to connect to MySQL&quot;);
  echo &quot;Connected to MySQL&lt;br&gt;&quot;;
  
  
  //select a database to work with
  
  $selected = mysql_select_db($database,$dbhandle) 
    or die(&quot;Could not select examples&quot;);
    echo &quot;Connected ao banco&lt;br&gt;&quot;;
  
  //get data from db
  
  $sql = mysql_query(&quot;SELECT * FROM $tabela&quot;);
  $count=mysql_num_rows($sql);
  
  //start a table
  
  echo '&lt;form name=&quot;form1&quot; method=&quot;post&quot; action=&quot;&quot;&gt;
  &lt;table width=&quot;292&quot; border=&quot;0&quot; cellspacing=&quot;1&quot; cellpadding=&quot;0&quot;&gt;';
  
  //start header of table
  
  echo '&lt;tr&gt;
  &lt;th&gt;&amp;nbsp;&lt;/th&gt;
  &lt;th&gt;Name&lt;/th&gt;
  &lt;th&gt;Email&lt;/th&gt;
  &lt;/tr&gt;';
  
  //loop through all results
  
  while($r=mysql_fetch_object($sql)){
  
  //print out table contents and add id into an array and email into an array
  
  echo '&lt;tr&gt;
  &lt;td&gt;&lt;input type=&quot;hidden&quot; name=&quot;id[]&quot; value='.$r-&gt;$valor1.' readonly&gt;&lt;/td&gt;
  &lt;td&gt;'.$r-&gt;$valor2.'&lt;/td&gt;
  &lt;td&gt;&lt;input name=&quot;email[]&quot; type=&quot;text&quot; id=&quot;price&quot; value=&quot;'.$r-&gt;$valor3.'&quot;&gt;&lt;/td&gt;
  &lt;/tr&gt;';
  }
  
  //submit button
  
  
  echo' &lt;tr&gt;
  &lt;td colspan=&quot;3&quot; align=&quot;center&quot;&gt;&lt;input type=&quot;submit&quot; name=&quot;Submit&quot; value=&quot;Submit&quot;&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;/table&gt;
  &lt;/form&gt;';
  
  
  // if form has been submitted, process it
  
  
  if(isset($_POST[&quot;Submit&quot;]))
  {
     // get data from form
     
   $name = $_POST['name'];
     
     // loop through all array items
     
     foreach($_POST['id'] as $value)
     {
      
     // minus value by 1 since arrays start at 0      
     
             $item = $value-1;
             
                  //update table
                
      //appending suggested changes  
      //place of var_dumps
     //$sql1 = mysql_query(&quot;UPDATE $tabela SET $valor3='$valor3[$item]' WHERE     $valor1='$value'&quot;) or die(mysql_error());
     $sql1 = mysql_query(&quot;UPDATE $tabela SET $valor3='$email[$item]' WHERE $valor1='$value'&quot;) or die(mysql_error());
  
  
                                  
     }
  
  // redirect user
  
  $_SESSION['success'] = 'Updated';
  header(&quot;location:index.php&quot;);
  }
  </code></pre>
  <p>The problem is, after clicking at the 'submit' button, the database table just get weird.</p>
  <pre><code>From This:
  mysql&gt; select * from AtuadorAnalogico;
  +-------+---------+----------+
  | AA_id | AA_nome | AA_valor |
  +-------+---------+----------+
  |     0 | nome1   | d        |
  |     1 | nome2   | Agfd     |
  |     2 | nome3   | Aasd     |
  |     3 | nome4   | _asda    |
  |     5 | teste5  | a        |
  |     6 | teste6  | asda     |
  |     7 | testte1 | o        |
  |     8 | testte2 | rwqdq    |
  |     9 | asdadsd | gwrg     |
  |    10 | asdadsd | qdw      |
  |    11 | adasd   | 234      |
  |    12 | adasd   | g42      |
  +-------+---------+----------+
  12 rows in set (0.00 sec)
  </code></pre>
  <p>and</p>
  <pre><code>To This:
  mysql&gt; select * from AtuadorAnalogico
  +-------+---------+----------+
  | AA_id | AA_nome | AA_valor |
  +-------+---------+----------+
  |     0 | nome1   |          |
  |     1 | nome2   |          |
  |     2 | nome3   |          |
  |     3 | nome4   |          |
  |     5 | teste5  |          |
  |     6 | teste6  |          |
  |     7 | testte1 |          |
  |     8 | testte2 |          |
  |     9 | asdadsd |          |
  |    10 | asdadsd |          |
  |    11 | adasd   |          |
  |    12 | adasd   |          |
  +-------+---------+----------+
  12 rows in set (0.00 sec)
  </code></pre>
  <p>Please, I really can't see my mistake.</p>
  <p>Thanks a lot for the code revision.</p>
  <p>PS.: just to remember. When i run the php in browser, it DOES show the contents of the database table just as it is. after clicking the submit, the thing goes crazy.</p>
  <h1>EDIT1</h1>
  <p>after renaming 'valor3' to 'email', it is not writing the string to the table anymore.
  but, leaving the whole column blank (writing blank in all column cells as shown above) .</p>
  <h1>EDIT2</h1>
  <p>in comments, we just figured that is a logical problem with the code.
  just can't figure out what to do.</p>
  <h1>Update</h1>
  <pre><code>the output from just before the sql1 query:
  var_dump($email);
  var_dump($tabela);
  var_dump($item);
  var_dump($valor3);
  var_dump($valor1);
  var_dump($value);
  
  vardump $email: NULL
  vardump $tabela: string(16) &quot;AtuadorAnalogico&quot;
  vardump $item: int(-1)
  vardump $valor3: string(8) &quot;AA_valor&quot;
  vardump $valor1: string(5) &quot;AA_id&quot;
  vardump $value: string(1) &quot;0&quot;
  
  vardump $email: NULL
  vardump $tabela: string(16) &quot;AtuadorAnalogico&quot;
  vardump $item: int(0)
  vardump $valor3: string(8) &quot;AA_valor&quot;
  vardump $valor1: string(5) &quot;AA_id&quot;
  vardump $value: string(1) &quot;1&quot;
  vardump $email: NULL
  
  vardump $tabela: string(16) &quot;AtuadorAnalogico&quot;
  vardump $item: int(1)
  vardump $valor3: string(8) &quot;AA_valor&quot;
  vardump $valor1: string(5) &quot;AA_id&quot;
  vardump $value: string(1) &quot;2&quot; 
  </code></pre>
  <p>ps.: i reduced the number of rows, so makes easyer to see and think.</p>
  <h1>I FOUND IT!</h1>
  <p>stack didn't allowed me to answer my own question, so, i'm editing as they wish.</p>
  <p>renaming this line fields 'name' and 'id' to &quot;&quot;anything&quot;&quot;:</p>
  <pre><code>&lt;td&gt;&lt;input name=&quot;valores[]&quot; type=&quot;text&quot; id=&quot;valores&quot; value=&quot;'.$r-&gt;$valor3.'&quot;&gt;&lt;/td&gt;
  </code></pre>
  <p>renaming this to the same as before:</p>
  <pre><code>     $valores = $_POST['valores'];
  </code></pre>
  <p>changing this to EQUAL (as stated by @JA):</p>
  <pre><code>  $item = $value;
  </code></pre>
  <p>and adapting the query like this:</p>
  <pre><code>    $sql1 = mysql_query(&quot;UPDATE $tabela SET $valor3='$valores[$item]' WHERE $valor1='$value'&quot;) or die(mysql_error());
  </code></pre>
  <p>and IT's DONE!</p>
  <h1>Thanks so much for your brainstorming.</h1>
  <p>and special thanks for @arielnmz who gave me the idea of dumping the vars so i can find the logic error.</p>
  <p>by the way, the code finished.</p>
  <pre><code>&lt;?php
  $username = &quot;root&quot;;
  $password = &quot;&quot;;
  $hostname = &quot;localhost&quot;; 
  $database = &quot;domonet&quot;;
  $tabela = &quot;AtuadorAnalogico&quot;;
  $valor1 = &quot;AA_id&quot;;
  $valor2 = &quot;AA_nome&quot;;
  $valor3 = &quot;AA_valor&quot;;
  
  //connection to the database
  
  $dbhandle = mysql_connect($hostname, $username, $password) 
    or die(&quot;Unable to connect to MySQL&quot;);
  echo &quot;Connected to MySQL&lt;br&gt;&quot;;
  
  
  //select a database to work with
  
  $selected = mysql_select_db($database,$dbhandle) 
    or die(&quot;Could not select examples&quot;);
    echo &quot;Connected ao banco&lt;br&gt;&quot;;
  
  //get data from db
  
  $sql = mysql_query(&quot;SELECT * FROM $tabela&quot;);
  $count=mysql_num_rows($sql);
  
  //start a table
  
  echo '&lt;form name=&quot;form1&quot; method=&quot;post&quot; action=&quot;&quot;&gt;
  &lt;table width=&quot;292&quot; border=&quot;0&quot; cellspacing=&quot;1&quot; cellpadding=&quot;0&quot;&gt;';
  
  //start header of table
  
  echo '&lt;tr&gt;
  &lt;th&gt;&amp;nbsp;&lt;/th&gt;
  &lt;th&gt;Name&lt;/th&gt;
  &lt;th&gt;Email&lt;/th&gt;
  &lt;/tr&gt;';
  
  //loop through all results
  
  while($r=mysql_fetch_object($sql)){
  
  //print out table contents and add id into an array and email into an array
      
  echo '&lt;tr&gt;
  &lt;td&gt;&lt;input type=&quot;hidden&quot; name=&quot;id[]&quot; value='.$r-&gt;$valor1.' readonly&gt;&lt;/td&gt;
  &lt;td&gt;'.$r-&gt;$valor2.'&lt;/td&gt;
  &lt;td&gt;&lt;input name=&quot;valores[]&quot; type=&quot;text&quot; id=&quot;valores&quot; value=&quot;'.$r-&gt;$valor3.'&quot;&gt;&lt;/td&gt;
  &lt;/tr&gt;';
  }
  
  //submit button
      
  
  echo' &lt;tr&gt;
  &lt;td colspan=&quot;3&quot; align=&quot;center&quot;&gt;&lt;input type=&quot;submit&quot; name=&quot;Submit&quot; value=&quot;Submit&quot;&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;/table&gt;
  &lt;/form&gt;';
  
  // if form has been submitted, process it
  
  if(isset($_POST[&quot;Submit&quot;]))
  {
         // get data from form
         
       $valores = $_POST['valores'];
       
         // loop through all array items
         
     foreach($_POST['id'] as $value)
         {
         // minus value by 1 since arrays start at 0 (not anymore)
          
                 $item = $value;
                  //update table
  
         $sql1 = mysql_query(&quot;UPDATE $tabela SET $valor3='$valores[$item]' WHERE     $valor1='$value'&quot;) or die(mysql_error());
  
     }
  
  // redirect user
  
  $_SESSION['success'] = 'Updated';
  header(&quot;location:index.php&quot;);
  };
  </code></pre>
  <p>Everybody have a great night!</p>