Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I populate a PHP form with MySQL data.

Status
Not open for further replies.

peterv12

Technical User
Dec 31, 2008
108
US
I need to know how to get data selected from a MySQL database into the fields of a PHP form. I've searched the web and found nothing that helped. A coding example would be really helpful, or if you can point me to documentation that would help.
Thanks....
 
Basically all you need to do is output the data in the value fields of the elements of the form.



As an example suppose you have a table that look like:
Code:
id f_name l_name age
1   John  Smith  25
2   Paul  Jones  32
3   Peter Jane   19

Code:
[green]\\Connect to DB[/green]
$conn=mysql_connect("dbserver" , "user", "Password") or die(mysql_error());

$db=mysql_select_db("mydb",$conn) or die(mysql_error());

$query="SELECT *FROM mytable WHERE id=1";

$res=mysql_query($query) or die(mysql_error());

$row=mysql_fetch_array($res);

echo '<form name="myform" action="process.php" method="POST">';

echo '<input type=text name="id" value="' . $row['id'] . '">';

echo '<input type=text name="first" value="' . $row['f_name'] . '">';
echo '<input type=text name="last" value="' . $row['l_name'] . '">';

echo '<input type=text name="age" value="' . $row['age'] . '">';
echo '<input type=submit name=send value="Submit Form">';
echo '</form>';




----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Check this CRUD (Create, Read, Update, Delete) I put together some time ago ...
The code could be downloaded here



You will need a MySQL DB named "test". Use
Code:
--
-- Table structure for table `members`
--

CREATE TABLE IF NOT EXISTS `members` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `firstname` varchar(50) COLLATE latin1_bin DEFAULT NULL,
  `lastname` varchar(50) COLLATE latin1_bin DEFAULT NULL,
  `phone` varchar(50) COLLATE latin1_bin DEFAULT NULL,
  UNIQUE KEY `id` (`id`),
  FULLTEXT KEY `lastname` (`lastname`),
  FULLTEXT KEY `phone` (`phone`),
  FULLTEXT KEY `firstname` (`firstname`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 COLLATE=latin1_bin AUTO_INCREMENT=24 ;
to create the table named members.

This CRUD should point you in the right direction on how to manipulate data in/out of MySQL tables. Further, it uses classes in its simplest form so it will also serve as a stepping stone towards OOP as well.

Hope you like it!



--
SouthBeach
The good thing about not knowing is the opportunity to learn - Yours truly, 2008.
 
to be honest you might want to get hold of a basic introduction such as the dummies guide.
 
Thanks guys! I'll dig into this and let you know how it works!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top