php - PHP表格检查数组mysql中的复选框值时查询?

原文 标签 php html mysql checkbox

Php form checked when the checkbox value in array mysql query?

I have and value in my database. It's call type

I have 2 checkbox in my html form.

<input name="barea[]" type="checkbox" id="barea[]" value="1" {$cbvar}/>Normal 
<input name="barea[]" type="checkbox" value="2" id="barea[]" {$cbvar}/>Gold

And I store data as code below:

$checkboxvar = implode(',', $_GET['barea']);

So my table data like:

+-------+-----------+
| ID    | type      |
+-------+-----------+
| 1     | 1         |
| 2     | 1,2       |
| 3     | 2         |
| 4     | 1,2       |
| 5     | 1,2       |
| 6     | 1         |
+-------------------+

When user edit this data, how to checked the checkbox when the barea[] value exist in_array in the mysql_query?

I try the below coding, in my php file:

$checkbox = explode(',',$row['type']);
if (in_array($_GET['barea'],$checkbox)){
    $cbvar = "checked=\"checked\"";
}else{
    $cbvar = '';
}

In my html

<input name="barea[]" type="checkbox" id="barea[]" value="1" {$cbvar}/>Normal 
<input name="barea[]" type="checkbox" value="2" id="barea[]" {$cbvar}/>Gold

But it can't work, i think maybe the problem is on if (in_array($_GET['barea'],$checkbox)){.

so how to improve my coding, or any other good coding suggestion? thank you.

Answer

=>Try this Code I hope it's useful.

// html page..

<label>Select State</label><br>
<?php 
$allgroup = mysql_query("SELECT * FROM  state");
$flag=false;
while($state_list = mysql_fetch_array($allgroup))
{
   $parr=explode(',',$er['state_id']);
   $size = sizeof($parr);
   for($i=0;$i<$size;$i++) { 
     if($parr[$i]==$state_list['id']) {                              
        $flag=true;
     } 
   }    

  if($flag==true) {
    ?>
    <input  type='checkbox' name='state[]' style="margin-left:5px;" value="<?php echo $state_list['id']; ?>" checked  > <?php echo $state_list['name']; ?> <br>
    <?php
    $flag=false;
  } else { 
    ?>
    <input  type='checkbox' name='state[]' style="margin-left:5px;" value="<?php echo $state_list['id']; ?>"     > <?php echo $state_list['name']; ?> <br>
    <?php
    }
}
?>

// php code ..

<?php
$states="";
$i=0;
foreach( $_POST['state'] as $selected) {
   echo sizeof($_POST['state']);
   if($i==sizeof($_POST['state'])-1) {
       $states = $states.$selected;
   } else {
     $states = $states.$selected.",";
   }
   $i++;
}
?>

翻译

我在数据库中拥有价值。叫做type

我的html表单中有2个复选框。

<input name="barea[]" type="checkbox" id="barea[]" value="1" {$cbvar}/>Normal 
<input name="barea[]" type="checkbox" value="2" id="barea[]" {$cbvar}/>Gold


我将数据存储为以下代码:

$checkboxvar = implode(',', $_GET['barea']);


所以我的表数据像:

+-------+-----------+
| ID    | type      |
+-------+-----------+
| 1     | 1         |
| 2     | 1,2       |
| 3     | 2         |
| 4     | 1,2       |
| 5     | 1,2       |
| 6     | 1         |
+-------------------+


用户编辑此数据时,当checkedbarea[] value中存在in_array时,如何mysql_query复选框?

我在我的php文件中尝试以下编码:

$checkbox = explode(',',$row['type']);
if (in_array($_GET['barea'],$checkbox)){
    $cbvar = "checked=\"checked\"";
}else{
    $cbvar = '';
}


在我的HTML中

<input name="barea[]" type="checkbox" id="barea[]" value="1" {$cbvar}/>Normal 
<input name="barea[]" type="checkbox" value="2" id="barea[]" {$cbvar}/>Gold


但这行不通,我认为问题可能出在if (in_array($_GET['barea'],$checkbox)){上。

那么如何改善我的编码或其他任何好的编码建议?谢谢。
最佳答案
=>尝试此代码,希望它有用。

// html页面..

<label>Select State</label><br>
<?php 
$allgroup = mysql_query("SELECT * FROM  state");
$flag=false;
while($state_list = mysql_fetch_array($allgroup))
{
   $parr=explode(',',$er['state_id']);
   $size = sizeof($parr);
   for($i=0;$i<$size;$i++) { 
     if($parr[$i]==$state_list['id']) {                              
        $flag=true;
     } 
   }    

  if($flag==true) {
    ?>
    <input  type='checkbox' name='state[]' style="margin-left:5px;" value="<?php echo $state_list['id']; ?>" checked  > <?php echo $state_list['name']; ?> <br>
    <?php
    $flag=false;
  } else { 
    ?>
    <input  type='checkbox' name='state[]' style="margin-left:5px;" value="<?php echo $state_list['id']; ?>"     > <?php echo $state_list['name']; ?> <br>
    <?php
    }
}
?>


// php代码..

<?php
$states="";
$i=0;
foreach( $_POST['state'] as $selected) {
   echo sizeof($_POST['state']);
   if($i==sizeof($_POST['state'])-1) {
       $states = $states.$selected;
   } else {
     $states = $states.$selected.",";
   }
   $i++;
}
?>
相关推荐

php - 无法从数组获取正确的值

php - cakePHP在模型中使用请求/身份验证数据的3个最佳实践(基于帐户的云服务)

php - 什么数据类型可用于在mysql中放置标志(布尔值)?

javascript - 尝试将数据从.php文件传递到.js文件

php - 更改子菜单时如何更改网页背景?

php - Symfony2:如何避免数据库中已经存在重复的实体?

php - Eclipse中的PHP 7调试器设置

javascript - 访问URL参数-PHP和Angular.js

php - 使用更新的MySQL库修复PHP数据库连接失败

php - PHP代码不允许多个输入值通过我的表单发送