Assuming the following MySQL table:
table-name 'questions,' with columns q_id, q_text
<?php
// I'm assuming you've connected to the database server, and the correct database
$query = "SELECT q_id AS num, q_text AS question FROM questions";
$results = mysql_query($query);
$row = mysql_fetch_array($results);
?>
<form enctype="form/multipart" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<fieldset>
<?php
while($row = mysql_fetch_array($results)) {
echo "<label for=\"q" . $row['num'] . "\">" . $row['question'] . "</label>";
echo "<input type=\"text\" name=\"q" $row['num'] . "\" />
}
?>
</fieldset>
</form>
This should -assuming I've not messed up really badly- generate something like:
<label for="q1">What is your name?</label><input type="text" name="q1" />
<label for="q2">What is your favourite colour?</label><input type="text" name="q2" />
It might also be worth putting in id attributes as well, but see if the basics work first.
As an addenda, this will only implement straight <label>
/<input>
pairs; if you need to use radio buttons or checkboxes, then you'll need some way of differentiating between the required form element types. Which will almost certainly involve a second table (for different types "radio","check","text","file", etc), and a lookup table to link the question text to the form-element type for that question.
Also, it's worth noting that using mysqli
or pdo
might offer better results, or ease of implementation. As a hobbyist, I've not yet found the time to work with them to find out, reliably. Much to my shame.