php - Generate checkbox based on entries on a MySQL table

704

help me guys on my project in school. I want to generate checkbox based on my data in MySQL.

For example... in STUDENT table, the data are: Daniel, Jam, Minho, James, Paul.

I want to make checkboxes based on the contents of the STUDENT table.

[ ] Daniel [ ] Jam [ ] Minho [ ] James [ ] Paul <

382

Answer

looks like this :)

Then once I insert another content "Samantha" on the STUDENT table, it will generate a checkbox that contains [] Samantha.

I can't search for a situation like this on the internet! I'm using PHP, HTML and MySQL (PHPMyAdmin).

I don't know if that is called dynamic or what.

126

Answer

Solution:

$sql = "SELECT name FROM students";
$res = mysql_query($sql);
while($row = mysql_fetch_assoc($res)){
    echo "<input type='checkbox' name='students[]' value='".$row['name']."'>"
        .$row['name'];
}
797

Answer

Solution:

To get that kind of checkboxes, you have to retrieve the result and embed it withHTML code.

// create connection here
$sql = 'select name from STUDENT';
$result = mysqli_query($connection_object,$sql);
while($row = mysqli_fetch_array($result)) {
     echo "<input type='checkbox' value='{$row['name']}'>" . $row['name'];
}

People are also looking for solutions to the problem: php - Techniques to avoid race conditions in an inverted index algorithm

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.