如何在我的数据库中插入单选按钮的值,我应该使用什么表格布局?

我有一个问题列表,我可以发布答案,但现在我不知道如何将它们放入数据库中。我也不知道我应该使用什么样的表格布局。


我尝试了一些东西,但它们会起作用,因为你有多个答案,所以它不能一次全部插入。我在我的表中使用了一个“答案”列,因为我无法制作 1000 个“答案”列,因为不是每个问题列表都有与另一个问题一样多的问题。


<form action="Antwoord.php" method="POST">

    <input type="text" name="Naam" placeholder="Uw naam:">

<?php

   $sql = "SELECT * FROM  questionlist_choice WHERE Qid='1'";

   $result = mysqli_query($conn, $sql);

  if ($result->num_rows > 0) {

    $i = 0;

  while ($row = mysqli_fetch_assoc($result)) {

$vraag = $row['Vraag'];

$vraagA = $row['Vraag_keuzeA'];

$vraagB = $row['Vraag_keuzeB'];

$vraagC = $row['Vraag_keuzeC'];

$vraagD = $row['Vraag_keuzeD'];

$vraagE = $row['Vraag_keuzeE'];

$vraagF = $row['Vraag_keuzeF'];

echo "<div>";

    echo "<br><p>$vraag</p>";

    echo "<input type='radio' name='q[$i]' value='$vraagA'> $vraagA<br>";

    echo "<input type='radio' name='q[$i]' value='$vraagB'> $vraagB<br>";

    echo "<input type='radio' name='q[$i]' value='$vraagC'> $vraagC<br>";

    echo "<input type='radio' name='q[$i]' value='$vraagD'> $vraagD<br>";

    echo "<input type='radio' name='q[$i]' value='$vraagE'> $vraagE<br>";

    echo "<input type='radio' name='q[$i]' value='$vraagF'> $vraagF<br>";


echo "</div>";

$i++;


?>

<input type="submit">

</form>


眼眸繁星
浏览 169回答 2
2回答

潇湘沐

我将创建 3 个不同的表:用户、问题和答案结构:&nbsp;users:&nbsp; &nbsp; &nbsp;- id&nbsp;&nbsp; &nbsp; &nbsp;- name&nbsp; &nbsp; &nbsp;- other data you store ...&nbsp;surveys:&nbsp; &nbsp; &nbsp;- id&nbsp; &nbsp; &nbsp;- name or whatever data you store about survey&nbsp;questions&nbsp; &nbsp; &nbsp;- id&nbsp; &nbsp; &nbsp;- survey_id&nbsp; &nbsp; &nbsp;- text (actual question)&nbsp; &nbsp; &nbsp;- ...&nbsp;answers&nbsp; &nbsp; &nbsp;- id&nbsp; &nbsp; &nbsp;- user_id&nbsp; &nbsp; &nbsp;- question_id&nbsp; &nbsp; &nbsp;- answer如果您想在一列中插入所有答案(我不推荐),您可以序列化答案您还必须使用不同的数据库结构answers:&nbsp; &nbsp; - id&nbsp; &nbsp; - user_id&nbsp; &nbsp; - survey_id&nbsp; &nbsp; - answers将您的 $i 更改为 1,以便您的问题 ID 以 1 开头;&nbsp; $i = 1;&nbsp; while ($row = mysqli_fetch_assoc($result)) {&nbsp; &nbsp; &nbsp; &nbsp;// also change the name to just $i&nbsp; &nbsp; &nbsp; &nbsp;echo "<input type='radio' name='.$i.' value='$vraagA'> $vraagA<br>";发布到数据库:// put all checkbox values in a single array$answers = [];for($i = 1; $i<= $numberOfAnswers; $i++) {&nbsp; &nbsp; $answers[$i] = $_POST[$i];}$answers = serialize($answers);// do the insert query

一只萌萌小番薯

在Antwoord.php你需要这样的逻辑。&nbsp;<?php&nbsp; &nbsp; if (isset($_POST['q1'])){&nbsp; &nbsp; &nbsp; &nbsp; $q1 = $_POST['q1'];&nbsp; &nbsp; &nbsp; &nbsp; mysql_query("INSERT INTO vraag (q1) VALUES ('$q1')");&nbsp; &nbsp; }&nbsp; ?>
打开App,查看更多内容
随时随地看视频慕课网APP