Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old January 4th, 2005, 05:32 PM
Registered User
 
Join Date: Jan 2005
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default code does not work



I have been working through "begining php5"

chap 11 register.php

I have typed the coded, and downloaded it, edit both the php, and the inc fiule to match my mysql. When the form is filled out and submit is pressed, the form just resets. no errors on the page or in view source.

I am running php5, mysql apache 2.0 on win xp sp2

I have alos uploaded tro my host which is freebsd mysql, php4.###

what I am doing wrong

thanks

Reply With Quote
  #2 (permalink)  
Old January 5th, 2005, 10:14 AM
Authorized User
 
Join Date: Dec 2004
Location: Tunkhannock, PA, USA.
Posts: 53
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to peg110
Default

Since I (and probably others) do not have "Beginning PHP5" then perhaps you could present us with the code of the pages and we could be more helpful.

Also, anything in the error logs?



Paul Gardner
------------------
PHP-LIVE help
Via Web @ http://www.mnetweb.co.uk/irc
Via IRC Client pgardner.net:6667
room #PHP
Reply With Quote
  #3 (permalink)  
Old January 5th, 2005, 10:52 AM
Registered User
 
Join Date: Jan 2005
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

[u]register.php</u>
Code:
<?php
//register.php
include_once "./common_db.inc";

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 $userposition;
global $PHP_SELF;

$link_id = db_connect();
mysql_select_db("php");
$position_array = enum_options('userposition', $link_id);
mysql_close($link_id);

?>


<CENTER><H3>Create your account!</H3></CENTER>
<FORM METHOD="POST" ACTION="<?php echo $PHP_SELF ?>">
<INPUT TYPE="HIDDEN" NAME="action">
  <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"
                             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" SIZE="20"></TD>
    </TR>
    <TR>
      <TH WIDTH="30%" NOWRAP>Position</TH>
      <TD WIDTH="70%"><SELECT NAME="userposition" SIZE="1">
<?php


   for($i=0; $i < count($position_array); $i++) {
    if(!isset($userposition) && $i == 0) {
      echo "<OPTION SELECTED VALUE=\"". $position_array[$i] .
           "\">" . $position_array[$i] . "</OPTION>\n";
    }
    else if($userposition == $cposition_array[$i]) {
      echo "<OPTION SELECTED VALUE=\"". $position_array[$i] . "\">" .
                                        $position_array[$i] . "</OPTION>\n";
    }
    else {
      echo "<OPTION VALUE=\"". $position_array[$i] . "\">" .
                               $position_array[$i] . "</OPTION>\n";
    }
  }
?>


      </SELECT></TD>
    </TR>
    <TR>
      <TH WIDTH="30%" NOWRAP>Email</TH>
      <TD WIDTH="70%"><INPUT TYPE="TEXT" NAME="useremail" SIZE="20"
      </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() {
   $userid = $_POST['userid'];
   $username = $_POST['username'];
   $userpassword = $_POST['userpassword'];
   $userpassword2 = $_POST['userpassword2'];
   $userposition = $_POST['userposition'];
   $useremail = $_POST['useremail'];
   $userprofile = $_POST['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', '$userposition', '$useremail', '$userprofile')";
   $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>Position</TH>
    <TD WIDTH="70%"><?php echo $userposition ?></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();
}

if (empty($_POST)) $_POST['action'] = "";

switch($_POST['action']) {
   case "register":
      create_account();
   break;
   default:
      html_header();
      register_form();
      html_footer();
   break;
}
?>
[u]common_db.inc</u>

Code:
<?php
$dbhost = 'localhost';
$dbusername = '***';
$dbuserpassword = '****';
$default_dbname = 'php';

$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");
      NEW_WIN.location.href = url;
   }
   //-->
   </SCRIPT>
   <TITLE>User Record Viewer</TITLE>
   </HEAD>
   <BODY>
   <?php
}


function html_footer() {
?>
</BODY>
</HTML>
<?php
}

function db_connect(){
   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 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;
}

?>
no errors, ,the page just resets.  I can view my database with php no problem.  globels is off, (and I have tried on)
Reply With Quote
  #4 (permalink)  
Old January 6th, 2005, 09:08 AM
Authorized User
 
Join Date: Dec 2004
Location: Tunkhannock, PA, USA.
Posts: 53
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to peg110
Default

Well without plugging through ALL that code at first glance, I noticed that you are including "./common_db.inc" and you call the file "./common.inc"

Next thing I would suggest is to check your error_reporting line in the PHP.INI file. Make sure that you are actually reporting errors. Indeed if you have the include file name wrong (and presuming that the included file DOES NOT exist) you SHOULD get an error.

HTH get you started.

My next suggestion would be to try and narrow down the code that you presented to what you think is the relavant sections of where you think the problem might be.



Paul Gardner
------------------
PHP-LIVE help
Via Web @ http://www.mnetweb.co.uk/irc
Via IRC Client pgardner.net:6667
room #PHP
Reply With Quote
  #5 (permalink)  
Old January 7th, 2005, 01:40 PM
Registered User
 
Join Date: Jan 2005
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

that was just a typo in this forum.

Reply With Quote
  #6 (permalink)  
Old January 10th, 2005, 08:52 AM
Authorized User
 
Join Date: Dec 2004
Location: Tunkhannock, PA, USA.
Posts: 53
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to peg110
Default

did you check the error reporting in the PHP.INI file?

Also, you reference $PHP_SELF a couple of locations. If globals are off, then you need to call it by usint the $_SERVER['PHP_SELF'] instead.

To make sure, when you load the page (initially) check the source to see if the ACTION item actually shows the proper page.

Paul Gardner
------------------
PHP-LIVE help
Via Web @ http://www.mnetweb.co.uk/irc
Via IRC Client pgardner.net:6667
room #PHP
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
code clinic - Why wont example asp code work? jardbf Classic ASP Basics 3 April 27th, 2006 06:22 PM
code for rss work hsascubaboy RSS and Atom 2 May 29th, 2005 06:51 PM



All times are GMT -4. The time now is 03:44 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.