Hi,
I'm having a problem with passing some information from one function to another via a switch statement.
The code I'm using is below, I'm trying to do this:
with the lookup_user_form() function you enter a user name via a form, pressing submit the value entered in the $_POST["username"] is query against the database in the function lookup_user(), via a switch statement.
lookup_user(), then gives the user the option to edit their details and once again using the submit via a switch the details are sent to the edit_user() function.
My probelm is that in the lookup_user() function a run a SQL query and load the result into a array, $user_infomation, I can't seem to pass that information to the edit_user() using my current method.
I'm new to PHP and would appreate any advice, sorry if this post is a bit long.
cheers
Paul
<?php
/*
Form for uses to enter username
*/
function lookup_user_form()
{
echo "<form name=\"form3\" method=\"post\" action=\"\">";
echo "<INPUT TYPE=\"HIDDEN\" NAME=\"action\" VALUE=\"lookup_user\">";
/* HTML form stuff detailed for the sake of brevaty */
echo "<td><input type=\"text\" name=\"username\"></td>";
/* HTML form stuff detailed for the sake of brevaty */
echo "<input type=\"submit\" name=\"Submit4\" value=\"Submit\">";
/* HTML form stuff detailed for the sake of brevaty */
echo "</form>";
}
/*
Looks up user details from database, if username in db displays form with values from user records. If does not exist error message displayed
*/
function lookup_user()
{
if(check_user ($_POST["username"]))
{
$get_user_details = "SELECT username, email, icq, aim, msn, yahoo FROM users WHERE username =\"".$_POST["username"]."\"";
$result = mysql_query($get_user_details);
$user_information = mysql_fetch_array($result);
echo "<INPUT TYPE=\"HIDDEN\" NAME=\"action\" VALUE=\"edit_user\">";
/* HTML form stuff detailed for the sake of brevaty */
echo "<input type=\"submit\" name=\"Submit4\" value=\"Submit\">";
/* HTML form stuff detailed for the sake of brevaty */
}
else
{
echo "user not in database";
}
}
/*
Function that counts number of rows returned, as a check if username in db
*/
function check_user($name)
{
$query = "SELECT username FROM users WHERE username = \"".$_POST["username"]."\"";
$result = mysql_query($query);
if(!mysql_num_rows($result)) return 0;
else return 1;
}
/*
function that will edit user data in db, unable to pass data from function lookup
_user()
*/
function edit_user()
{
echo "query ".$user_information["username"]."<br>";
echo "post ".$_POST["username"]."<br>";
if ($_POST["username"] != $user_information["username"])
{
echo "different user name";
}
elseif ($_POST["username"] == $user_information["username"])
{
echo "no user name update";
}
}
Global $action;
$link_id= mysql_connect("localhost", "testdb", "password");
mysql_select_db("epsilonfleet", $link_id);
switch($action)
{
case "lookup_user";
lookup_user();
break;
case "edit_user";
edit_user($user_information);
break;
default:
lookup_user_form();
break;
}
echo "<br><a href=\"admin.php\">Admin menu</a>";
?>