lundi 29 avril 2019

Build checkboxes with PHP from mysql

I have the following table in my database:

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

In the table I have already entered some data (subject names).

What I'm trying to do is that for each subject in the table, a checkbox is created with the name of the subject next to it.

So far I have managed to create checkboxes for each subject in the table, but I can not get the name of the subject shown next to the checkbox. Does anyone know how to do it?

I'm doing it this way:

<?php
$sql = "SELECT subject FROM subjects"; /*Select from table name: subjects*/
$result = $conn->query($sql); /*Check connection*/

if($result)
{
    foreach($result as $row)
    {
        echo "<input type='checkbox' name='data[]' value='" . htmlspecialchars($row['subject']) . "' /> <label>Here goes the subject name</label>";
    }
}
?>




Aucun commentaire:

Enregistrer un commentaire