populate a text area

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
sulen
Forum Commoner
Posts: 79
Joined: Wed Jul 09, 2003 4:55 pm
Location: los angeles
Contact:

populate a text area

Post by sulen »

I need to populate the text area of a form with data coming out of a mysql database. The code I am using is as under. Kindly help me out figuring whats wrong with it.

<?php
@ $db = mysql_pconnect("localhost", "root", "dbstuff3r");
if(!$db)
{
echo "error could not connect to the database. please try again later";
exit;
}

mysql_select_db("extranet");
$sql = "select file_desc from files where id='$id'";
$result = mysql_query($sql);

if(!$result)
{
echo"Cannot execute query";
}
?>
<html>
<head>
<title>Update Patch Description</title>
</head>
<body>
<?
while($row = mysql_fetch_row($result)) {
echo $row[0];
//the data shows up when I echo it here but does not show up in the textarea

?>

<form name='dupdate' method='post' action="<?php echo $_SERVER["PHP_SELF"];?>">
<div align="center">
<table width="70%">
<tr>
<td><b>Patch Description</b></td>
<td width="40%"><textarea name="file_desc" value="<?=$row[0]?>" cols='50' rows='8' ></textarea><br><br></td>
</tr>
<tr><td colspan=2><input type='submit' value='edit'></div></td></tr>
</table>
</div>
</form>
<?}?>
<br><br><p><a href='pedit.php'>Go back to patch edit page</a></p>
User avatar
AVATAr
Forum Regular
Posts: 524
Joined: Tue Jul 16, 2002 4:19 pm
Location: Uruguay -- Montevideo
Contact:

Post by AVATAr »

use it like this:
<textarea name="file_desc" cols='50' rows='8' ><?=$row[0]?></textarea>
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Re: populate a text area

Post by m3rajk »

i've done some things to make the code a little neater.changes such as

Code: Select all

$db = mysql_pconnect("localhost", "root", "dbstuff3r");
if(!$db)
{
echo "error could not connect to the database. please try again later";
exit;
}
to

Code: Select all

$db = mysql_pconnect("localhost", "root", "dbstuff3r") or die('error could not connect to the database. please try again later');
are equivalent that sometimes make the code seem friendlier to others

Code: Select all

<?php
@ $db = mysql_pconnect("localhost", "root", "dbstuff3r")or die('error could not connect to the database. please try again later');

mysql_select_db("extranet") or die('unable to select database'); // or added as a help if there's an issue
$sql = "select file_desc from files where id='$id'";
$result = mysql_query($sql) or die('Cannot execute query');

$textareainfo='';
while($row = mysql_fetch_row($result)) { 
textareainfo.="$row\n";
}

/* switched to heredoc format since it makes some things easier... such as the single variable in the action note: textarea does not take a value argument. any preset value is put between the <textarea></textarea> tags */
echo <<<END
<html>
<head>
<title>Update Patch Description</title>
</head>
<body>
<form name='dupdate' method='post' action="$_SERVER["PHP_SELF"]">
<div align="center">
<table width="70%">
<tr>
<td><b>Patch Description</b></td>
<td width="40%"><textarea name="file_desc" cols='50' rows='8' >$textareainfo</textarea><br><br></td>
</tr>
<tr><td colspan=2><input type='submit' value='edit'></div></td></tr>
</table>
</div>
</form>
<?}?>
<br><br><p><a href='pedit.php'>Go back to patch edit page</a></p>
END;
?>
Post Reply