I'll try to setup the situation as briefly as possible:
I have a simple single-table database that stores personal data in a spreadsheet like style. I made a custom page within WordPress that can display every person (every table row) in this non-WordPress database.
Since this is on WordPress and every desired visitor is already logged in, I am able to display info about the currently logged in WordPress user. I'm not sure exactly how it works but the variable is $current_user->display_name'
So I successfully combined the two. With a field I created in my custom database for each person's WordPress username - wp_user - I am able to display ONLY the data from my custom database that corresponds to the person who is already logged in to WordPress. Here is the code that works fine:
$db = mysql_connect("localhost", "XXXX", "XXXX");
mysql_select_db("XXXX",$db);
$query = "SELECT * FROM brotherhood WHERE wp_user='$current_user->display_name'";
$result = mysql_query ($query);
if (@mysql_num_rows($result))
{
print "<table border=\"1\" >\n";
print "<tr>
<td>First Name</td>
<td>Last Name</td>
<td>Copy of WordPress Display Name</td>
</tr>\n";
while($row = mysql_fetch_array($result)) {
print "<tr>\n";
print "<td>".$row['firstname']."</td>\n";
print "<td>".$row['lastname']."</td>\n";
print "<td>".$row['wp_user']."</td>\n";
print "</tr>\n";
}
print "</table>\n";
} else { echo "<p>Sorry, no records were found!</p>"; }
So that code above works fine for simply displaying. But my final goal is not to simply display the logged-in user's data (from my custom, non-WordPress database), but to allow them to edit their own data in that database. So now I switch gears...
I have some simple code that works fine for updating data from my custom database but I just need to modify it and I haven't been successful so far. This code below starts off by listing everybody in the database (each row). When you click on a person's name it then reloads the same page with a value for "id" within the url (like ?id=119), which then displays a form so that that the data for that chosen person is pre-populated in the form and can be edited and updated in the browser. The first thing I need to modify is that I don't need to first list everybody in the database. I want it to go immediately to the editng form. The second thing I need to modify is that the person's data that will be edited/updated needs to be determined by the user who's already logged in to WordPress and not the value of "id" within the URL (like ?id=119). I tried to modify this code several times but it didn't work, and hopefully somebody can tell me what I did wrong.
First the code as it worked in a different situation:
[code=php]<?php
$db = mysql_connect("localhost", "XXXXX", "XXXXX");
mysql_select_db("XXXXX",$db);
if ($id) {
if ($submit) {
$sql = "UPDATE brotherhood SET firstname='$firstname', lastname='$lastname', nickname='$nickname', graduationyear='$graduationyear' WHERE id=$id";
$result = mysql_query($sql);
echo "Thank you! Information updated.\n";
} else {
$sql = "SELECT * FROM brotherhood WHERE id=$id";
$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="firstname" value="<?php echo $myrow["firstname"] ?>"><br />
Last Name: <input type="Text" name="lastname" value="<?php echo $myrow["lastname"] ?>"><br />
Nickname: <input type="Text" name="nickname" value="<?php echo $myrow["nickname"] ?>"><br />
Graduation Year: <input type="Text" name="graduationyear" value="<?php echo $myrow["graduationyear"] ?>"><br />
<input type="Submit" name="submit" value="Process Information">
<?php
}
} else {
// display list again
$result = mysql_query("SELECT * FROM brotherhood ORDER BY lastname",$db);
while ($myrow = mysql_fetch_array($result)) {
printf("<a href=\"%s?id=%s\">%s %s</a><br />\n", $PHP_SELF, $myrow["id"], $myrow["lastname"], $myrow["firstname"]);
}
echo "<p><a href=\"/\">Click here</a> to go home.\n";
}
?>[/code]
OK, so that code above works fine in another location and for a different purpose. I now have to modify it to only edit/update the data of the person who's already logged in to WordPress, and make it have nothing to do with values passed along through the URL. Here's my best attempt below. I've added comments to tell you what I tried changing and why.
[code=php]<?php
$db = mysql_connect("localhost", "XXXXX", "XXXXX");
mysql_select_db("XXXXX",$db);
// before it said >>> if ($id) { <<< but I just removed this line
if ($submit) {
$sql = "UPDATE brotherhood SET firstname='$firstname', lastname='$lastname', nickname='$nickname', graduationyear='$graduationyear' WHERE wp_user='$current_user->display_name'";
// Above, I replaced >>> WHERE id=$id <<< and replaced it with what should designate the data that matches the logged in user
$result = mysql_query($sql);
echo "Thank you! Information updated.\n";
} else {
$sql = "SELECT * FROM brotherhood WHERE wp_user='$current_user->display_name'";
// Above, I replaced >>> WHERE id=$id <<< and replaced it with what should designate the data that matches the logged in user
$result = mysql_query($sql);
$myrow = mysql_fetch_array($result);
?>
<form method="post" action="<?PHP echo $PHP_SELF?>">
<!-- I don't know what that $PHP_SELF above does but I actually think this may be a reason my code isn't working. -->
<input type=hidden name="wp_user" value="<?php echo $myrow["wp_user"] ?>">
<!-- Above, I changed "id" to "wp_user" since that's what I'm using to determine which line to edit/update, but I could be wrong. Is this line above even needed? Seems redundant to me. -->
First Name: <input type="Text" name="firstname" value="<?php echo $myrow["firstname"] ?>"><br />
Last Name: <input type="Text" name="lastname" value="<?php echo $myrow["lastname"] ?>"><br />
Nickname: <input type="Text" name="nickname" value="<?php echo $myrow["nickname"] ?>"><br />
Graduation Year: <input type="Text" name="graduationyear" value="<?php echo $myrow["graduationyear"] ?>"><br />
<input type="Submit" name="submit" value="Process Information">
<?php
// I removed this bracket >>> } <<< because I think it goes with that "if" statement I already removed earlier.
// I removed this whole "display list again" section, from } else { to }
echo "<p><a href=\"/\">Click here</a> to go home.\n";
}
?>[/code]
The result of the code I tried above is that it shows me the form fields and they are properly pre-populated with the data that is already in the database. However when I hit the submit button ("Process Information") it just seems to reload the same page and disregards any changes I attempted to make, re-populating the form with the data that was already in the database. If it worked anything like the original code, it shouldn't have shown me the same form again, and instead it should have displayed the confirmation message of "Thank you! Information updated". But it didn't update the info or display the confirmation message. I think I'm close, but does anybody have any idea what I'm doing wrong here, or how to fix it. Thanks so much for any help.