Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 26th, 2003, 04:37 PM
Registered User
 
Join Date: Sep 2003
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Rich

Thanks ...

your example:

// the wrong way
echo "Your favorite author is: " . $Author;

// the correct way
echo "Your favorite author is: " . $_GET['Author'];

This works with globals Off.



James S
 
Old September 26th, 2003, 04:39 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Okay, so James should have enough info to go on... but we're still waiting to hear back from wander.

If PHP is installed properly, then phpinfo() should generate a web page containing all the active configuration settings. If that script fails, then there's something wrong with the installation. That's where we need to start.


Take care,

Nik
http://www.bigaction.org/
 
Old September 29th, 2003, 05:58 PM
Registered User
 
Join Date: Sep 2003
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by jscomputing
 Hi

Like you, I am new to PHP ..ran the same same program as you and had the similar problem.

I created a script testPhp.php
added the line:
<?php phpinfo(); ?>
saved and ran from the browser. This was successful. It showed all the configuration details.
According to it, the php.ini file resides in c:\WINDOWS.
I edited php.ini, searched for string 'register_globals'
The value was set to Off. I changed it On, saved it.
The script then worked. Did print the Author I entered.

Not sure why the variable Author have to be global. I need to do more thinking



James S
Regards,
Jake.
 
Old September 29th, 2003, 06:01 PM
Registered User
 
Join Date: Sep 2003
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Sigh. Ok I had more difficulty with that than I'd like to admit, but here's the actual quote and reply I meant to send:

Quote:
quote:Not sure why the variable Author have to be global. I need to do more thinking
This is awesome. It is good that you are thinking like this, and it will definitely help in learning a language.

In addition to getting the code to run and the output to appear, it helps to think about how it is happening and why. In this particular case, the method shown by the book was not actually the current preferred method for displaying a query-string variable, but it is good that you are thinking about these things.

Regards,
Jake.
 
Old October 1st, 2003, 07:55 PM
Registered User
 
Join Date: Aug 2003
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Nik sorry - it's true I never give answer to your question:
  "You still didn't answer -- what do you see when you run a simple phpinfo() script?"

-> I see that everything is ok and run perfectly

I had two errors, I forgot something in the installation and when I tried some of my scripts I wasn't in server mode - I double-click on the file in Windows Explorer to open it... so the server never worked (in particular case!!)

So here for the beginners:
Don't forget this: PHP is server engine so in your browser always enter http://computername and everything will work like a charm!

Thanks to everyone and the posts you put help a lot

-Wander-

 
Old October 1st, 2003, 11:17 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I've done that before, and boy did I feel silly. I had a project I was working on where the client's HTML pages were created in FrontPage, and for some reason, all the hyperlinks were to "file://path/to/file/whatever.html" instead of an http:// link.

ARg.


Take care,

Nik
http://www.bigaction.org/
 
Old October 2nd, 2003, 11:06 AM
Registered User
 
Join Date: Aug 2003
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Okay,
Now I'm trying to test a login script to access a web page and I've got some problems, first here is my script (same as the one in the book):

<?php
//register.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 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;
}
?>
</BODY>
</HTML>

Problem #1: when using in my browser (in server mode!!) this script and when I complete all the fields nothings happend - i mean when I click on submit I can't see the next stop who is:"Welcome, $username"!" and the fields I entered are not added to the databases.

Nick, your past comments helps - do you have one for this????

Tks,

-Wander-

 
Old October 2nd, 2003, 04:16 PM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

Again wander, many of the problems in your script are related to register_globals.

Instead of $PHP_SELF, use $_SERVER["PHP_SELF"]
which eliminates the need for global $PHP_SELF;

Instead of the global keyword, personally I would use the $GLOBALS superglobal array which makes globals not only available in the broader global scope but also within every function and class's local scope.

You also close the database connection right after creating it.. how are you supposed to be able to make queries if there is no connection present?

This script is pretty long and I'll bet no one will probably have the time to go through the entire thing. Personally I'm going to suggest revisting register_globals and apply what you know about that to this script.

I'm finding it pretty difficult to follow the flow and logic of this thing. You export many of these variable to global scope (via the global keyword) yet I can see no where that accesses them in global scope.

You're using the post method to send variables to the server but in your validation you aren't accessing these via the $_POST superglobal array e.g. $_POST["userid"];

You're HTML is pretty messy too. See my faq:
http://p2p.wrox.com/topic.asp?TOPIC_ID=4028

Take another crack at this thing and try to think through it for yourself (lest you never really learn how these things work!). And revisit register_globals there are a plethora of links on this page that discus this.

If I have time I will revisit your script later and point out more errors.

: )
Rich

:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
 
Old October 2nd, 2003, 06:51 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

It is messy code, but that's less Wander's fault than it is the authors of the book. That user viewer script is taken straight from the book, and I remember about a hundred complaints from users who didn't know why they got an undefined function error for enum_options().* At first glance, the code looks like it should work if you turn on register_globals, though I do suggest taking the time to convert the script over to register_globals = off.

Rich, each function that imports a global variable does end up using it. It's difficult to see, because it's so sloppy, but the function register_form() takes up about 1/3 of the entire script; both in and out of <?php ... ?> blocks.

Here's yet another long rant/FAQ that might help you:
  http://p2p.wrox.com/topic.asp?TOPIC_ID=3935


Take care,

Nik
http://www.bigaction.org/





Similar Threads
Thread Thread Starter Forum Replies Last Post
how to pass data from Client to server script pushpa Classic ASP Basics 8 March 11th, 2007 04:30 AM
client side sorting and paging in data grid prankur ASP.NET 1.0 and 1.1 Basics 1 February 3rd, 2006 12:05 PM
client side sorting and paging in data grid prankur ASP.NET 1.0 and 1.1 Professional 1 February 3rd, 2006 12:00 PM
storing data client side Anjali VBScript 3 September 3rd, 2004 05:24 PM
manipulate data returned using client script alyeng2000 Classic ASP Databases 5 December 9th, 2003 01:39 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.