質問オプションの入力をデータベースのテーブルに保存しようとしています。ただし、最後の質問入力のみを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); } ?>