I've searched through the threads and I can't find what I am looking for exactly. I apologise if I'm posting something already resolved for someone else.
I have a page for browsing the contents of a database. An option for each of those records is to update/edit. The user clicks an edit button and a new page is called using $_GET to specify the record id and query the database for all fields in that record. I want my form to be populated with those fields so I can edit the data. This is the code so far:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<?php
//from the url, get the id
$id = $_GET["id"];
if (!$id)
{
die("Variable id not defined. Script terminating.");
}
//connect to the database
$con = mysql_connect("localhost","root");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
//select Jokes database
$db=mysql_select_db("jokes", $con) or die(mysql_error());
//define the query
$sql="SELECT * FROM data WHERE id=".$id."";
echo "<head>";
echo " <title>Editing record id=".$id."</title>";
echo "</head>
";
echo "<body>
";
$result = mysql_query($sql, $con) or die('Could not connect: ' . mysql_error());
If (!$result) {
die('Result not returned:' . mysql_error());
}
//close the connection
mysql_close();
while ($list = mysql_fetch_array($result)){
echo "<form ENCTYPE=\"multipart/form-data\" action=\"".$_SERVER['PHP_SELF']."\" method=\"post\">
";
echo "<input type=\"text\" value=\"".$list['category']."\" maxlength=\"64\" name=\"category\" size=\"25\" /><br />
";
echo "<input type=\"text\" value=\"".$list['title']."\" maxlength=\"128\" name=\"category\" size=\"25\" /><br />
";
echo "<textarea cols=\"80\" rows=\"10\" wrap=\"ON\" name=\"body\">
$list['localstyles']</textarea><br />
";
echo "<textarea cols=\"80\" rows=\"15\" wrap=\"ON\" name=\"body\">
$list['body']</textarea><br />
";
echo "<button type=\"submit\">Update</button>
";
echo "</form>";
}
?>
</body>
</html>
The latter part in the while {} statement is the problem. I've done the one quoted above and variations that have provided the form but not the data associated with the variables. Inspecting the HTML results displays value="" in each of the forms tags. I know the array is being returned for testing I have done using print_r(mysql_fetch_array($result))
displays the data.
I realise there may be several ways to do this and would like help as to the most efficient that will set-up the form and populate it with the existing array of data. I know the following:
- I'm only going to have one row to work with. By querying on the id, it's only going to return one record.
- To iterate the data, I'll need to do it using an inline method like the code quoted above or assign it to variables using mysql_result before the form, then include each of those variables in the forrm.
- For defining the form, I can do it the way it's quoted or use a method using
echo <<<FORMENT <form action = "{$_SERVER['PHP_SELF']}" method = "post"> quoted form text and textareas as I would in html </form> FORMENT;
I picked up that method for creating a form for data entry on a tutorial but I don't know how to make it work to populate the fields with existing data. As well, I can't find documentation for that
echo <<< FORMENT ... FORMENT;
syntax. The site I obtained it from has since removed its tutorials and gone over to a forum only format.
I know that I will still have code to write for updating the record once the Update button is clicked and I know that part.
If you can't tell, I'm a newbie to all of this. I would really appreciate on this and thank you for reading this far.
Dan