我正在尝试将问题选项输入保存到数据库中的表中。但是,我只能将最后一个问题输入保存到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);
}
?>
答案 0 :(得分:0)
从您的代码中我认为您将为每个问题使用相同名称创建一组输入标记,因此我认为这是您获得的原因。您可以尝试将其声明为数组,然后在保存到数据库时尝试循环。
答案 1 :(得分:-1)
从您的问题:''使用foreach循环到MYSQL''
如果要使用foreach循环,为什么不在数组中执行
<input type="text" name="salaries[]" >
<input type="text" name="salaries[]" >
提交时
像这样循环你的文字字段
$salaries=$_POST['salaries'];
foreach ($salaries as $key => $value){
echo $value;
}
如果你不确定如何在数组中插入表单,我建议你使用prepare statement
//$dbc is your database connection
mysqli_autocommit($dbc,false);
if($stmt=$dbc->prepare("INSERT INTO question_table ('question_detail') VALUES ('?')")){
foreach ($salaries as $key => $value){
$stmt->bind_param('s',$value);
$stmt->execute();
if(!$stmt){//if statement problem , prevent commit
echo 'insertion failed , roll back all query';
exit();
}//end of smtp error
}//end of foreach
mysqli_commit($dbc);