将数据插入mysql表最后是空行?

时间:2014-08-14 16:50:04

标签: php mysql

我正在显示mysql数据库中的所有问题。 当我单击提交名称,并正确保存电子邮件地址。

但是question_id和answer_id只是保存空行?为什么呢?

我是否错过了使用sql函数插入答案的内容?

 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);

?>

3 个答案:

答案 0 :(得分:1)

你有两个单独的表格。 {db}提取循环中创建的answer_value业务是在您创建的业务中定义的。但是你的提交按钮实际上是另一种形式,你有名字/电子邮件表格。

仅提交已定义 INSIDE 表单标记的字段。在一些完全不同的<form>...</form>块中定义的字段完全被忽略/不相关,也不会被提交。

换句话说,你需要

<form ...>
    [radio button set for question #1]
    [radio button set for question #2]
    ...
    [input fields for name/email]
</form>

<form ... >
   [radio button set #1]
</form>
<form ...>
   [radio button set #2]
</form>
<form ...>
   [name/email input fields]
</form>

答案 1 :(得分:1)

您没有在HTML表单中准备question_id。它会是这样的:

  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>';

答案 2 :(得分:0)

您没有在问题表单中提交值。要在一个POST中提交所有条目,您需要在同一表单上的所有元素。