Forms are killing me, help please !

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
User avatar
rax369
Forum Commoner
Posts: 56
Joined: Sun Oct 06, 2002 8:50 pm

Forms are killing me, help please !

Post by rax369 »

Hi!, I'm trying this simple example w/forms, that uses 2 .php pages, but if I dont turn 'ON' the "register_globals = Off" in the php.ini file, the variables from forms are not passed between .php pages... why ? (I have had problems not only w/this example, but w/all the examples that uses variables coming from forms). :(

I have created a database in mysql called "test", and the table w/which I'm dealing is "employees". The objective of the example is to insert information into the table "employees", simple as that, but everytime the form try to send the info by the "post method" the info in the form elements never comes to the other page, is like if that info. get banned.

* my first page is called "forms-test01.php", and is as follows:

<html>
<body>

<form method="post" action="forms-test02.php">

First name:<input type="Text" name="first"><br>
Last name:<input type="Text" name="last"><br>
Address:<input type="Text" name="address"><br>
Position:<input type="Text" name="position"><br>

<input type="Submit" name="submit" value="Enter information">

</form>
</body>
</html>

* and the second one is called "forms-test02.php":

<?

$connect = mysql_connect("localhost", "user", "pass") or die("Could not connect to the server");
mysql_select_db("test", $connect) or die("Could not select database");

$query1 = "INSERT INTO EMPLOYEES (first, last, address, position) VALUES('$first', '$last', '$address', '$position')";
$result1 = mysql_query("$query1", $connect);
$query2 = "SELECT * FROM EMPLOYEES";
$result2 = mysql_query("$query2", $connect);

echo "<table border=1>";
echo "<tr><td>First</td> <td>Last</td> <td>Address</td> <td>Position</td></tr><br>";
$myrow = mysql_fetch_array($result2);
do
{
printf("<tr> <td>%s</td> <td>%s</td> <td>%s</td> <td>%s</td> </tr> <br>", $myrow["first"], $myrow["last"], $myrow["address"], $myrow["position"]);
}
while($myrow = mysql_fetch_array($result2));
echo "</table>";

?>

--------------------------------------------------------------------------------
I'm runing Apache 2.0.36, PHP 4.2.0 and MySQL 3.23.52 on Win XP Pro, thx for any help guys 8) .
Coco
Forum Contributor
Posts: 339
Joined: Sat Sep 07, 2002 5:28 am
Location: Leeds, UK
Contact:

Post by Coco »

even if your globals are registered as off, you still have to use $_POST or $HTTP_POST_VARS

ie

$query1 = "INSERT INTO EMPLOYEES (first, last, address, position) VALUES($_POST['first'], $_POST['last'], $_POST['address'], $_POST['position'])";


no syntax guarantees
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

you must use the $_POST, $_GET, $_COOKIE, $_SERVER, and $_FILES arrays for things if register globals is off.
User avatar
rax369
Forum Commoner
Posts: 56
Joined: Sun Oct 06, 2002 8:50 pm

Post by rax369 »

So if a I use "$HTTP_POST_VARS or $HTTP_GET_VARS" I can use the information stored in the form fields ?

Can I use it in every php page, I mean, it is like if the variables become 'globals' :?: :?: :?:

What for should I use "$_COOKIE, $_SERVER, and $_FILES " ? (Could u give me some example please) :roll:
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

Yes you can use it anywhere and everywhere! It's kinda like super global so you don't need to use GLOBAL statement.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Have a read of this sticky:
viewtopic.php?t=511

For an explanation of what each array is for try the manual:
http://www.php.net/manual/en/reserved.variables.php

Mac
User avatar
rax369
Forum Commoner
Posts: 56
Joined: Sun Oct 06, 2002 8:50 pm

Post by rax369 »

thx a lot twigletmac, I knew that PHP was the one causing the problem w/my form examples, but I didnot what was causing the problem, that sticky u referenced in ur post, came to clear my doubts.

I printed and added your post to my php documentation for future reference.

thx a lot :P
User avatar
rax369
Forum Commoner
Posts: 56
Joined: Sun Oct 06, 2002 8:50 pm

