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>";?>
You haven’t fetched a row from your query result, and are outputting undefined variables. The proper sequence should be:
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: