如果值在数据库中,则禁用复选框

时间:2018-09-30 01:35:41

标签: php database checkbox

大家好可以帮助我吗?关于如何在数据库中已经存在复选框的情况下禁用复选框,我已经在数据库中插入了值1A,1B,但是突然所有复选框都被禁用了:(这是我的简单代码

 <?php
    //Connections
$server_name='localhost';
$username='root';
$password='admin';
$db_name='matnogreservationv2';
$con= mysqli_connect($server_name, $username, $password, $db_name);
if(mysqli_connect_errno())
{
    echo 'Failed..!!'.mysqli_connect_errno();
} 
>?


//code for disabling checkbox
$resulta= mysqli_query($con,"SELECT * FROM seat WHERE SeatStatus = 1");
$display = mysqli_num_rows($resulta);
$con->query($display);
$disable = $display ? 'disabled="disabled"': '';

<input type="checkbox" name="seat[]" id="1A" value="1A"  <?php echo $disable; ?>>
          <label for="1A">1A</label>

          <input type="checkbox" name="seat[]" id="1B" value="1B" <?php echo $disable; ?>>
          <label for="1B">1B</label>

          <input type="checkbox" name="seat[]" id="1C" value="1C"<?php echo $disable; ?>>
          <label for="1C">1C</label>

          <input type="checkbox" name="seat[]"  id="1D" value="1D"<?php echo $disable; ?>>
          <label for="1D">ID</label>

          <input type="checkbox" name="seat[]" id="1E" value="1E"<?php echo $disable; ?>>
          <label for="1E">1E</label>

          <input type="checkbox" name="seat[]" id="1F" value="1F"<?php echo $disable; ?>>
          <label for="1F">1F</label>

enter image description here

3 个答案:

答案 0 :(得分:0)

尝试一下:

<?php
$arrays=array(
        array('a','b'),
        array('a','b','c'),
        array('a','b','c')
    );
$per=0;
for ($h=0; $h < count($arrays); $h++) { 
    if (count($arrays) > $per) {
        $per= count($arrays);
     }
}

$koneksi = new mysqli("127.0.0.1","root","","data");

$nil=array();
foreach ($arrays as $data) {
    foreach ($data as $key => $value) {
        $data[$key] = $data[$key];  
    }
     $nil[]="('".implode("', '",$data). "')";
 }

$insert="INSERT INTO datams (data1,data2,data3) VALUES ".implode(', ',$nil);

 $queri=mysqli_query($koneksi, $insert);

 if ($queri == true){
    echo 'upload done'.PHP_EOL;
  } else {
     echo 'fail upload'.PHP_EOL;
  }

...

答案 1 :(得分:0)

我不知道$display包含什么。如果您要基于$display值禁用该复选框,我将在下面进行操作。

$resulta= mysqli_query($con,"SELECT * FROM seat WHERE SeatStatus = 1");
//$display = mysqli_num_rows($resulta); // dont now why you want this
$seats= array(); // array look like this : array("1B","1D","1F")

while( $row = mysql_fetch_array( $resulta ) ){
  $seats[] = $row['seat'];
}

现在基于$seats数组中包含的值禁用该复选框。

<input type="checkbox" name="seat[]" id="1B" value="1B" <?php echo (in_array("1B", $seats)?"disabled='disabled'":"") ?>>
<label for="1B">1B</label>

<input type="checkbox" name="seat[]" id="1C" value="1C" <?php echo (in_array("1C", $seats)?"disabled='disabled'":"") ?>>
<label for="1C">1C</label>

答案 2 :(得分:0)

如果我们假设您的表名为test,则如下所示:

enter image description here

那么我们可以得到以下代码:

<?php
    //Connections
$server_name='localhost';
$username='root';
$password='admin';
$db_name='matnogreservationv2';
$con= mysqli_connect($server_name, $username, $password, $db_name);
if(mysqli_connect_errno())
{
    echo 'Failed..!!'.mysqli_connect_errno();
}



//code for disabling checkbox
$resulta= mysqli_query($con,"SELECT * from test");
$display = mysqli_num_rows($resulta);
$array_results = mysqli_fetch_all($resulta, MYSQLI_ASSOC);
$values = array("1A", "1B", "1C", "1D", "1E", "1F");
$array_values = array_column($array_results, 'value');

foreach ($values as $key => $value)
{
        $index = array_search($value, $array_values);

        if ($index === false) // if the value was not exist in the table
            $disable = '';
        elseif ($array_results[$index]['display'] == 1) // if the value was exist in table but its display status was 1
            $disable = 'disabled="disabled"';
        else
            $disable = '';
    ?>
    <input type="checkbox" name="seat[]" id="<?php echo $value?>" value="<?php echo $value?>"  <?php echo $disable; ?>>
    <label for="<?php echo $value?>"><?php echo $value?></label>
    <?php
}
?>