Need help on my php and mysql code

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
wcfoone
Forum Newbie
Posts: 3
Joined: Wed Dec 31, 2003 2:30 pm

Need help on my php and mysql code

Post by wcfoone »

:?: :?:
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; 
} 
?>
login.php

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!"; 
} 
?>
Edited by Infolock : Added eye candy php tags
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

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
are you not sure what table to create in your database ? only table i see you using is the table called USER

or are you getting an error? if you are, please give the error message.
wcfoone
Forum Newbie
Posts: 3
Joined: Wed Dec 31, 2003 2:30 pm

This how the error occured

Post by wcfoone »

At 1st, i say 10 to who willing to help me.

I have a database call:test" and i create a table call"user"
and inside the table , i create this field << userid,userpassword,username,usercountry,curdate>>>
i know some of it must set the validation but i really what valitaion i suppose to set.
beside that, when i go to login.php

i appeared this kind of error

Warning: session_start(): open(/tmp\sess_68143e6275bc56e8c1ab3269ff2e8f14, O_RDWR) failed: No such file or directory (2) in C:\Apache2\htdocs\login.php on line 58

Warning: session_start(): Cannot send session cookie - headers already sent by (output started at C:\Apache2\htdocs\db.inc:95) in C:\Apache2\htdocs\login.php on line 58

Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at C:\Apache2\htdocs\db.inc:95) in C:\Apache2\htdocs\login.php on line 58

Warning: Unknown(): open(/tmp\sess_68143e6275bc56e8c1ab3269ff2e8f14, O_RDWR) failed: No such file or directory (2) in Unknown on line 0

Warning: Unknown(): Failed to write session data (files). Please verify that the current setting of session.save_path is correct (/tmp) in Unknown on line 0


i try to set database validation but i found myself fail to set the rite validation for those field......
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

Looks like there isnt a session folder.. or it is linked wrong in php.ini?
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

PHP Manual : php.ini's save_path wrote:Note: Windows users have to change this variable in order to use PHP's session functions. Make sure to specify a valid path, e.g.: c:/temp.
wcfoone
Forum Newbie
Posts: 3
Joined: Wed Dec 31, 2003 2:30 pm

10 on ur help

Post by wcfoone »

I say 10 1st, and i will try to set the temp fold c can work o not,,
Post Reply