Q:

Data from mysql database in to text fields via php?

I've created a database with a table called pages in it.
Within this table there are fields called pages_id, pagename, pagedesc, pagekey and pagecont.
How can I pull the data from these fields in the database, in to editable text fields? I've tried below, but my code isn't working :(
<?php
// Make a MySQL Connection
mysql_connect("####", "####", "####") or die(mysql_error());
mysql_select_db("####") or die(mysql_error());

// Get all the data from the "example" table
$result = mysql_query("SELECT * FROM pages") 
or die(mysql_error());  

?>
<p>Page title</p><br />
<?php echo"<input name=\"pagename\" type=\"text\" id=\"pagename\" value=\"" .$pagename. "\">"; ?>

<p>Keywords</p><br />
  <?php echo"<input name=\"pagekey\" type=\"text\" id=\"pagekey\" value=\"" .$pagekey. "\">"; ?>

<p>Description</p><br />
<?php echo"<input name=\"pagedesc\" type=\"text\" id=\"pagedesc\" value=\"" .$pagedesc. "\">"; ?>

<p>Content</p><br />
<?php echo "<textarea name=\"pagecont\" cols=\"120\" rows=\"20\" id=\"pagecont\" >".$pagecont."</textarea>";?>

A:

You haven't fetched a row from your query result, and are outputting undefined variables. The proper sequence should be:
$result = mysql_query($sql) or die(mysql_error()); // run the query
$row = mysql_fetch_assoc($row); // fetch a result row

echo $row['name_of_field']; // output one of the result row's data fields.

Also note that when you're outputting data into an HTML form as you are, you have to make sure that nothing in the data you're outputting will 'break' the form. e.g. If the output text contains a ", and you're using those quotes around your form field's attributes, you'll "break" the html. best practice for output-to-html is to use:
<input type="text" name="somefield" value="<?php echo htmlspecialchars($row['name_of_field']) ?>" />

