0

我有一个具有不同权限名称的表和另一个具有权限 ID 和具有该权限的 user_ID 的表。我有一个表单,我在其中提交适用于某个用户的权限。现在,我想检索这些权限和复选框以防它们有效。我使用此代码,但它似乎不起作用。

<?php
include "db.php";
$employees=mysql_query("SELECT * FROM asiakas");

echo "<form method='post' action='' id='employeesselection'><select name='select_employee' id='select_employee'>";
while($row=mysql_fetch_array($employees)){
        $selected = ($row['Id'] == $_POST['select_employee'])?'selected="selected"':'';
        echo '<option '.$selected.' value="'.$row['Id'].'">'.$row['Etunimi'].' - '. $row['Sukunimi'].'</option>';

    }
echo "</select><input type='hidden' name='action' value='selectedemployee'>
        <input type='submit' value ='submit' name='submit'><br/>";

if(isset($_POST['select_employee'])){
    $specific=mysql_query("SELECT Id, Sukunimi,Etunimi from asiakas WHERE Id=".$_POST['select_employee']);
    $sp=mysql_fetch_array($specific);
}
if(isset($sp['Etunimi']) && isset($sp['Sukunimi'])){
        $comp=mysql_query("SELECT * FROM Competences"); 
        echo "<br/>select competences for: ". $sp['Etunimi'];
        $id= $sp['Id'];

        $result=mysql_query("SELECT distinct c_ID from User_Competence WHERE e_ID=".$id);
        while($test=mysql_fetch_array($result))
        {
            echo $test['c_ID']; 
        }


        echo "<table><th>valid?</th><th>Competence description</th>";

        $counter=0;
        $traverse=0;

        while($compi=mysql_fetch_array($comp)){
            $checked='';
            if($test[$traverse]==$counter){
                $checked="checked";
                $traverse++;
            }
            $counter ++;
            echo "<tr><td><input type='checkbox'" .$checked." name='c[]' value='".$compi['Competence_ID']."'></td><td>".$compi['Competence_Description']."</td></tr>";
            }
            echo "</table>";
            echo "<input type='hidden' name='action' value='selectchecked'>";
            echo "<input type='submit' value='submit checks'>"; 
        }

        if(isset($_POST) && !empty($_POST)){
            print_r($_POST);
        }
        if(isset($_POST['action']) && $_POST['action']='selectchecked'){

            if (isset($_POST['c'])){
                $s = $_POST['c'];

                foreach ($s as $k => $v) {

                    if (is_array($v)) {

                        //  array_push($s, implode('', array($v [$what])));

                    }
            };
                echo $abc= implode(',', $s);

                for ( $a=0;$a<count($s);$a++){          
                    $ar=explode(',',$abc);
                    echo $var= $ar[$a];
                    $q=mysql_query("INSERT INTO user_competence(c_ID, e_ID) VALUES ('".$var."','".$id."')");

                }
            }

        }
        echo "</form>"; 
?>
4

1 回答 1

0

如果你把它checked作为一个属性放在<input type="checkbox">它上面,它将被检查,即使你将属性的实际值留空。

因此,将以下部分从...

$checked="checked";
...
echo "<tr><td><input type='checkbox' checked='".$checked."' name='c[]' ...

进入...

$checked="checked='checked'";
...
echo "<tr><td><input type='checkbox' ".$checked." name='c[]' ...

这意味着如果$test[$traverse$]为真,checked='checked'将被放入 HTML 中,否则将被排除在外。

于 2013-03-11T14:23:00.570 回答