Don't see variables being set...hlep

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
Lorenzo
Forum Newbie
Posts: 5
Joined: Mon Jul 28, 2003 12:04 pm

Don't see variables being set...hlep

Post by Lorenzo »

I am new to PHP...I have a script that I got from a tutorial that uses Forms but when I do a submit, I do not see the $REQUEST_METHOD variable that the author is looking for set. I also tried looking at $submit, and tried the isset() function but I could not get it to work. Could someone please help? Here is the code...

<?
/*
PHP Guestbook 1.1
Written by Tony Awtrey
Anthony Awtrey Consulting
See http://www.awtrey.com/support/dbeweb/ for more information

1.1 - Oct. 20, 1999 - changed the SQL statement that reads data
back out of the database to reverse the order putting the
newest entries at the top and limiting the total displayed
by default to 20. Added the ability to get the complete list
by appending the URL with '?complete=1'. Added the code and
additional query to count and list the total number of entries
and included a link to the complete list.
1.0 - Initial release

This is the SQL statement to create the database required for
this application.

CREATE TABLE guests (
guest_id
int(4)
unsigned
zerofill
DEFAULT '0000'
NOT NULL
auto_increment,
guest_name varchar(50),
guest_email varchar(50),
guest_time timestamp(14),
guest_message text,
PRIMARY KEY (guest_id)
);

*/

////////////////////////////////
// This checks to see if we need to add another guestbook entry.
////////////////////////////////
if (($REQUEST_METHOD=='POST')) {

////////////////////////////////
// This loop removed "dangerous" characters from the posted data
// and puts backslashes in front of characters that might cause
// problems in the database.
////////////////////////////////
for(reset($HTTP_POST_VARS);
$key=key($HTTP_POST_VARS);
next($HTTP_POST_VARS)) {
$this = addslashes($HTTP_POST_VARS[$key]);
$this = strtr($this, ">", " ");
$this = strtr($this, "<", " ");
$this = strtr($this, "|", " ");
$$key = $this;
}
////////////////////////////////
// This will catch if someone is trying to submit a blank
// or incomplete form.
////////////////////////////////
if ($name && $email && $message ) {

////////////////////////////////
// This is the meat of the query that updates the guests table
////////////////////////////////
$query = "INSERT INTO guests ";
$query .= "(guest_id, guest_name, ";
$query .= "guest_email, guest_time, guest_message) ";
$query .= "values(0000,'$name','$email',NULL,'$message')";
mysql_pconnect("host","user","password")
or die("Unable to connect to SQL server");
mysql_select_db("dbasename") or die("Unable to select database");
mysql_query($query) or die("Insert Failed!");

} else {

////////////////////////////////
// If they didn't include all the required fields set a variable
// and keep going.
////////////////////////////////
$notall = 1;

}
}
?>
<!-- Start Page -->
<HTML>
<HEAD>
<TITLE>Add a Message</TITLE>
</HEAD>

<BODY BGCOLOR="white">

<H1>Add A Message</H1>

<!-- Let them know that they have to fill in all the blanks -->
<? if ($notall == 1) { ?>
<P><FONT COLOR="red">Please answer all fields</FONT></P>
<? } ?>

<!-- The bits of PHP in the form allow the data that was already input
to be placed back in the form if it is filled out incompletely -->

<FORM METHOD="post" ACTION="guest.php">
<PRE>
Your Name: <INPUT
TYPE="text"
NAME="name"
SIZE="20"
MAXLENGTH="50"
VALUE="<? echo $name; ?>">
Your Email: <INPUT
TYPE="text"
NAME="email"
SIZE="20"
MAXLENGTH="50"
VALUE="<? echo $email; ?>">

Enter Message:
<TEXTAREA NAME="message" COLS="40" ROWS="8" WRAP="Virtual">
<? echo $message; ?>
</TEXTAREA>

<INPUT TYPE="submit" VALUE="Add">

</PRE>
</FORM>

<HR>

<?

////////////////////////////////
// This is where we connect to the database for reading.
////////////////////////////////
mysql_pconnect("host","user","password")
or die("Unable to connect to SQL server");
mysql_select_db("dbasename") or die("Unable to select database");

////////////////////////////////
// This is where we count the number of entries.
////////////////////////////////
$query = "SELECT COUNT(*) FROM guests";
$numguests = mysql_query($query) or die("Select Failed!");
$numguest = mysql_fetch_array($numguests);

