在下面的代码中,我正在从数据库构建表单。问题是当使用foreach($ _POST as $ response => $ answer)时,我只得到两个变量$ response ['response']和$ response ['response_id']。为什么我无法获得更多,即回复,名称和价值?
// Question form
echo "<style>.error{color: red;}
</style>";
echo '<form action="'.$_SERVER['PHP_SELF'].'" method="POST">';
$legend = $responses[0]['category'];
echo "<fieldset>";
echo"<legend>$legend</legend>";
foreach ($responses as $response) {
if ($legend != $response['category']) {
$legend = $response['category'];
echo "</fieldset>";
echo "<fieldset>";
echo"<legend>$legend</legend>";
var_dump($response);
}
echo '<label '.($response['response'] == NULL ? 'class="error"' : '').' for ="'.$response['response_id'].'">'.$response['name'].':</label>';
echo "Love <input ".($response['response'] == 1 ? 'checked = "checked"' :'')." type='radio' '.'id='".$response['response_id']."' name='".$response['topic_id']."' value='1'></input> or";
echo "Hate <input ".($response['response'] == 2 ? 'checked = "checked"':'')."type='radio' id='".$response['response_id']."' name='".$response['topic_id']."' value='2'></input><br />";
}
echo "</fieldset>";
echo"<br /><input type = 'submit' submit = 'submit'>";
echo "</form>";
// php action on form
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
var_dump($_POST);
echo "post <br />";
foreach ($_POST as $response => $answer) {
var_dump($_POST);
$query = "UPDATE mismatch_response SET response = '$answer' WHERE response_id = '$response' ";
mysqli_query($dbc, $query);
//var_dump($answer);
}