0

I have a table that has different names of competences and another one in which we have the competence_ID and the user_ID that has that competence. I have a form in which I submit the competences that apply to a certain user. Now, I want to retrieve these competences and check boxes in case they are valid. I use this code, but it does not seem to work.

<?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>"; 
?>
auicsc
  • 297
  • 1
  • 3
  • 14
  • [Please, don't use mysql_* functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) in new code. They are no longer maintained [and are officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the [red box](http://uk.php.net/manual/en/function.mysql-connect.php)? Learn about [_prepared statements_](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://php.net/manual/en/mysqlinfo.api.choosing.php) will help you decide which. – TNK Mar 11 '13 at 14:35
  • Sorry, but in your edited question, I'm not sure how you expect people to read and understand such badly formatted code! – freefaller Mar 11 '13 at 14:36
  • I edited it again, with better formatting. – auicsc Mar 11 '13 at 19:29

1 Answers1

0

If you put checked as an attribute on the <input type="checkbox"> it will be checked, even if you leave the actual value of the attribute empty.

So change the following parts from...

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

Into...

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

Which will mean if the $test[$traverse$] is true, checked='checked' will be placed into the HTML, otherwise it will be left out.

freefaller
  • 19,368
  • 7
  • 57
  • 87
  • Thanks for the answer, but why does it check only the first element? I have for example, one employee who has 7 competences, and it always check the first one only, for all employees. – auicsc Mar 11 '13 at 14:25
  • You have two incrementing values (`$counter` and `$traverse`)... however they are both in a single while loop. I'm not exactly sure what you're trying to do, but that doesn't appear to be correct – freefaller Mar 11 '13 at 14:29
  • @auicsc - are you sure you want the closing brace (`}`) *straight* after the `echo $test['c_ID'];`? It looks like you should have all the code running within the first `while` loop – freefaller Mar 11 '13 at 14:31
  • If i put it at the end of the form it will print it many times! I have uploaded the whole form for you to see if there is something wrong with the brace. Thanks a lot ! – auicsc Mar 11 '13 at 14:36
  • @auicsc, format your code and I will consider trying to figure it out... but I refuse to try and do anything with code that is so badly formatted – freefaller Mar 11 '13 at 14:37
  • I hope it is better like this? – auicsc Mar 11 '13 at 14:44