?>

<!-- This is where we report the total messages. -->
<P>
<A HREF="guest.php?complete=1"><? echo $numguest[0]; ?> people</A> have
left me a message.
</P>

<?

////////////////////////////////
// This is where we decide to get all the entries or just the last 20.
// This variable is set by just adding a '?complete=1' after the URL.
////////////////////////////////
if ($complete == 1) {
$query = "SELECT * FROM guests ORDER BY guest_time DESC";
} else {
$query = "SELECT * FROM guests ORDER BY guest_time DESC LIMIT 20";
}
$guests = mysql_query($query) or die("Select Failed!");

////////////////////////////////
// This will loop as long as there are records waiting to be processed.
// Notice the plain HTML inside the while loop structure. PHP is flexable
// enough to allow you to break into and out of the "code" at any point.
////////////////////////////////
while ($guest = mysql_fetch_array($guests)) {

?>
<TABLE BORDER="1" WIDTH="500">
<TR><TD>
Name: <? echo $guest['guest_name']; ?>
</TD><TD>
Email: <A HREF="mailto:<? echo $guest['guest_email']; ?>">
<? echo $guest['guest_email']; ?></A>
</TD><TD>
<?

////////////////////////////////
// The database has a timestamp record type that we can use to show the
// date the guestbook was filled out.
////////////////////////////////
$datefromdb = $guest['guest_time'];
$year = substr($datefromdb,0,4);
$mon = substr($datefromdb,4,2);
$day = substr($datefromdb,6,2);
$hour = substr($datefromdb,8,2);
$min = substr($datefromdb,10,2);
$sec = substr($datefromdb,12,2);
$orgdate = date("l F dS, Y h:i A",mktime($hour,$min,$sec,$mon,$day,$year));
?>
Date: <? echo $orgdate; ?>
</TD></TR>
<TR><TD COLSPAN="3">
<? echo $guest['guest_message']; ?>
</TD></TR>
</TABLE>
<BR>

<? } ?>

</BODY>
</HTML>
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post by m3rajk »

this uses $HTTP_POST_VARS
that variable was deprecated in php 4.1.0

php is now on 4.3.3 with a beta of 5.0.0 out.

my suggestion is to either get a good php book and use the modifcation of that via what you can get form the book as a way to learn php, or to find one made for a version of php that's at least 4.1

there's several threads on books.
search.php?mode=results

hmmm. that probalby wont work. i searched for: php books

there's a LOT of threads that came up
Lorenzo
Forum Newbie
Posts: 5
Joined: Mon Jul 28, 2003 12:04 pm

Post by Lorenzo »

If $HTTP_POST_VARS was deprecated and I am running PHP 4.3.2 for Windows with Apache 1.3.28, what could I use for this to work?
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post by m3rajk »

i still recommend getting a book. any book work the money you pay for it will go over what's been deprecared since 4.0 and will cover through at least 4.1 possibly 4.2 or 4.3

i learned on 3.0.18 over a year and a half ago. didn't do anything with php till about 2 months ago. first thing i did was look at a deprecation list between 4.0 and 3.x

from that i knew i needed a book to go over the new things as well as talk about what's deprecated so i know i have something to refrence and relearn.

in the very begining of the one i have they talk about things like $HTTP_POST_VARS v $_POST
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

There are two things you can do:

1. This thread will answer your question regarding var-changes.
2. Let me introduce you to your new best friend:

Code: Select all

echo "<pre>";
print_r($myVariable);
echo "</pre>";
Lorenzo
Forum Newbie
Posts: 5
Joined: Mon Jul 28, 2003 12:04 pm

Thanks for your comments...

Post by Lorenzo »

I will look into getting some books...
Lorenzo
Forum Newbie
Posts: 5
Joined: Mon Jul 28, 2003 12:04 pm

Thanks for the info...

Post by Lorenzo »

I am now looking at why my session variables are not being set in additional pages...
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

It's actually worthwhile reading the stickies, such as this one.
Lorenzo
Forum Newbie
Posts: 5
Joined: Mon Jul 28, 2003 12:04 pm

Post by Lorenzo »

That is where I got the current example on sessions to try...

When I get to the $_SESSION['auth'] statement in page5.php, it is not set even though it got set in page4.php...is there something that needs to be set in php.ini for sessions or is this an Apache problem?

Any ideas?...Thanks ahead of time
Post Reply