0

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>";
    }
}
?>

This is how my table looks like:

enter image description here

And this is what I'm getting in PHP:

enter image description here

7
  • Can you post the picture of the output... Commented Apr 29, 2019 at 19:12
  • try debugging the $row object, and see what it contains, using var_dump() or print_r() - you might be getting an object or array back from your query, rather than an associative array. you might only need to use $row[0] Commented Apr 29, 2019 at 19:14
  • How is it stored in the database? Commented Apr 29, 2019 at 19:14
  • 1
    Are you just looking for echo "<input type='checkbox' name='data[]' value='" . htmlspecialchars($row['subject']) . "' /> <label>" . htmlspecialchars($row['subject']) . "</label>"; Commented Apr 29, 2019 at 19:14
  • 1
    Your query is only selecting subject; is that the name? Should you maybe do a SELECT * and access $row["subject_id"] in value and just output $row["subject"] after? Commented Apr 29, 2019 at 19:16

1 Answer 1

1

Pretty easy actually, just concatenate the $row['subject'] variable in between the label tags:

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

You can use variables more than once in any output.

Sign up to request clarification or add additional context in comments.

5 Comments

Yes it works! Thank you. It also works with <label>" . htmlspecialchars($row['subject']) . "</label>
Not sure why you're wrapping the values in that function - things coming from the database should be safe already.
No, you always escape for HTML output, never escape values before putting them in the database.
@JayBlanchard I've always heard things should be stored in databases raw most of the time, and escaped on output.
escape on output is sufficient.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.