我对 PHP 和 SQL 很陌生...我无法识别我的 SQL 代码中使用的变量?
这是完整的代码,我正在尝试用从 html 页面回答的问题的答案填充数据库表
请问有什么帮助吗?
<?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 < $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'<h2> hello </h2>';
$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++;
}
?>