Just as the subject says, the record won't update after I edit it via the form. I've been working my way through the webmonkey tutorial and I'm stuck on the following code, some of which I've already edited with isset, GET, POST, and $_SERVER['PHP_SELF'] to fix the usual problems. I also tried putting apostrophes in the "$sql = "UPDATE..." line variables thinking that might work, but it produced an error so I took them out. I've tried all sorts of different code and scoured the net, but no luck. Clicking the link brings up the form with the data in it, but after editing and submitting it just goes back to the original page. TIA. (My first post here.)
<?php
$db = mysql_connect("localhost", "root");
mysql_select_db("mydb",$db);
if (isset($_GET['id'])) {
if (isset($_POST['submit'])) {
$sql = "UPDATE employees SET first=$_POST[first],last=$_POST[last],address=$_POST[address],position=$_POST[position] WHERE id=$_POST[id]";
$result = mysql_query($sql);
echo "Thank you! Information updated.\n";
} else {
// query the DB
$sql = "SELECT * FROM employees WHERE id=$_GET[id]";
$result = mysql_query($sql);
$myrow = mysql_fetch_array($result);
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']?>">
<input type=hidden name="id" value="<?php echo $myrow["id"] ?>">
First name:<input type="Text" name="first" value="<?php echo $myrow["first"] ?>"><br>
Last name:<input type="Text" name="last" value="<?php echo $myrow["last"] ?>"><br>
Address:<input type="Text" name="address" value="<?php echo $myrow["address"] ?>"><br>
Position:<input type="Text" name="position" value="<?php echo $myrow["position"] ?>"><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", $_SERVER['PHP_SELF'], $myrow["id"], $myrow["first"], $myrow["last"]);
}
}
?>