Post by rax369 »

One more question I did what jason said, about using $_POST, but...

echo $_POST[variable]; <-- this works

echo $_POST['variable']; <-- this doesnt works (the ' ' are apparently the cause right ?)

now.... if I use $_POST in a query like this: (doesnt works... why ?)

$query1 = "INSERT INTO EMPLOYEES (first, last, address, position) VALUES ($_POST[first], $_POST[last], $_POST[address], $_POST[position])";

Why this doesnt works, if I'm using in the same way $_POST. I just wanna insert some info. into a database. When I show the content of some variable stored in the $_POST array using echo (eg. echo $_POST[variable];) this works, and using the same way $_POST in a query doesnt :(

HELP PLEASE !
User avatar
rax369
Forum Commoner
Posts: 56
Joined: Sun Oct 06, 2002 8:50 pm

Post by rax369 »

forget it !, I already found my error in the code.

stupid me :x !, I forgot to add the ' ' in the query syntaxis.

Here it goes, for all of u that did get my mystake:

* WRONG:
$query1 = "INSERT INTO EMPLOYEES (first, last, address, position) VALUES ($_POST[first], $_POST[last], $_POST[address], $_POST[position])";

* RIGHT:
$query1 = "INSERT INTO EMPLOYEES (first, last, address, position) VALUES ('$_POST[first]', '$_POST[last]', '$_POST[address]', '$_POST[position]')";

Greetings ! :lol:
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

a single quote is the start-stop-sign of a string-literal.
$var = 'this is a string'; will assing the string-literal this is a string to $var. Until something else is assigned $var is a placeholder for this literal.
$var = '$somethingelse'; will assign the string $somethingelse to $var
$var = $somethingelse the contents of (/for what the variable is a placeholder right now) $somethingelse.
The only 'magic' there is in PHP is the ""-marked string literal since variables are replaced by the string-representation of their contents.

Code: Select all

$somethingelse = 'magic';
$var = "it's $somethingelse";
-->$var is the placeholder for the string literal it's magic. This evaluation takes place right where the literal is with the current value of the replaced variable

Code: Select all

$somethinglese = 'magic';
$var = "it's $somethingelse";
$somethinglese = 'crap';
print($var);
will still output it's magic.
Always remind yourself that the variable will be replaced by its string-representation. try

Code: Select all

$a = 1;
$b = "$b";
echo gettype($a), ' is not ', gettype($b);
the output will be integer is not string.

Note that you can use the ' in a "..."-literal as well as " in a '...'-literal

Code: Select all

$var = "use ' in here freely but escape " signs";
print($var);
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

The reason it probably didn't work with quoted elements in your arrays is because you may have been doing things like this:

Code: Select all

echo "this is my variable $_POST&#1111;'var']";
PHP doesn't like that it needs to be

Code: Select all

echo 'this is my variable '.$_POST&#1111;'var'];
or

Code: Select all

echo "this is my variable {$_POST&#1111;'var']}";
if you don't quote your array elements then PHP does work it doesn't have to, if you turn your error_reporting to the highest level you'll find it's looking for constants that don't exist, eg if you had $array[var] PHP would first search for a constant called var and when it couldn't find one it would assume that you had meant $array['var'].

Mac
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Code: Select all

$arr&#1111;'no_quotes_here'] = "tadaa";
$var = "this work as well: $arr&#1111;no_quotes_here]";
print($var);
without any errors or warnings at all ;)
you only need { } if you want to turn the special meaning of certain characters on again in a literal.
But with a syntax-highlighting editor 'something '.$arr['index'] is much easier to read and/or debug.

Code: Select all

$var = "this work as well: $arr&#1111;no_quotes_here]";
$var = 'this work as well: '.$arr&#1111;'no_quotes_here'];
see the difference?
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Ok, I'm just getting irritated at the number of people who never quote element names in arrays no matter what context they are using them in and forgot that bit :lol: . I really wish all those 'Learn PHP in 10 minutes' books publishers would bring out some editions that are actually relevant to PHP 4.1 and up though.

Mac
Post Reply