Hi,
I am having a bit of trouble getting the following edit form to work. I think this is because of something to do with retrieving id's from the DB??
Any ideas?
Regards,
G
<html>
<head>
<title>Update Link</title>
</head>
<?php
$db = mysql_connect("localhost", "user", "password")
or die ("cant connect to the DB");
print ("You have connected to database successfully");
?><br><br><?php
mysql_select_db("test",$db);
?>
<h2>Update Link</h2>
<?PHP
processed when form is submitted back onto itself
if ($_SERVER['REQUEST_METHOD'] == "POST") {
$id = $_POST['id'];
$first = addslashes($_POST["first"]);
$last = addslashes($_POST["last"]);
$address = addslashes($_POST["address"]);
$dept = addslashes($_POST["dept"]);
# setup SQL statement
$sql = " UPDATE employees SET";
$sql .= " first = '$first', ";
$sql .= " last = '$last', ";
$sql .= " address = '$address', ";
$sql .= " dept = '$dept' ";
$sql .= " WHERE id = '$id' ";
$sql .= " LIMIT 1 ";
# execute SQL statement
$result = mysql_query($sql,$db);
# check for errors
if (mysql_error()) { print "Database Error: $sql " . mysql_error(); }
print "<p><b>Link Updated</b></p>\n";
}
else { # display edit form (not post method)
# setup SQL statement to retrieve link that we want to edit
$sql = " SELECT * FROM employees ";
$sql .= " WHERE id = $id ";
# execute SQL statement
$results = mysql_query($sql,$db);
// $result = mysql_query("SELECT * FROM employees",$db);
# retrieve values
$row = mysql_fetch_array($results);
$first = $row["first"];
$last = $row["last"];
$address = $row["address"];
$dept = $row["dept"];
?>
<form name="fa" action="editform2.php" method="post">
<table>
<tr><td><b>First: </b> </td><td><input type="text" name="first" value="<?=$first?>" size=40></td>
</tr>
<tr><td><b>last:</b> </td><td><input type="text" name="last" value="<?=$last?>" size=40></td></tr>
<tr><td><b>address: </b> </td><td><input type="text" name="address" value="<?=$address?>" size=40></td></tr>
<tr><td valign=top><b>dept: </b> </td><td> <textarea name="dept" rows=5 cols=40><?=$dept?></textarea></td></tr>
<tr><th colspan=2><p><input type="submit" value="update link"></p></th></tr>
</table>
</form>
<?php } php?>
</body>
</html>