Reputation: 313
I am displaying all questions from the mysql database. When i click submit the name, and emailadress is saved correctly.
But question_id and answer_id is just saving blank rows? Why?
I'm i missing something with the sql function for insert the answers?
index.php:
include ('connection.php');
function getQuestions($con) {
// generate all quetions
$query = "SELECT * FROM questions";
$result = @mysqli_query($con, $query);
if ($result) {
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
$body = $row['question_body'];
$question_id = $row['question_id'];
echo '
<tr>
<form action="insert.php" method="POST">
<td>'.$question_id, $body.'</td>
<td><input type="radio" name="answer_value" value="1"></td>
<td><input type="radio" name="answer_value" value="2"></td>
<td><input type="radio" name="answer_value" value="3"></td>
</tr>
</form>
<br/>';
}
}
}
echo'
<form action="insert.php" method="post">
Firstname: <input type="text" name="firstname">
Lastname: <input type="text" name="lastname">
Email: <input type="text" name="email">
<input type="submit">
</form>';
insert.php:
<?php
include ('connection.php');
// escape variables for security
$firstname = mysqli_real_escape_string($con, $_POST['firstname']);
$lastname = mysqli_real_escape_string($con, $_POST['lastname']);
$email = mysqli_real_escape_string($con, $_POST['email']);
$sql="INSERT INTO users (FirstName, LastName, Email)
VALUES ('$firstname', '$lastname', '$email')";
if (!mysqli_query($con,$sql)) {
die('Error: ' . mysqli_error($con));
}
echo "User added <br>";
// escape variables for security
$question_id = mysqli_real_escape_string($con, $_POST['question_id']);
$answer_value = mysqli_real_escape_string($con, $_POST['answer_value']);
$sql="INSERT INTO answers (question_id, answer_value)
VALUES ('$question_id', '$answer_value')";
if (!mysqli_query($con,$sql)) {
die('Error: ' . mysqli_error($con));
}
echo "Answers added";
mysqli_close($con);
?>
Upvotes: 0
Views: 85
Reputation: 9964
You are not preparing question_id in your HTML form. It would go something like this :
echo '<tr>
<form atcion="insert.php" method="post">
<td><input type="hidden" name="question_id" value="'.$question_id.'">'.$question_id, $body.'</td>
<td><input type="radio" name="answer_value" value="1"></td>
<td><input type="radio" name="answer_value" value="2"></td>
<td><input type="radio" name="answer_value" value="3"></td>
</tr>
</form>';
Upvotes: 2
Reputation: 748
You're not submitting the values in your question form. To submit all the entries in one POST, you need all elements on the same form.
Upvotes: 0
Reputation: 360572
You've got TWO separate forms. The answer_value
business is defined in the one you create inside your db fetch loop. But your submit button is actually in the other form, where you have the name/email forms.
Only the fields defined INSIDE a form tag will get submitted. Fields defined in some completely different <form>...</form>
block are utterly ignored/unrelated and will not be submitted as well.
In other words, you need
<form ...>
[radio button set for question #1]
[radio button set for question #2]
...
[input fields for name/email]
</form>
Not
<form ... >
[radio button set #1]
</form>
<form ...>
[radio button set #2]
</form>
<form ...>
[name/email input fields]
</form>
Upvotes: 1