PDA

View Full Version : updating a mysql record...


robojob
12-22-2005, 02:03 PM
hi i have this page which pulls information from the database into a form depending on what id is entered in the url for example update.php?id=4

this page works fine, what i cant figure out is how to then post this form on to a new page or to its self which actually updates any info changed on the form....

can someone help me out please.

this is the code for the form page...

<html>
<link rel="stylesheet" href="stylesheet.css" type="text/css">

<link rel="stylesheet" href="style.css" type="text/css">
<body bgcolor="#000099">
<?php
// listing script

// connect to the server
mysql_connect( 'localhost', 'root', '' )
or die( "Error! Could not connect to database: " . mysql_error() );

// select the database
mysql_select_db( 'directory' )
or die( "Error! Could not select the database: " . mysql_error() );

// get the id from the URL request
$id = $_REQUEST['id'];

// retrieve the row from the database
$query = "SELECT * FROM `directory` WHERE `id`='$id'";

$result = mysql_query( $query );

// print out the results
if( $result && $details = mysql_fetch_object( $result ) )
{
// print out the info
$business = $details -> business;
$address1 = $details -> address1;
$address2 = $details -> address2;
$address3 = $details -> address3;
$postcode = $details -> postcode;
$phone = $details -> phone;
$fax = $details ->fax;
$email = $details -> email;
$web = $details -> web


?>
<div align="center"><img src="images/details.gif" width="251" height="30"><br>
<br>
<form name="form1" method="post" action="update1.php">
<table border="0" cellpadding="3" cellspacing="3" class="style" width="100%">
<tr>
<th align="left" width="143">Business:</th>
<td align="left" width="839">
<input type="hidden" name="id" value="<?php echo($id) ?>">
<input type="text" name="business" value="<?php echo($business) ?>">
</td>
</tr>
<tr>
<th align="left" width="143" valign="top">Address:</th>
<td align="left" width="839">
<input type="text" name="address1" value="<?php echo($address1) ?>">
<br>
<input type="text" name="address2" value="<?php echo($address2) ?>">
<br>
<input type="text" name="address3" value="<?php echo($address3) ?>">
<br>
<input type="text" name="postcode" value="<?php echo($postcode) ?>">
</td>
</tr>
<tr>
<th align="left" width="143">Phone:</th>
<td align="left" width="839">
<?php echo($phone) ?>
</td>
</tr>
<tr>
<th align="left" width="143" height="32">Fax:</th>
<td align="left" width="839" height="32">
<?php echo($fax) ?>
</td>
</tr>
<tr>
<th align="left" width="143">Email:</th>
<td align="left" width="839">
<?php echo($email) ?>
</td>
</tr>
<tr>
<th width="143" height="23">Website:</th>
<td align="left" width="839" height="23">
<?php echo($web) ?>
</td>
</tr>
<tr>
<th align="left" width="143" height="23">&nbsp;</th>
<td align="left" width="839" height="23">
<input type="submit" name="Submit" value="Submit">
</td>
</tr>
</table>
</form>
<br>
</div>

<?php
}
else
{
die( "Error: Could not get contact from database. ".mysql_error() );
}

?>

</body>
</html>

degsy
12-22-2005, 02:17 PM
You need to grab the $_POST vars and insert them into the appropriate table.

Here is an example (using GET)
http://www.freewebmasterhelp.com/tutorials/phpmysql/7