title: PHP MYSQL array foreach and insert
body: <p><strong>tbl1</strong></p>
  
  <pre><code>id | detail
  1  | this, that
  2  | these, those
  </code></pre>
  
  <p><strong>tbl2</strong></p>
  
  <pre><code>id | visit  | cost
  1  | this   | 20
  2  | that   | 30
  3  | these  | 40
  4  | those  | 50
  </code></pre>
  
  <p><strong>PHP:</strong></p>
  
  <pre><code>&lt;?php 
  
  $id=$_GET['id'];    
  $detail=$_GET['detail'];
  $pieces = explode(",", $rows['detail']);
  
      // $pieces[0]; piece1
      // $pieces[1]; piece2        
      // $pieces[2]; piece2        
      // $pieces[3]; piece3    
      // $pieces[4]; piece4
  
      foreach($pieces[1] as $test){            
  
  $query="SELECT * FROM tbl1, tbl2 WHERE
    $test = tbl2.cost AND
    tbl1.id='$id'";
      }   
  
  $result=mysql_query($query) or die("error: " . mysql_error());
  
  while($rows=mysql_fetch_array($result)){    
  ?&gt;
  &lt;table&gt;
      &lt;tr&gt;
      &lt;td&gt;&lt;input value="&lt;? echo $rows['code'];?&gt;" readonly="readonly"/&gt;&lt;/td&gt;
      &lt;td&gt;&lt;input value="&lt;? echo $rows['cost']; ?&gt;" readonly="readonly"/&gt;&lt;/td&gt;
      &lt;/tr&gt;
    &lt;/table&gt;
  
  &lt;?php
  }
  ?&gt;
  </code></pre>
  
  <p>for id=1, array 0=>this, 1=>that
  So, <code>foreach($pieces[1] as $test)</code>, it means "that"</p>
  
  <p>The above works, but, when I change <code>$pieces[1]</code> to <code>$pieces[0]</code>, I still get the result which corresponds with <code>$pieces[1]</code>.  And, when I change it to <code>$pieces[4]</code> which doesn't exist, I again get the result which corresponds with <code>$pieces[1]</code>. </p>
  
  <p>Where am I doing wrong? How can I make this array work?</p>
  
  <p>According to tbl1.id=1 piece[0]=>this, piece[1]=>that and I want to make it work for my tables. Please help.</p>