title: Using A PHP variable within An SQL statement
body: <p>I am very new to PHP and SQL... 
  I cant get variables used within my SQL code to be recognised ?</p>
  
  <p>here is the full code, i am trying to populate a database table with answer to questions answered from a html page</p>
  
  <p>any help please?</p>
  
  <pre><code>&lt;?php
  
  $odbc = mysql_connect ('localhost', 'root', '') or die ("Could not connect to database");
  mysql_select_db('Questionaire', $odbc) or die ("can not find database");
  
  $sql;
  $quantity = 1;
  
  $id = 222;
  $r = 1;
  $course = '555';
  
  
  
  for ($i = 1; $i &lt; $quantity; $i++)
  {   
  $answer = $_POST['q'.$r];
  
  if ($answer == 'a')
  {
      $sql = "INSERT INTO `questionaire`.`tanswer` (`User`, `QuestionID`, `Answer1`, `Answer2`, `Answer3`, `Answer4`, `Answer5`, `AnswerFreeText`) VALUES (".$id.", ".$i.",       '1', '0', '0',  '0', '0', '')";                         
  }
  
  if ($answer == 'b')
  {
      $sql = 'INSERT INTO `questionaire`.`tanswer` (`User`, `QuestionID`, `Answer1`, `Answer2`, `Answer3`, `Answer4`, `Answer5`, `AnswerFreeText`) VALUES ('.$id.', '.$i.',               "0", "1", "0",  "0", "0", "")'; 
      echo'&lt;h2&gt; hello &lt;/h2&gt;'; 
      $result = mysql_query($sql,$odbc) or die ("can not run query");                 
  }
  
  if ($answer == 'c')
  {
      $sql = "INSERT INTO `questionaire`.`tanswer` (`User`, `QuestionID`, `Answer1`, `Answer2`, `Answer3`, `Answer4`, `Answer5`, `AnswerFreeText`) VALUES (".$id.", ".$i.",       '0', '0', '1',  '0', '0', '')";     echo $sql;                  
  }
  
  if ($answer == 'd')
  {
      $sql = "INSERT INTO `questionaire`.`tanswer` (`User`, `QuestionID`, `Answer1`, `Answer2`, `Answer3`, `Answer4`, `Answer5`, `AnswerFreeText`) VALUES (".$id.", ".$i.", '0', '0', '0', '1', '0', '');";                           
  }
  
  $r++;
  
  }
  
  
  
  ?&gt;
  </code></pre>