View Full Version : How to populate php/html form with MySQL data
peterv
06-12-2009, 10:13 PM
I am learning PHP/MySQL on my own. I've got a MySQL database and a few php/html forms that work fine. What I want to do, but haven't been able to figure out, is to display a form so I can enter a key value to retrieve a record from the database and display that data in the form. I then want to be able to modify 1 or more fields and then update the record in the database. I think I can figure out how to do the update, but I can't figure out how to populate the form with the selected record's data. Anyone have any examples of how to do this? I'd appreciate the help.
n1tr0b
06-13-2009, 02:15 AM
This is the most simplest i know..
<?php
$con = mysql_connect("localhost","user","pass");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("my_db", $con);
$result = mysql_query("SELECT * FROM field");
while($row = mysql_fetch_array($result))
{
echo '<form action="">';
echo '<select name="nameofname :D">';
echo '<option value=". $row['nameoffield'] .">". $row['nameoffield']. "</option>';
echo '</form>';
}
mysql_close($con);
?>
heavensgate15
06-13-2009, 03:08 AM
Hmmm.. What you mean is that you display the data from the database in a form manner? If that so, then try something like this:
<?php
include('dbconnect.php');
$data = "select * from table_name where ID = 100";
$query = mysql_query($data);
$data2 = mysql_fetch_array($query);
?>
<html>
<head>
<title></title>
</head>
<body>
// form to display record from database
<form>
Name: <input type="text" value="<?php echo $data2[name]?>"/> </br>
age: <input type="text" value="<?php echo $data2[age]?>"/> </br>
hobby: <input type="text" value="<?php echo $data2[hobby]?>"/>
</form>
</body>
</html>
forum_amnesiac
06-13-2009, 01:41 PM
This is very simple to do with 3 separate scripts:
The first 1 here is called Form.htm and asks for what record to find:
Form.htm
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
</head>
<body>
<!-- form to get key detail of record in database -->
<form name="form" method="POST" action="form1.php">
Keyfield <input type="text" name="search"> <br><br>
<input type="submit" value="submit">
</form>
</body>
</html>
The second script is called Form1.php, it is called by Form.htm, and displays the relevant record and allows changes to be made.
Form1.php
<?php
$connection = mysql_connect('XXXXX','XXXXXXX','XXXXXXX') or die ("Couldn't connect to server.");
$db = mysql_select_db('XXXXXXXX', $connection) or die ("Couldn't select database.");
$search=$_POST['search'];
$data = 'SELECT * FROM `table_name` WHERE `ID` = "'.$search.'"';
$query = mysql_query($data) or die("Couldn't execute query. ". mysql_error());
$data2 = mysql_fetch_array($query);
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
</head>
<body>
<!-- form to display record from database -->
<form name="form" method="POST" action="form2.php">
Name: <input type="text" name="namefield" value="<?php echo $data2[name]?>"/> <br>
age: <input type="text" name="agefield" value="<?php echo $data2[age]?>"/> <br>
hobby: <input type="text" name="hobbyfield" value="<?php echo $data2[hobby]?>"/><br><br>
<input type="hidden" name="keyfield" value="<?php echo $search?>">
<input type="submit" value="submit">
</form>
</body>
</html>
The third script is called Form2.php, it is called by Form1.php, it updates the record and redisplays the entered data.
Form2.php
<?php
$connection = mysql_connect('XXXXX','XXXXXXX','XXXXXXX') or die ("Couldn't connect to server.");
$db = mysql_select_db('XXXXXXXX', $connection) or die ("Couldn't select database.");
$Key=$_POST['keyfield'];
$Name=$_POST['namefield'];
$Age=$_POST['agefield'];
$Hobby=$_POST['hobbyfield'];
$data = "UPDATE `table_name` SET name='$Name', age='$Age', hobby='Hobby' WHERE ID=".'"'.$Key.'"';
$query = mysql_query($data) or die("Couldn't execute query. ". mysql_error());
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
</head>
<body>
<!-- display the changed record from database -->
Name: <?php echo $Name?><br>
Age: <?php echo $Age?> <br>
Hobby: <?php echo $Hobby?><br><br>
</body>
</html>
I haven't tested this code but it is hopefully ok and will give you an idea of how you could do what you have requested.
I have used a hidden input field to pass the "key" field between Form1.php and Form2.php because sometimes this may not a field you want to alter, just a field you want to search by, eg an ID number
heavensgate15
06-14-2009, 01:34 PM
Ya, that was it, ehehhe... hey forum_amnesiac, why do you use $_POST['value'] instead of $_POST['search'] in Form1.php?
forum_amnesiac
06-14-2009, 01:58 PM
typo, as I said I hadn't tested it.
I'll correct it now
heavensgate15
06-16-2009, 11:19 AM
ahw.. I thought it was another knowledge to earn.. haahahhaha:D
mukeshgulia
07-16-2009, 09:28 AM
hi all,
I want to do the similar operation as described above, but in a different manner.
My form comprises of 1000's html text boxes. User will enter 2 or 3 fields and then on a button/submit a php script will find thr record in the database using these fields entered by the user.
Record reterived by the user will populate all the other fields in the form.
I hope u understand what I mean? I do not want to jump to any other page.
Moreover, my form already have a submit button which saves the data in mysql. Do I need to create a different form for search and give the search textboxes there only. Or I can use the same form.
Please suggest. I am struck for two days now because of this.
Thanks in advance.
vinaykr
08-26-2015, 04:21 PM
hi all,
I want to do the similar operation as described above, but in a different manner.
My form comprises of 1000's html text boxes. User will enter 2 or 3 fields and then on a button/submit a php script will find thr record in the database using these fields entered by the user.
Record reterived by the user will populate all the other fields in the form.
I hope u understand what I mean? I do not want to jump to any other page.
Moreover, my form already have a submit button which saves the data in mysql. Do I need to create a different form for search and give the search textboxes there only. Or I can use the same form.
Please suggest. I am struck for two days now because of this.
Thanks in advance.
How can this be acheived using PDO
Beverleyh
08-26-2015, 06:16 PM
vinaykr, Your question isn't clear - neither is what you are referring to in the post from 6 years ago.
Please start your own thread, clearly stating your question and clearly describing what you're trying to achieve. Sample code/data would be helpful.
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.