显示保存的复选框值

时间:2015-06-10 14:28:33

标签: php html mysql

我有一些我保存在数据库中的复选框选项。我能够查看并选择多个选项并将其保存在数据库中。问题是我想显示保存的信息,但我不知道该怎么做。

<form action="save_comp.php" method="post">
<?php
//Display 

include ('mysql_connect.php');

$sql = mysql_query("SELECT * FROM competency ");
//$row = mysql_fetch_array($sql);

while($row = mysql_fetch_array($sql))
{

echo"<input type='checkbox' name='comp[]' value= ".$row['id']." /> ".$row['competency']." <br />";


}

?>
<input name="submit" type="submit" value="submit" />
</form>

保存到DB

<?php
session_start();
$id = $_SESSION['user_id'];
//$id = 3;

include ('mysql_connect.php');

$insStr = ''; 
foreach($_POST['comp'] as $val){ $insStr .=$val.","; }
mysql_query("INSERT INTO competency_result (user_id,result) VALUES ( '$id', '$insStr' )") or die(mysql_error()); 

echo'<script>alert("Inserted Successfully")</script>'; 
?>

我现在要做的就是以表格格式显示保存的信息。我试过这样做,但它只显示了我保存的ID

<?php
$res= mysql_query("SELECT * FROM competency_result WHERE user_id = '$user'")or die(mysql_error());

while($row = mysql_fetch_array($res))
{
    echo"<tr>";
    echo"<td>&nbsp;$row[result]</td>";                
?>

<?php  
    echo"</tr>";
}
?>                              

2 个答案:

答案 0 :(得分:0)

<form action="save_comp.php" method="post">
<?php
//Display 

include ('mysql_connect.php');

$sql = mysql_query("SELECT * FROM competency ");
//$row = mysql_fetch_array($sql);

while($row = mysql_fetch_array($sql))
{

echo"<input type='checkbox' name='comp[". $row['id']. "]' value='". $row['competency'] ."' /> ".$row['competency']." <br />";


}

?>
<input name="submit" type="submit" value="submit" />
</form>

答案 1 :(得分:0)

如果您想要复选框,请检查以下代码:

# Please note that I don't know how YouTube API works, just guessing.
client = YouTube.new(api_key: 'hola')
video_urls = client.videos # => ['https://...', 'https://...', ...]
相关问题