无法在php和mysql中存储多个单选按钮值

时间:2015-03-29 15:05:21

标签: html mysql radio-button

我想将每个问题的单选按钮值存储到我的数据库中。我跑了它,它只存储第一个问题。我知道使用ifelseif可能有误。用户在所有问题中回答如何将它们全部存储在mysql表中?是否需要使用for循环?

  saveunit.php
  <?php

  include'connect.php';


  if(isset($_POST['u1'])){
    $u1 = $_POST['u1'];
    mysql_query("INSERT INTO unit_form (u1) VALUES ('$u1')");
}
  elseif(isset($_POST['u2'])){
    $u2 = $_POST['u2'];
    mysql_query("INSERT INTO unit_form (u2) VALUES ('$u2')");
}

 ?>


unitform.php

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

U1 I have found the unit intellectually interesting and stimulating :  <br />
1 <input type="radio" name="u1" value="1">  2 <input type="radio" name="u1" value="2">  3 <input type="radio" name="number" value="u1">  4 <input type="radio" name="u1" value="4">  5 <input type="radio" name="u1" value="5"> <br /> <br />

U2 I have gained knowledge that I consider valuable :  <br />
1 <input type="radio" name="u2" value="1">  2 <input type="radio" name="u2" value="2">  3 <input type="radio" name="u2" value="3">  4 <input type="radio" name="u2" value="u2">  5 <input type="radio" name="u2" value="5"> <br /> <br />

U3 I have acquired skills and abilities that I consider valuable :  <br />
1 <input type="radio" name="u3" value="1">  2 <input type="radio" name="u3" value="2">  3 <input type="radio" name="u3" value="3">  4 <input type="radio" name="u3" value="4">  5 <input type="radio" name="u3" value="5"> <br /> <br />


<input type = "Submit" name = "Submit1" value = "Submit">
<input type="reset" name="reset" value="Clear">

1 个答案:

答案 0 :(得分:0)

我认为你不需要if和else if if。它存储u1因为它总是执行第一个if(),这就是它总是存储第一个数据的原因。你需要做的是将所有单位放在一个if()中。 试试这个......

 if(isset($_POST['u1'])){
    $u1 = $_POST['u1'];
    $u2 = $_POST['u2'];
    $u3 = $_POST['u3'];
    $u4 = $_POST['u4'];
    $u5 = $_POST['u5'];
    mysql_query("INSERT INTO unit_form (u1,u2,u3,u4,u5) VALUES ('$u1','$u2','$u3','$u4','$u5')");
}