2

I have the following tables in my database:

CREATE TABLE subjects (
  subject_id int(11) NOT NULL AUTO_INCREMENT,
  subject_name text,
  PRIMARY KEY (subject_id)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;

CREATE TABLE users (
  id int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
  username varchar(100) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

CREATE TABLE users_subjects (
  users_subjects_id int(11) NOT NULL AUTO_INCREMENT,
  user_id_fk int(11),
  subject_id_fk int(11),
  FOREIGN KEY(user_id_fk) REFERENCES users(id),
  FOREIGN KEY(subject_id_fk) REFERENCES subjects(subject_id),
  PRIMARY KEY (users_subjects_id)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;

In the table 'users_subjects' I’m trying to relate the 'subjects' and 'users' tables. All the data in the tables are entered from my index.php.

I introduce the subject_name from my index.php and every time I enter a new one, checkboxes like these are created in the part where the user is added:

enter image description here

In this way, when entering a user and choosing the subjects that are desired, the 'users_subjects' table is created.

If for example I enter a user 'EXAMPLE' with ID = 1 and the SAT and ICT subjects with IDs 1 and 2, the 'users_subjects' table would look something like this:

enter image description here

The problem is that I am trying to show the users with their corresponding subjects in a PHP table but I doN’t know how to show the subjects separated by commas in the column 'SUBJECT'. I would like to do something like the following image but I don’t know how to do it. Can somebody help me? Thank you

enter image description here

This is my index.php:

<table class="users">
    <tr>
        <th colspan="2"><h2>Users registered</h2></th>
    </tr>
    <tr>
        <th> USER </th>
        <th> SUBJECT </th>
    </tr>

<?php
$result = $conn->query($sql); 
$sql = "SELECT * FROM users WHERE rol='profesor'";
$result = $conn->query($sql);

$sql2 = "SELECT * FROM users_subjects WHERE id=$id";
$result2 = $conn->query($sql2);

if ($result->num_rows==0){

    echo 'No professors >';
}else{
    while($row = $result->fetch_assoc()) {
        echo "<tr>
                  <td>".$row["username"]."</td>
                  <td>".$row["subject"]."</td>
                  </tr>";
    }
}

?>
</table>
ana
  • 417
  • 2
  • 10
  • 3
    You might like to know that MyISAM tables do not enforce foreign key constraints. You should use InnoDB. MyISAM is slowly on its way to being deprecated. Also read my answer to [MyISAM versus InnoDB](https://stackoverflow.com/a/17706717/20860) – Bill Karwin May 05 '19 at 17:07
  • *"but I doN’t know how to show the subjects separated by commas in the column 'SUBJECT'"* Sounds like you are looking for MySQL's [GROUP_CONCAT](https://dev.mysql.com/doc/refman/8.0/en/group-by-functions.html#function_group-concat) function and you would need to use a JOIN – Raymond Nijland May 05 '19 at 17:22

1 Answers1

1

Change the $sql2 query to :-

$sql2 = "SELECT s.subject_name,u.username FROM users_subjects us LEFT JOIN users ON us.user_id=u.id 
LEFT JOIN subjects ON s.subject_id = us.subject_id  WHERE users_subjects_id=$id";

And replace the while loop with the following :- 
while($row = $result->fetch_assoc()) {
        $subjects = implode(',',$row['subject_name']);
        echo "<tr>
                  <td>".$row["username"]."</td>
                  <td>".$subjects."</td>
                  </tr>";
 }

You can take a look but do something like this.

Thanks

Man87
  • 131
  • 6
  • Thank you for your answer. I tried with `$sql2 = "SELECT s.subject_name,u.username FROM users_subjects us LEFT JOIN users ON us.user_id_fk=u.id LEFT JOIN subjects ON s.subject_id = us.subject_id_fk WHERE users_subjects_id=$id";` and `$result2 = $conn->query($sql2)` but it's giving me an error in: `implode` with: `Warning: implode(): Invalid arguments passed ` – ana May 05 '19 at 17:14