p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

beginning_php thread: Chapter 13: userman.php example, two problems, PLEASE HELP!


Message #1 by "Chris Clough" <chris2k@h...> on Sun, 15 Sep 2002 03:09:02
I'm going through the userman.php exercise.. I get two constant problems. 
First is for the switch statement it keeps telling me that $action isnt 
defined which it isnt. But how do I get it to be defined or fix that error?

Next when running the script I cant get the Javascript function to open a 
new window. It gives me the error:

Line: 1
Char: 1
Error: Object Expected
Code: 0

Now here is my code and the function its calling. ANy help would be 
greatly greatly appreciated! Thanks, Chris

FUNCTION ITS CALLING: 
** From common_db.inc file

function html_header()
{
	global $new_win_width, $new_win_height;
	
	?>
	<html>
	<head>
   	<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">
   	<!--
   	function open_window(url) {
      		var NEW_WIN = null;
      		NEW_WIN = window.open 
("", "RecordViewer", "toolbar=no,width="<?php echo $new_win_width ?
>",height="<?php echo $new_win_height?
>",directories=no,status=no,scrollbars=yes,resize=no,menubar=no");
      		NEW_WIN.location.href = url;
   	}
   	//-->
   	</SCRIPT>
	<title>User Record Viewer</title>
	</head>
	<body>
	<?php
}

** The userman.php file:

<?php
//userman.php
include "./common_db.inc";

$link_id = db_connect();
mysql_select_db("sample_db");
$country_array = enum_options('usercountry', $link_id);
mysql_close($link_id);

