Forum Moderators: mack
<html>
<body>
<?php
//from post
$staffid = $_POST['staffmember'];
if($staffid == "0")
{
echo ("error");
}
$db = mysql_connect("localhost", "james", "d3st");
mysql_select_db("test",$db);
if ($staffid) {
if ($submit) {
$sql = "UPDATE employees SET firstname='$first',surname='$last',email='$address',title='$position' WHERE ID=$staffid";
$result = mysql_query($sql);
echo "Thank you! Information updated.\n";
} else {
// query the DB
$sql = "SELECT * FROM employees WHERE ID=$staffid";
$result = mysql_query($sql);
$myrow = mysql_fetch_array($result);
?>
<form method="post" action="<?php echo $PHP_SELF?>">
<input type=hidden name="ID" value="<?php echo $myrow["ID"]?>">
First name:<input type="Text" name="first" value="<?php echo $myrow["firstname"]?>"><br>
Last name:<input type="Text" name="last" value="<?php echo $myrow["surname"]?>"><br>
Address:<input type="Text" name="address" value="<?php echo $myrow["email"]?>"><br>
Position:<input type="Text" name="position" value="<?php echo $myrow["title"]?>"><br>
<input type="Submit" name="submit" value="Enter information">
</form>
<?php
}
} else {
// display list of employees
$result = mysql_query("SELECT * FROM employees",$db);
while ($myrow = mysql_fetch_array($result)) {
printf("<a href=\"%s?id=%s\">%s %s</a><br>\n", $PHP_SELF, $myrow["ID"], $myrow["firstname"], $myrow["surname"]);
}
}
?>
</body>
</html>
echo "<p>First name: $first</p>";
Another thing you might run into problems with later is that $staffid is not surrounded by quotes as are your other variables in the update query.
Oh yeah, one other thing in the form action I have always seen PHP_SELF being accessed through the $_SERVER global variable such as $_SERVER['PHP_SELF'], which I guess doesn't matter if you have register_globals enabled. Although, php recommends setting register_globals to off for newer versions of php.
lol I've edited this thing like 3 times already...
One other suggestion that may help you later for mysql is to put an or die("This is the message that prints if the operation fails."); function after the mysql operations.
So you could do something like:
$update = mysql_query($query) or die("Query failed to run.");