PHP Classes

How to bind MySQL data to dynamically created Form Elements

Recommend this page to a friend!

      Top level forums  >  PHP Specialists  >  General  >  How to bind MySQL data to...  
Subject:How to bind MySQL data to...
Summary:Bind fetched database values to dynamically generated form field
Author:Marcellinus Okeke
Date:2013-08-01 00:44:09
Update:2013-08-01 00:57:23

  1. How to bind MySQL data to...   Reply   Report abuse  
Picture of Marcellinus Okeke Marcellinus Okeke - 2013-08-01 00:57:24
I have some sets of form elements created dynamically which I use on page creation.

<?php for ($i=0;$i<6;$i++){ ?>
<div id="hold">
<input type="text" name="slinks<?php echo $i; ?>" value="<?php echo $slinks[$i]; ?>" placeholder="Input Link Name" />
<select name="cat<?php echo $i; ?>">
<option value="" selected="selected">Select Category</option>
<option value="1">Item 1</option>
<option value="2">Item 2</option>
<option value="3">Item 3</option>
<div class="clear"></div>
<?php } ?>

I am able to insert the values into my MySQL database. I want to populate values into these fields on page edit. My problem is how to bind values into the form elements assuming my database fetched sample values as:

$query = $dhb->prepare("SELECT * FROM table2 WHERE $col = ?");
$amt = $query->rowCount();
$rows = $query->setFetchMode(PDO::FETCH_NUM);
while($z = $query->fetch()){
And the output from print_r()

[0] => 3
[1] => Item3
[1] => 1
[2] => Item1
[0] => 2
[1] => Item2