简体   繁体   English

插入数据到mysql表以空行结尾?

[英]Insert data to mysql table ends up with blank rows?

I am displaying all questions from the mysql database. 我正在显示mysql数据库中的所有问题。 When i click submit the name, and emailadress is saved correctly. 当我单击提交名称时,正确保存了电子邮件地址。

But question_id and answer_id is just saving blank rows? 但是question_id和answer_id只是保存空白行? Why? 为什么?

I'm i missing something with the sql function for insert the answers? 我想用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: 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);

?>

You've got TWO separate forms. 您有两个单独的表格。 The answer_value business is defined in the one you create inside your db fetch loop. answer_value业务在您在db fetch循环中创建的业务中定义。 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. 在某些完全不同的<form>...</form>块中定义的字段将被完全忽略/不相关,也将不会提交。

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>

You are not preparing question_id in your HTML form. 您没有在HTML表单中准备question_id。 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>';

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. 要在一个POST中提交所有条目,您需要所有元素都在同一表单上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM