Jump to content

Populating form with record from MySQL db


ninimac

Recommended Posts

Hi hope someone can help me.

 

I'm creating a form which allows a use to update a record in an SQL database. I want to populate the form with the current data in that record. I've got it working fine for the input boxes and text areas but don't know how to do it for the drop down box (first in the form).

 

Any help is greatly appreciated! :-\

 

$query1 = "SELECT * FROM articles where id=".$rowid."";

$getvals = mysql_query($query1);
while($row = mysql_fetch_array($getvals, MYSQL_ASSOC)){
?>
<form method="post">
	<table width="400" border="0" cellspacing="1" cellpadding="2">
		<tr> 
			<td width="100">Section:</td>
			<td>
				<select name="a_section" id="a_section">
				<option value="gw">Global Warming</option>
				<option value="ol">Ozone Layer</option>
				<option value="ae">Alternative Energy</option>
				</select>
			</td>
		</tr>

		<tr> 
      			<td width="100">Title:</td>
			<td><input name="a_title" type="text" id="a_title" value= "<?php echo htmlentities($row['a_title']); ?>"> </td>
		</tr>

		<tr> 
      			<td width="100">Synopsis:</td>
			<td><TEXTAREA name="a_synopsis" type="text" id="a_synopsis" rows="6" cols="32" value=""><?php echo htmlentities($row['a_synopsis']);?></TEXTAREA></td>
		</tr>

		<tr> 
      			<td width="100">Article Text:</td>
			<td><TEXTAREA name="a_text" type="text" id="a_text" rows="6" cols="32" value=""><?php echo htmlentities($row['a_text']);?></TEXTAREA></td>
		</tr>

		<tr> 
      			<td width="100">Image:</td>
			<td><input name="a_image" type="text" id="a_image" value="<?php echo $row['a_image'];?>"></td>
		</tr>

		<tr> 
      			<td width="100">Thumbnail Image:</td>
			<td><input name="a_thumb_image" type="text" id="a_thumb_image" value="<?php echo $row['a_thumb_image'];?>"></td>
		</tr>

		<tr> 
      			<td width="100">Author Name:</td>
			<td><input name="a_author" type="text" id="a_author" value="<?php echo $row['a_author'];?>"></td>
		</tr> 


		<tr> 
			<td width="100"> </td>
			<td><input name="add" type="submit" id="add" value="Add Article"></td>
		</tr>
	</table>
</form>
<?php
}

Link to comment
Share on other sites

Just make sure the variable can be declared where you want, you can either do this by just wrapping the variable in php tags where-ever it is in the code or don't drop out of php after the select query.

 

I prefer staying in php and echoing all the html otherwise you might end up going in and out of php all over the page...

This might not be the best method but it works for me!

 


<?php
$sql="SELECT * FROM table WHERE field = 'filter'";
$rs=odbc_exec($conn,$sql);
if (!$rs)
      {exit("Error in SQL");}

while (odbc_fetch_row($rs))
{
     $Field1=odbc_result($rs,"field1");
     $Field2=odbc_result($rs,"field2");
     $Field3=odbc_result($rs,"field3");

echo "

<form action='your_target' method='POST'>

<select name='select_box' id='select_box'>
               <option value='$Field1'>$Field1</option>
               <option value='ol'>Ozone Layer</option>
               <option value='ae'>Alternative Energy</option>
               </select>

...and so on with the rest of your form...

</form>
";
}

 

Its not in your code but I hope that helps.

Link to comment
Share on other sites

@fearpig: your code should be:

 

<?php
$sql="SELECT * FROM table WHERE field = 'filter'";
$rs=odbc_exec($conn,$sql);
if (!$rs)
      {exit("Error in SQL");}

echo "<form action='your_target' method='POST'>";
while (odbc_fetch_row($rs))
{
     $Field1=odbc_result($rs,"field1");
     $Field2=odbc_result($rs,"field2");
     $Field3=odbc_result($rs,"field3");

echo "
<select name='select_box' id='select_box'>
               <option value='$Field1'>$Field1</option>
               <option value='ol'>Ozone Layer</option>
               <option value='ae'>Alternative Energy</option>
               </select>

...and so on with the rest of your form...
";
}

echo "</form>";

 

however i strongly suggest you do not mix html and php as it gets very hard to maintain over time. This is called separating bussiness- from presentation logic, and i use the MVC pattern to accomplish that. However a simple example could be:

 

// index.php
<?php

// i like to use overloading it's very nice for use as a templace mechanism
class Template
{
  protected $_vars = array();
  
  public function __set($key, $value)
  {
    $this->_vars[$key] = $value;
  }
  
  public function __get($key)
  {
    if ($this->__isset($key)) {
      return $this->_vars[$key];
    }
  }
  
  public function __isset($key)
  {
    return (null !== $this->_vars[$key]);
  }
  
  public function __unset($key)
  {
    unset($this->_vars[$key];
  }
  
  public function render($script)
  {
    include_once($script);
  }
  
  public function __call($method, $args)
  {
    // does the helper exist?
  }
}

$tpl = new Template();

while (false != ($row = mysql_fetch_array($result, MYSQL_ASSOC))) {
  foreach ($row as $key => $value) {
    $tpl->{$key} = $value;
  }
}

$tpl->render('index.tpl');

?>


// index.tpl (this is also called a view, with it you can use view helpers, like populating a combo box)

<form action="" method="post" enctype="application/x-www-form-urlencoded">
<fieldset><legend>Account Details</legend>
<table>
<tr>
  <td><label for="name">Name</label>:</td>
  <td><?php echo $this->textField($this->name /* name refers to the table field */); ?></td>
</tr>
<tr>
  <td><label for="select">Country</label>:</td>
  <td><?php echo $this->comboBox($this->comboBoxDetails); ?></td>
</tr>
...
</table>
</fieldset>
</form>
</form>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.