MySQL INSERT - 将数据插入数据库的一部分但不是全部

时间:2012-11-20 23:49:47

标签: database insert mysqli

帮助朋友使用表单将测试插入数据库,我可以使用INSERT函数插入test_Name和所有问题,但不会将可能的答案插入数据库。我已经通过回应变量确认了数据。

这是数据库结构:

Database: `mdapTests`
Table structure for table `PossibleAnswers`
CREATE TABLE `PossibleAnswers` (
`test_Num` int(11) NOT NULL,
`question_Num` int(11) NOT NULL,
`p_Answer` varchar(150) NOT NULL,
`order` char(1) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

Table structure for table `Questions`
CREATE TABLE `Questions` (
`test_Num` int(11) NOT NULL,
`question_Num` int(11) NOT NULL,
`question` varchar(150) NOT NULL,
`answer` char(1) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

Table structure for table `Tests`
CREATE TABLE `Tests` (
`test_Num` int(11) NOT NULL auto_increment,
`test_Name` varchar(60) NOT NULL,
PRIMARY KEY  (`test_Num`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

这是INSERT代码。

$test_Name = $_POST['test_Name'];
mysqli_query($mysqli, "INSERT INTO mdapTests.Tests (test_Name) VALUES ('$test_Name')");

$query = "SELECT * FROM Tests";
$result = mysqli_query($mysqli, $query);
while($row = mysqli_fetch_array($result))
    {
        if($test_Name == $row[test_Name])
            $test_Num = $row[test_Num];
    }
$charA = "A";
$charB = "B";
$charC = "C";
$charD = "D";

if(isset($_POST['question1']))
    {
        $question = $_POST['question1'];
        $p_Answer1 = $_POST['p_Answer1_q1'];
        $p_Answer2 = $_POST['p_Answer2_q1'];
        $p_Answer3 = $_POST['p_Answer3_q1'];
        $p_Answer4 = $_POST['p_Answer4_q1'];
        $answer = $_POST['answer1'];
        echo '<br />'.$question;
        echo '<br />'.$p_Answer1;
        echo '<br />'.$p_Answer2;
        echo '<br />'.$p_Answer3;
        echo '<br />'.$p_Answer4;
        echo '<br />'.$answer.'<br /><hr />';

    mysqli_query($mysqli, "INSERT INTO mdapTests.Questions (test_Num, question_Num, question, answer) VALUES ($test_Num,1,'$question','$answer')");

    mysqli_query($mysqli, "INSERT INTO mdapTests.PossibleAnswers (test_Num, question_Num, p_Answer, order) VALUES ($test_Num,1,'$p_Answer1','$charA')");

    mysqli_query($mysqli, "INSERT INTO mdapTests.PossibleAnswers (test_Num, question_Num, p_Answer, order) VALUES ($test_Num,1,'$p_Answer2','charB')");

    mysqli_query($mysqli, "INSERT INTO mdapTests.PossibleAnswers (test_Num, question_Num, p_Answer, order) VALUES ($test_Num,1,'$p_Answer3','$charC')");

    mysqli_query($mysqli, "INSERT INTO mdapTests.PossibleAnswers (test_Num, question_Num, p_Answer, order) VALUES ($test_Num,1,'$p_Answer4','$charD')");
    }

其余部分几乎完全相同,如果我能得到插入这部分代码的可能答案,我可以调整其余部分;

0 个答案:

没有答案