Need help on my php and mysql code
Posted: Wed Dec 31, 2003 2:30 pm
i have try to use this in my website, but i really duno what is the tables which need to create in mysql database.
can kindly ppl pls help me.............i had try to fix it in 4 days already, even how many times i try to create the database, it seem liek i got soemthg which missing..below there is my code
reg.php
Code: Select all
<?php
//register.php
include "./db.inc";
$link_id = db_connect();
mysql_select_db("sample_db");
$country_array = enum_options('usercountry', $link_id);
mysql_close($link_id);
function in_use($userid) {
global $user_tablename;
$query = "SELECT userid FROM $user_tablename WHERE userid = '$userid'";
$result = mysql_query($query);
if(!mysql_num_rows($result)) return 0;
else return 1;
}
function register_form() {
global $userid, $username, $usercountry, $useremail, $userprofile, $country_array;
global $PHP_SELF;
?>
<CENTER><H3>Create your account!</H3></CENTER>
<FORM METHOD="POST" ACTION="<?php echo $PHP_SELF ?>">
<INPUT TYPE="HIDDEN" NAME="action" VALUE="register">
<DIV ALIGN="CENTER"><CENTER><TABLE BORDER="1" WIDTH="90%">
<TR>
<TH WIDTH="30%" NOWRAP>Desired ID</TH>
<TD WIDTH="70%"><INPUT TYPE="TEXT" NAME="userid"
VALUE="<?php echo $userid ?>"
SIZE="8" MAXLENGTH="8"></TD>
</TR>
<TR>
<TH WIDTH="30%" NOWRAP>Desired Password</TH>
<TD WIDTH="70%"><INPUT TYPE="PASSWORD"
NAME="userpassword" SIZE="15"></TD>
</TR>
<TR>
<TH WIDTH="30%" NOWRAP>Retype Password</TH>
<TD WIDTH="70%"><INPUT TYPE="PASSWORD"
NAME="userpassword2" SIZE="15"></TD>
</TR>
<TR>
<TH WIDTH="30%" NOWRAP>Full Name</TH>
<TD WIDTH="70%"><INPUT TYPE="TEXT" NAME="username"
VALUE="<?php echo $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"></TEXTAREA></TD>
</TR>
<TR>
<TH WIDTH="30%" COLSPAN="2" NOWRAP>
<INPUT TYPE="SUBMIT" VALUE="Submit">
<INPUT TYPE="RESET" VALUE="Reset"></TH>
</TR>
</TABLE>
</CENTER></DIV>
</FORM>
<?php
}
function create_account() {
global $userid, $username, $userpassword, $userpassword2,
$usercountry, $useremail, $userprofile;
global $default_dbname, $user_tablename;
if(empty($userid)) error_message("Enter your desired ID!");
if(empty($userpassword)) error_message("Enter your desired password!");
if(strlen($userpassword) < 4 ) error_message("Password too short!");
if(empty($userpassword2))
error_message("Retype your password for verification!");
if(empty($username)) error_message("Enter your full name!");
if(empty($useremail)) error_message("Enter your email address!");
if(empty($userprofile)) $userprofile = "No Comment.";
if($userpassword != $userpassword2)
error_message("Your desired password and retyped password mismatch!");
$link_id = db_connect($default_dbname);
if(in_use($userid))
error_message("$userid is in use. Please choose a different ID.");
$query = "INSERT INTO user VALUES(NULL, '$userid',
password('$userpassword'), '$username',
'$usercountry', '$useremail',
'$userprofile', curdate(), NULL)";
$result = mysql_query($query);
if(!$result) error_message(sql_error());
$usernumber = mysql_insert_id($link_id);
html_header();
?>
<CENTER><H3>
<?php echo $username ?>, thank you for registering with us!
</H3></CENTER>
<DIV ALIGN="CENTER"><CENTER><TABLE BORDER="1" WIDTH="90%">
<TR>
<TH WIDTH="30%" NOWRAP>User Number</TH>
<TD WIDTH="70%"><?php echo $usernumber ?></TD>
</TR>
<TR>
<TH WIDTH="30%" NOWRAP>Desired ID</TH>
<TD WIDTH="70%"><?php echo $userid ?></TD>
</TR>
<TR>
<TH WIDTH="30%" NOWRAP>Desired Password</TH>
<TD WIDTH="70%"><?php echo $userpassword ?></TD>
</TR>
<TR>
<TH WIDTH="30%" NOWRAP>Full Name</TH>
<TD WIDTH="70%"><?php echo $username ?></TD>
</TR>
<TR>
<TH WIDTH="30%" NOWRAP>Country</TH>
<TD WIDTH="70%"><?php echo $usercountry ?></TD>
</TR>
<TR>
<TH WIDTH="30%" NOWRAP>Email</TH>
<TD WIDTH="70%"><?php echo $useremail ?></TD>
</TR>
<TR>
<TH WIDTH="30%" NOWRAP>Profile</TH>
<TD WIDTH="70%"><?php echo htmlspecialchars($userprofile) ?></TD>
</TR>
</TABLE>
</CENTER></DIV>
<?php
html_footer();
}
switch($action) {
case "register":
create_account();
break;
default:
html_header();
register_form();
html_footer();
break;
}
?>db.inc
Code: Select all
<?php
$dbhost = 'localhost';
$dbusername = 'phpuser';
$dbuserpassword = 'phppass';
$default_dbname = 'sample_db';
$default_sort_order = 'ASC';
$default_order_by = 'usernumber';
$records_per_page = 5;
$user_tablename = 'user';
$access_log_tablename = 'access_log';
$MYSQL_ERRNO = '';
$MYSQL_ERROR = '';
$new_win_width = 600;
$new_win_height = 400;
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
}
function html_footer() {
?>
</BODY>
</HTML>
<?php
}
function db_connect($dbname='') {
global $dbhost, $dbusername, $dbuserpassword, $default_dbname;
global $MYSQL_ERRNO, $MYSQL_ERROR;
$link_id = mysql_connect($dbhost, $dbusername, $dbuserpassword);
if(!$link_id) {
$MYSQL_ERRNO = 0;
$MYSQL_ERROR = "Connection failed to the host $dbhost.";
return 0;
}
else if(empty($dbname) && !mysql_select_db($default_dbname)) {
$MYSQL_ERRNO = mysql_errno();
$MYSQL_ERROR = mysql_error();
return 0;
}
else if(!empty($dbname) && !mysql_select_db($dbname)) {
$MYSQL_ERRNO = mysql_errno();
$MYSQL_ERROR = mysql_error();
return 0;
}
else return $link_id;
}
function sql_error() {
global $MYSQL_ERRNO, $MYSQL_ERROR;
if(empty($MYSQL_ERROR)) {
$MYSQL_ERRNO = mysql_errno();
$MYSQL_ERROR = mysql_error();
}
return "$MYSQL_ERRNO: $MYSQL_ERROR";
}
function error_message($msg) {
html_header();
echo "<SCRIPT>alert("Error: $msg");history.go(-1)</SCRIPT>";
html_footer();
exit;
}
function enum_options($field, $link_id) {
$query = "SHOW COLUMNS FROM user LIKE '$field'";
$result = mysql_query($query, $link_id);
$query_data = mysql_fetch_array($result);
if(eregi("('.*')", $query_data["Type"], $match)) {
$enum_str = ereg_replace("'", "", $match[1]);
$enum_options = explode(',', $enum_str);
return $enum_options;
} else return 0;
}
?>Code: Select all
<?php
//auth_user.php
include "./db.inc";
$register_script = "./reg.php";
function auth_user($userid, $userpassword) {
global $default_dbname, $user_tablename;
$link_id = db_connect($default_dbname);
$query = "SELECT username FROM $user_tablename
WHERE userid = '$userid'
AND userpassword = password('$userpassword')";
$result = mysql_query($query);
if(!mysql_num_rows($result)) return 0;
else {
$query_data = mysql_fetch_row($result);
return $query_data[0];
}
}
function login_form() {
global $PHP_SELF;
?>
<HTML>
<HEAD>
<TITLE>Login</TITLE>
</HEAD>
<BODY>
<FORM METHOD="POST" ACTION="<? echo $PHP_SELF ?>">
<DIV ALIGN="CENTER"><CENTER>
<H3>Please log in to access the page you requested.</H3>
<TABLE BORDER="1" WIDTH="200" CELLPADDING="2">
<TR>
<TH WIDTH="18%" ALIGN="RIGHT" NOWRAP>ID</TH>
<TD WIDTH="82%" NOWRAP>
<INPUT TYPE="TEXT" NAME="userid" SIZE="8">
</TD>
</TR>
<TR>
<TH WIDTH="18%" ALIGN="RIGHT" NOWRAP>Password</TH>
<TD WIDTH="82%" NOWRAP>
<INPUT TYPE="PASSWORD" NAME="userpassword" SIZE="8">
</TD>
</TR>
<TR>
<TD WIDTH="100%" COLSPAN="2" ALIGN="CENTER" NOWRAP>
<INPUT TYPE="SUBMIT" VALUE="LOGIN" NAME="Submit">
</TD>
</TR>
</TABLE>
</CENTER></DIV>
</FORM>
</BODY>
</HTML>
<?
}
session_start();
if(!isset($userid)) {
login_form();
exit;
}
else {
session_register("userid", "userpassword");
$username = auth_user($userid, $userpassword);
if(!$username) {
session_unregister("userid");
session_unregister("userpassword");
echo "Authorization failed. " .
"You must enter a valid userid and password combo. " .
"Click on the following link to try again.<BR>\n";
echo "<A HREF="$PHP_SELF">Login</A><BR>";
echo "If you're not a member yet, click " .
"on the following link to register.<BR>\n";
echo "<A HREF="$register_script">Membership</A>";
exit;
}
else echo "Welcome, $username!";
}
?>