我正在从数据库中检索一些数据,并允许用户通过复选框进行多项选择,并为每个选中的复选框选择一个级别。保存时,我只能看到DB中的选中复选框,但不会看到所选的级别。
进行选择的代码
include ('mysql_connect.php');
$sql = mysql_query("SELECT * FROM competency WHERE department = '$department'");
while($row = mysql_fetch_array($sql))
{
echo "<tr>";
echo "<td>";
echo"<input type='checkbox' name='comp[]' value= ".$row['id']." /> ".$row['competency']." <br /> </td>";
echo"<td> <select name='level[]'value= ".$row['id']." >
<option></option>
<option>level 1</option>
<option>level 2</option>
<option>level 3</option>
<option>level 4</option>
<option>level 5</option> </select> </td> ";
}
echo "</tr>";
?>
<input name="submit" type="submit" value="submit" />
</form>
<?php
echo" </table>";
?>
&#13;
.. 保存到数据库的代码
session_start();
$id = $_SESSION['user_id'];
$hobb = $_POST['comp'];
$level = $_POST['level'];
include ('mysql_connect.php');
$N = count($hobb);
for($i=0; $i < $N; $i++)
{
$var1=$hobb[$i];
$var2 = $level[$i];
//include ('connect.php');
include ('mysql_connect.php');
$table = "INSERT INTO competency_result (user_id,competency_id,level) ".
"VALUES ('$id', '$var1', '$var2')";
mysql_query($table) or die(mysql_error());
$inserted_fid = mysql_insert_id();
mysql_close();
}
&#13;
答案 0 :(得分:0)
为comp[]
和level[]
设置相同的密钥,即。 $row['id']
-
while($row = mysql_fetch_array($sql))
{
...[your code]...
echo "<input type='checkbox' name='comp[".$row['id']."]' value= ".$row['id']." /> ".$row['competency']." <br /> </td>";
echo "<td> <select name='level[".$row['id']."]' >
...[your code]...
}
(注意:<select>
没有value
属性。这是由<option>
然后您可以使用
轻松获取相应的值foreach($hobb as $key=>$val)
{
$var1=$hobb[$key];
$var2=$level[$key];
...[your code]...
}
(另外,看起来你的循环中有<tr>
,但</tr>
之后。我会假设你想要在循环中移动结束标记以正确关闭每一行)