function user_message($msg, $url='')
{
	html_header();

	if(empty($url))
	{
		echo "<SCRIPT>alert(\"$msg\");history.go(-1)</script>";
	}
	else
	{
		echo "<SCRIPT>alert
(\"$msg\");self.location.href='$url'</script>";
	}
	html_footer();
	exit;
}

function list_records()
{
	global $default_dbname, $user_tablename;
	global $default_sort_order, $default_order_by, $records_per_page;
	global $sort_order, $order_by, $cur_page;
	global $PHP_SELF;

	$link_id = db_connect($default_dbname);
	if(!$link_id) error_message(sql_error());

	$query = "SELECT count(*) FROM $user_tablename";

	$result = mysql_query($query);
	if(!$result) error_message(sql_error());

	$query_data = mysql_fetch_row($result);
	$total_num_user = $query_data[0];
	if(!$total_num_user) error_message('No User Found!');

	$page_num = $cur_page +1;

	$total_num_page = $last_page_num = ceil($total_num_user/
$records_per_page);

	html_header();

	echo "<CENTER><h3>$total_num_user users found. Displaying the page 
$page_num out of $last_page_num.</h3></center>\n";

	if(empty($order_by))
	{
		$order_by_str = "ORDER BY $default_order_by";
		$order_by = $default_order_by;
	}
	else $order_by_str = "ORDER BY $order_by";

	if(empty($sort_order))
	{
		$sort_order_str = $org_sort_order = $default_sort_order;
		$sort_order = 'DESC';
	}
	else
	{
		$sort_order_str = $org_sort_order = $sort_order;
		if($sort_order == 'DESC') $sort_order = 'ASC';
		else $sort_order = 'DESC';
	}

	if(empty($cur_page))
	{
		$cur_page = 0;
	}

	$limit_str = "LIMIT ". $cur_page * $records_per_page . ", 
$records_per_page";

	$query = "SELECT usernumber, userid, username FROM $user_tablename 
$order_by_str $sort_order_str $limit_str";
	$result = mysql_query($query);
	if(!$result) error_message(sql_error());
?>
	<DIV ALIGN="CENTER">
	<TABLE border="1" width="90%" CELLPADDING="2">
		<tr>
			<th width="25%" NOWRAP>
				<A HREF="<?php echo "$PHP_SELF?
action=list_records&sort_order=$sort_order&order_by=usernumber"; ?>">
				User Number
				</a>
			</th>
			<th width="25%" NO WRAP>
				<A HREF="<?php echo "$PHP_SELF?
action=list_records&sort_order=$sort_order&order_by=userid"; ?>">
				User ID
				</a>
			</th>
			<th width="25%" NOWRAP>
				<A HREF="<?php echo "$PHP_SELF?
action=list_records&sort_order=$sort_order&order_by=username"; ?>">
				User Name
				</a>
			</th>
			<TH WIDTH="25%" NOWRAP>Action</th>
		</tr>
	<?php

	while($query_data = mysql_fetch_array($result))
	{
		$usernumber = $query_data["usernumber"];
		$userid = $query_data["userid"];
		$username = $query_data["username"];
		echo "<TR>\n";
		echo "<TD width=\"25%\" 
ALIGN=\"CENTER\">$usernumber</td>\n";
		echo "<TD width=\"25%\" ALIGN=\"CENTER\">$userid</td>\n";
		echo "<TD width=\"25%\" ALIGN=\"CENTER\">$username</td>\n";
		echo "<TD width=\"25%\" ALIGN=\"CENTER\"><A 
HREF=\"javascript:open_window('$PHP_SELF?
action=view_record&userid=$userid');\">View</a> <A HREF=\"$PHP_SELF?
action=delete_record&userid=$userid\"onClick=\"return confirm('Are you 
sure?');\">Delete</a></td>\n";
		echo "</tr>\n";
	}
	?>
	</table>
	</div>
	<?php

	echo "<BR>\n";
	echo "<STRONG><CENTER>";

	if($page_num > 1)
	{
		$prev_page = $cur_page - 1;

		echo "<A HREF=\"$PHP_SELF?
action=list_records&sort_order=$org_sort_order&order_by=$order_by&cur_page
0\">[Top]</a>";
		echo "<A HREF=\"$PHP_SELF?
action=list_records&sort_order=$org_sort_order&order_by=$order_by&cur_pag=$
prev_page\">[Prev]</a> ";
	}

	if($page_num < $total_num_page)
	{
		$next_page = $cur_page + 1;
		$last_page = $total_num_page -1;

		echo "<A HREF=\"$PHP_SELF?
action=list_records&sort_order=$org_sort_order&order_by=$order_by&cur_page
$next_page\">[Next]</a> ";
		echo "<A HREF=\"$PHP_SELF?
action=list_records&sort_order=$org_sort_order&order_by=$order_by&cur_page
$last_page\">[Bottom]</a>";
		echo "</STRONG></CENTER>";
		html_footer();
	}
}

function delete_record()
{
	global $default_dbname, $user_tablename, $access_log_tablename;
	global $userid;

	if(empty($userid)) error_message('Empty User ID!');

	$link_id = db_connect($default_dbname);
	if(!link_id) error_message(sql_error());

	$query = "DELETE FROM $user_tablename WHERE userid = '$userid'";
	$result = mysql_query($query);
	if(!$result) error_message(sql_error());

	$num_rows = mysql_affected_rows($link_id);
	if($num_rows != 1) error_message("No such user: $userid");

	$query = "DELETE FROM $access_log_tablename WHERE userid 
= '$userid'";
	$result = mysql_query($query);

	user_message("All records regarding $userid have been trashed!");
}

function edit_record()
{
	global $default_dbname, $user_tablename, $access_log_tablename;
	global $userid, $new_userid, $userid, $username, $userpassword, 
$usercountry, $useremail, $userprofile, $registerdate, $lastaccesstime;

	if(empty($userid)) error_message('Empty User ID!');

	$link_id = db_connect($default_dbname);
	if(!link_id) error_message(sql_error());

	$field_str = '';

	if($userid != $new_userid) $field_str = " userid = '$newuserid', ";

	if(!empty($userpassword))
	{
		$field_str .= " userpassword = password('$userpassword') ";
	}

	$field_str .= " username = '$username', ";
	$field_str .= " usercountry = '$usercountry', ";
	$field_str .= " useremail = '$useremail', ";
  	$field_str .= " userprofile = '$userprofile', ";
  	$field_str .= " registerdate = '$registerdate', ";
  	$field_str .= " lastaccesstime = '$lastaccesstime' ";

  	$query = "UPDATE $user_tablename SET $field_str WHERE userid 
= '$userid'";

  	$result = mysql_query($query);
  	if(!$result) error_message(sql_error());

  	$num_rows = mysql_affected_rows($link_id);
  	if(!$num_rows) error_message("Nothing Changed!");

  	if($userid != $new_userid)
  	{
  		$query = "UPDATE $access_log_tablename SET userid 
= '$new_userid' WHERE userid = ' $userid'";
  		$result = mysql_query($query);
  		if(!result) error_message(sql_error());
  		user_message("All records regarding $userid have been 
changed!", "$PHP_SELF?action=view_record&userid=$new_userid");
  	}
  	else
  	{
  		user_message("All records regarding $userid have been 
changed!");
  	}
  }

function edit_log_record()
{
	global $default_dbname, $access_log_tablename;
	global $userid, $org_page, $new_page, $visitcount, $accessdate;

	if(empty($userid)) error_message('Empty User ID!');

	$link_id = db_connect($default_dbname);
	if(!link_id) error_message(sql_error());

	$field_str = '';

	$field_str .= " page = '$new_page', ";
	$field_str .= " visitcount = $visitcount, ";
	$field_str .= " accessdate = '$accessdate' ";

	$query = "UPDATE $access_log_tablename SET $field_str WHERE userid 
= '$userid' AND page = '$org_page'";
	$result = mysql_query($query);
	if(!$result) error_message(sql_error());

	$num_rows = mysql_affected_rows($link_id);
	if(!$num_rows) error_message("Nothing Changed!");

	user_message("All records regarding $userid have been changed!");
}

function view_record()
{
	global $default_dbname, $user_tablename, $access_log_tablename;
	global $country_array, $userid;
	global $PHP_SELF;

	if(empty($userid)) error_message('Empty User ID!');

	$link_id = db_connect($default_dbname);
	if(!link_id) error_message(sql_error());

	$query ="SELECT usernumber, userid, username, usercountry, 
useremail, userprofile, registerdate, date_format(registerdate, '%M, %e, %
Y') as formatted_registerdate, lastaccesstime, date_format
(lastaccesstime, '%M, %e, %Y') FROM $user_tablename WHERE userid 
= '$userid'";
	$result = mysql_query($query);
	if(!$result) error_message(sql_error());

	$query_data = mysql_fetch_array($result);
	$usernumber = $query_data["usernumber"];
	$userid = $query_data["userid"];
	$username = $query_data["username"];
	$usercountry = $query_data["usercountry"];
	$useremail = $query_data["useremail"];
	$userprofile = $query_data["userprofile"];
	$registerdate = $query_data["registerdate"];
	$formatted_registerdate = $query_data["formatted_registerdate"];
	$lastaccesstime = $query_data["lastaccesstime"];
	$formatted_lastaccesstime = $query_data
["formatted_lastaccesstime"];

	html_header();
	echo "<CENTER><h3>Record for User No.$usernumber - $userid
($username)</h3></center>";

?>

	<form method="post" action="<?php echo $PHP_SELF ?>">
		<input type="hidden" name="action" value="edit_record">
		<INPUT type="hidden" name="userid" value="<?php echo 
$userid ?>">
		<div align="center">
		<center>
			<table border="1" width="90%" cellpadding="2">
				<tr>
					<th width="30%" NOWRAP>User ID</th>
					<td width="70%"><input type="text" 
name="new_userid" VALUE="<?php echo $userid ?>" size="8" 
maxlength="8"></td>
				</tr>
				<tr>
					<th width="30%" NOWRAP>User 
Password</th>
					<td width="70%"><input type="text" 
name="userpassword" size="15"></td>
				</tr>
				<tr>
					<th width="30%" NOWRAP>Full 
Name</th>
					<td width="70%"><input type="text" 
name="username" value="<?php $username ?>" size="20"></td>
				</tr>
				<tr>
					<th width="30%" NOWRAP>Country</th>
					<td width="70%"><SELECT 
NAME="usercountry" SIZE="1">
<?php
					for($i=0; $i < count
($country_array); $i++)
					{
						if(!isset($usercountry) && 
$i == 0)
						{
							echo "<OPTION 
SELECTED VALUE=\"". $country_array[$i] . "\">" . $country_array
[$i] . "</option>\n";
						}
						else if($usercountry == 
$country_array[$i])
						{
							echo "<OPTION 
SELECTED VALUE=\"". $country_array[$i] . "\">" . $country_array
[$i] . "</OPTION>\n";
						}
						else
						{
							echo "<OPTION 
VALUE=\"". $country_array[$i] . "\">" .  $country_array
[$i] . "</OPTION>\n";
						}
					}
?>
					</select></td>
				</tr>
				<tr>
					<th width="30%" NOWRAP>Email</th>
					<td width="70%"><input type="text" 
name="useremail" SIZE="20" value="<?php echo $useremail ?>"></td>
				</tr>
				<tr>
					<th width="30%" NOWRAP>Profile</th>
					<td width="70%"><textarea rows="5" 
cols="40" NAME="userprofile"><?php echo htmlspecialchars($userprofile) ?
></textarea></td>
				</tr>
				<tr>
					<th width="30%" NOWRAP>Register 
Date</th>
					<td width="70%"><input type="text" 
name="registerdate" size="10" maxlength="10" value="<?php echo 
$registerdate ?>"><?php echo $formatted_registerdate ?></td>
				</tr>
				<tr>
					<th width="30%" NOWRAP>Last Access 
Time</th>
					<td width="70%"><input type="text" 
name="lastaccesstime" size="14" maxlength="14" value="<?php echo 
$lastaccesstime ?>"><?php echo $formatted_lastaccessdate ?></td>
				</tr>
				<tr>
					<th width="100%" NOWRAP 
COLSPAN=2><INPUT TYPE="SUBMIT" VALUE="Change User Record">&nbsp;<input 
type="reset" value="Reset"></th>
				</tr>
			</table>
		</center>
		</div>
	</form>
<?php

	echo "<HR SIZE=\"2\" WIDTH=\"90%\">\n";

	$query = "SELECT page, visitcount, accessdate, date_format
(accessdate, '%M, %e, %Y') as formatted_accessdate FROM 
$access_log_tablename WHERE userid = '$userid'";
	$result = mysql_query($query);
	if(!result) error_message(sql_error());
	if(!mysql_num_rows($result))
	{
		echo "<CENTER>No access log record for $userid 
($username).</center>";
	}
	else
	{
		echo "<center>Access log record(s) for $userid 
($username).</center>";
?>
	<div align="center">
	<center>
		<table border="1" width="90%" cellpadding="2">
			<tr>
				<th width="20%" NOWRAP>Page</th>
				<th width="20%" NOWRAP>Hits</th>
				<th width="30%" NOWRAP>Last Access</th>
				<th width="30%" NOWRAP>Action</th>
			</tr>
<?php
	while($query_data = mysql_fetch_array($result))
	{
		$page = $query_data["page"];
		$visitcount = $query_data["visitcount"];
		$accessdata = $query_data["accessdate"];
		$formatted_accessdate = $query_data
["formatted_accessdate"];

		echo "<form method=\"post\" action=\"$PHP_SELF\">";
		echo "<input type=\"hidden\" name=\"action\" 
value=\"edit_log_record\">";
		echo "<input type=\"hidden\" name=\"userid\" 
value=\"$userid\">";
		echo "<input type=\"hidden\" name=\"org_page\" 
value=\"$page\">";
		echo "<tr>\n";
		echo "<td width=\"20%\"><input type=\"text\" 
name=\"new_page\" size=\"30\" value=\"$page\"></td>\n";
		echo "<td width=\"20%\" ALIGN=\"CENTER\"><INPUT 
type=\"text\" name=\"visitcount\" size=\"3\" value=\"$visitcount\"</td>\n";
		echo "<td width=\"30%\" ALIGN=\"CENTER\"><input 
type=\"text\" name=\"accessdate\" size=\"14\" maxlength=\"14\" 
VALUE=\"$accessdate\"><BR>$formatted_accessdate</td>\n";
		echo "<td width=\"30%\" ALIGN=\"CENTER\"><input 
type=\"submit\" value=\"Change\"><input type=\"RESET\" 
VALUE=\"Reset\"></td>\n";
		echo "</tr>\n";
		echo "</form>\n";
	}
?>
		</table>
	</center>
	</div>
<?php
	}
	html_footer();
}

switch($action)
{
	case "edit_record":
		edit_record();
		break;
	case "edit_log_record":
		edit_log_record();
		break;
	case "delete_record":
		delete_record();
		break;
	case "view_record":
		view_record();
		break;
	default:
		list_records();
		break;
}

?>


Message #2 by "Nikolai Devereaux" <yomama@u...> on Mon, 16 Sep 2002 11:21:40 -0700
Read through the archives of this and other p2p php lists.  You'll find that
there's at least 1 thread per week about the user mgr in chapter 13.

Do any of these other posts help you solve your problem?  It's my guess that
once your PHP code is straightened out, your javascript will start to work,
too.


nik


  Return to Index