Form processing
what I'm trying to do is run a select statement for each answer to select the answer in the database where the questionID = $i
and the userID = $userID
so I have the query like this set up so far but not sure what I'm missing or am I right and not missing anything? Also no matter what i开发者_如何学JAVA do both fields have values but I'm still getting the error message that I need to fill out both form fields.
<?php
$i = 1;
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
?>
<dl>
<dt style="width: 190px;"><label for="answer[<?php echo $row['id']; ?>]"><?php echo $row['question'] ?></label></dt>
<dd><input type="text" name="answer<?php echo $i ?>[<?php echo $row['id']; ?>]" size="54" /></dd>
</dl>
<?php
++$i;
}
?>
if (empty($_POST['answer1'][$i]) || trim($_POST['answer1'][$i])=="") {$errors = "yes";}
if (empty($_POST['answer2'][$i]) || trim($_POST['answer2'][$i])=="") {$errors = "yes";}
// Error checking, make sure all form fields have input
if ($errors == "yes") {
// Not all fields were entered error
$message = "You must enter values to all of the form fields!";
$output = array('errorsExist' => true, 'message' => $message);
} else {
$userID = mysqli_real_escape_string($dbc,$_POST['userID']);
$answer1 = mysqli_real_escape_string($dbc,$_POST['answer1'][$i]);
$answer2 = mysqli_real_escape_string($dbc,$_POST['answer2'][$i]);
$query = "SELECT * FROM manager_users_secretAnswers WHERE questionID = '".$questionID."' AND userID = '".$userID."'";
$result = mysqli_query($dbc,$query);
echo $query;
You can have a problem with php type autoquessing. Let suppose thatyou have questions with ids: 3,5,7,8 then you are using:
empty($_POST['answer1'][$i])
$_POST['answer1'][3] so you are fetching third element of array.
So I suggest to use not array notation, but: For input name: answer|${id} or answer_${id} instead of answer[$id]
精彩评论