我需要建议,因为我还是PHP新手,我尝试从复选框中获取数据,这是数据来自数据库,所以当我检查它们并按下按钮时它会显示我选择的数据,有没有错误,但我选择它的数据无法显示。这段代码:
<!DOCTYPE html>
<html>
<head>
<body>
<!-- code to show db data with chechbox-->
<form method="post">
<?php
include "db_connect.php";
$sql_select = $dt_bas->prepare("SELECT id_pegawai, nm_pegawai, tmp_lahir FROM pegawai");
$sql_select->execute();
while($row = $sql_select->fetch(PDO::FETCH_ASSOC)){
$id = $row["id_pegawai"]; // id
$nm = $row["nm_pegawai"]; // employe name
$tmp = $row["tmp_lahir"]; // birthday
?>
<input type="checkbox" name="select_DB[]" id="select_DB[]" /><?php echo "$id"."</br>";?>
<?php } ?>
<input type="submit" value="Test">
</form>
</body>
</head>
</html>
处理代码:
<?php
// this code for get the data that is chosen from checkbox
include "db_connect";
if(isset($_POST['select_DB[]'])){
$id = $_POST['select_DB[]'];
$sql_select2 = $db_bas->prepare("SELECT id_pegawai, nm_pegawai, tmp_lahir FROM pegawai WHERE id_pegawai = '$id' ");
$sql_select2->execute();
while($row = $sql_select->fetch(PDO::FETCH_ASSOC)){
$id = $row["id_pegawai"]; // id
$nm = $row["nm_pegawai"]; // employee name
$tmp = $row["tmp_lahir"]; // birth place
echo "$id"." "."$nm"." "."$tmp"."</br>";
}
}
?>
答案 0 :(得分:0)
嗯,有些事情你仍然没有做,并阻止这项工作(不要提及其他事情,如注射上面评论的那些):
<input type="checkbox" value="<?php echo $id ?>" name="select_DB[]" id="select_DB[]" /><?php echo $id; ?> </br>
if(isset($_POST['select_DB'])){
希望这有助于您上路。我建议你学习使用Xdebug进行调试,这样你就可以更好地分析你失败的地方。
答案 1 :(得分:0)
尝试将$ id作为复选框输入中的值,如下所示:
<input type="checkbox" name="select_DB[]" value="<?php echo $id; ?>" /><?php echo $id; ?><br>
对于从表单接收数据的文件。循环遍历数组$ _POST ['select_DB']如图所示
<?php
// this code for get the data that is chosen from checkbox
include "db_connect";
if(isset($_POST['select_DB'])){
foreach($_POST['select_DB'] as $select=>$option_id){
$sql_select2 = $db_bas->prepare("SELECT id_pegawai, nm_pegawai, tmp_lahir FROM pegawai WHERE id_pegawai = '$option_id' ");
$sql_select2->execute();
while($row = $sql_select->fetch(PDO::FETCH_ASSOC)){
$id = $row["id_pegawai"]; // id
$nm = $row["nm_pegawai"]; // employee name
$tmp = $row["tmp_lahir"]; // birth place
echo "$id"." "."$nm"." "."$tmp"."</br>";
}
}
}
?>