Page 1 of 1

populate a text area

Posted: Fri Sep 05, 2003 11:53 am
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>

Posted: Fri Sep 05, 2003 11:55 am
by AVATAr
use it like this:
<textarea name="file_desc" cols='50' rows='8' ><?=$row[0]?></textarea>

Re: populate a text area

Posted: Fri Sep 05, 2003 12:48 pm
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;
?>