I’m trying to create a several page survey that is saved each time they progress to the next page. If they then leave and go back to the start, all the information is auto-filled in the fields.
First I select all the previously saved data:
$survey = $_GET['survey'];
$query = "SELECT * FROM data WHERE id='{$survey}'";
$result = mysql_query($query);
$row = mysql_fetch_array($result);
Then later on I use a loop to fetch all the questions on a page with information auto-filled if it’s present in the $row array.
$query = "SELECT * FROM questions WHERE id in (1,2,3)";
$result = mysql_query($query);
while ($items = mysql_fetch_array($result)){
if($items['type'] == "text"){
$value = $items['name']; ?>
<p><?php echo $items['question']; ?> <input type="text" id="<?php echo $items['name']; ?>" name="<?php echo $items['name']; ?>" value="<?php echo $row['{$value}']; ?>" /></p>
<?php }
}
An example of the tables:
QUESTIONS
id,type,name,question
1,text,first_name,What is your first name?
2,text,last_name, What is your last name?
DATA
id,first_name,last_name
1,John,Doe
Everything seems to be working except for:
value="<?php echo $row['{$value}']; ?>"
Thanks in advance,
Ditch the quotes, they make it a string literal instead of a variable. (brackets probably aren’t necessary either):