我正在尝试将问题选项输入保存到数据库中的表中。但是,我只能将最后一个问题输入保存到 db 中,而不能保存之前的其他问题。
我目前正在使用 foreach 循环根据问题结构表输出文本字段输入。
我真的不知道如何用语言解释,让我给你看我的代码。我是这个领域的新手,希望你们理解并帮助我:)
foreachtest.php
<?php $user_name = "root"; $password = ""; $database = "leadership_program"; $server = "localhost"; $db_handle = mysql_connect($server, $user_name, $password); $db_found = mysql_select_db($database, $db_handle); $result = mysql_query("SELECT * FROM question_structure_tb", $db_handle); $row = mysql_fetch_assoc($result); mysql_close($db_handle); $survey_name = $row['survey_name']; $row2 = array_slice($row, 2); echo 'Survey Name: '. $survey_name ; foreach ($row2 as $v) { //echo "$v\n"; if ($v != NULL) { echo '<p>Question:</p>'; echo '<p>Enter Question: <input type=\"text\" name=\"question\" size=\"200\" maxLength=\"300\" value></p>'; if ($v === 'radio') { echo"<p>Option 1:<input type=\"text\" name=\"optionone\" size=\"40\" maxLength=\"30\"></p>"; echo"<p>Option 2:<input type=\"text\" name=\"optiontwo\" size=\"40\" maxLength=\"30\"></p>"; echo"<p>Option 3:<input type=\"text\" name=\"optionthree\" size=\"40\" maxLength=\"30\"></p>"; echo"<p>Option 4:<input type=\"text\" name=\"optionfour\" size=\"40\" maxLength=\"30\"></p>"; echo"<p>Option 5:<input type=\"text\" name=\"optionfive\" size=\"40\" maxLength=\"30\"></p>"; echo"<p>Option 6:<input type=\"text\" name=\"optionsix\" size=\"40\" maxLength=\"30\"></p>"; echo"<p>Option 7:<input type=\"text\" name=\"optionseven\" size=\"40\" maxLength=\"30\"></p>"; echo"<p><input type=\"hidden\" name=\"optioneight\" size=\"40\" maxLength=\"30\"></p>"; echo"<p><input type=\"hidden\" name=\"optionnine\" size=\"40\" maxLength=\"30\"></p>"; echo"<p><input type=\"hidden\" name=\"optionten\" size=\"40\" maxLength=\"30\"></p>"; echo"<p><input type=\"hidden\" name=\"optioneleven\" size=\"40\" maxLength=\"30\"></p>"; echo"<p><input type=\"hidden\" name=\"optiontwelve\" size=\"40\" maxLength=\"30\"></p>"; echo"<p><input type=\"hidden\" name=\"optionthirteen\" size=\"40\" maxLength=\"30\"></p>"; echo"<p><input type=\"hidden\" name=\"optionfourteen\" size=\"40\" maxLength=\"30\"></p>"; } else if ($v === 'checkbox') { echo"<p>Option 1:<input type=\"text\" name=\"optionone\" size=\"40\" maxLength=\"30\"></p>"; echo"<p>Option 2:<input type=\"text\" name=\"optiontwo\" size=\"40\" maxLength=\"30\"></p>"; echo"<p>Option 3:<input type=\"text\" name=\"optionthree\" size=\"40\" maxLength=\"30\"></p>"; echo"<p>Option 4:<input type=\"text\" name=\"optionfour\" size=\"40\" maxLength=\"30\"></p>"; echo"<p>Option 5:<input type=\"text\" name=\"optionfive\" size=\"40\" maxLength=\"30\"></p>"; echo"<p>Option 6:<input type=\"text\" name=\"optionsix\" size=\"40\" maxLength=\"30\"></p>"; echo"<p>Option 7:<input type=\"text\" name=\"optionseven\" size=\"40\" maxLength=\"30\"></p>"; echo"<p>Option 8:<input type=\"text\" name=\"optioneight\" size=\"40\" maxLength=\"30\"></p>"; echo"<p>Option 9:<input type=\"text\" name=\"optionnine\" size=\"40\" maxLength=\"30\"></p>"; echo"<p>Option 10:<input type=\"text\" name=\"optionten\" size=\"40\" maxLength=\"30\"></p>"; echo"<p>Option 11:<input type=\"text\" name=\"optioneleven\" size=\"40\" maxLength=\"30\"></p>"; echo"<p>Option 12:<input type=\"text\" name=\"optiontwelve\" size=\"40\" maxLength=\"30\"></p>"; echo"<p>Option 13:<input type=\"text\" name=\"optionthirteen\" size=\"40\" maxLength=\"30\"></p>"; echo"<p>Option 14:<input type=\"text\" name=\"optionfourteen\" size=\"40\" maxLength=\"30\"></p>"; } else { echo "<p>this is comment or with comments.therefore no input field</p>"; } } else { } unset($v); } ?> <p><input type="submit" value="Save" name="B1"></p>
foreachtest_save.php
$user_name = "root"; $password = ""; $database = "leadership_program"; $server = "localhost"; $db_handle = mysql_connect($server, $user_name, $password); $db_found = mysql_select_db($database, $db_handle); if ($db_found) { $Questions = $_POST['question']; $SQL = "INSERT INTO question_table ('question_detail') VALUES ('$Questions')"; $result = mysql_query($SQL); //execute the above $Option1 = $_POST['optionone']; $Option2 = $_POST['optiontwo']; $Option3 = $_POST['optionthree']; $Option4 = $_POST['optionfour']; $Option5 = $_POST['optionfive']; $Option6 = $_POST['optionsix']; $Option7 = $_POST['optionseven']; $Option8 = $_POST['optioneight']; $Option9 = $_POST['optionnine']; $Option10 = $_POST['optionten']; $Option11 = $_POST['optioneleven']; $Option12 = $_POST['optiontwelve']; $Option13 = $_POST['optionthirteen']; $Option14 = $_POST['optionfourteen']; $SQL2 = "INSERT INTO option_table " . "(option_one,option_two, option_three, option_four,option_five, option_six, option_seven, option_eight, option_nine, option_ten, option_eleven, option_twelve, option_thirteen, option_fourteen)" . "VALUES" . "('$Option1','$Option2','$Option3','$Option4', '$Option5','$Option6','$Option7','$Option8','$Option9','$Option10','$Option11','$Option12','$Option13','$Option14' )"; $result2 = mysql_query($SQL2); //execute the above if ($result2) { print "Options added to the database"; } else { print "Error: Options not added"; } mysql_close($db_handle); } else { print "Database NOT Found "; mysql_close($db_handle); } ?>