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
HTML Code:
<!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 Code:
<?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 Code:
<?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
Bookmarks