Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Undefined Variables 1

Status
Not open for further replies.

luds

Programmer
Jul 15, 2003
162
GB
I've just started learning php (again! lol) and am trying a few simple things out...

I've 2 pages. The first ("names1.php) is a form for a user to input info.. this is then sent to the second page where the info is displayed and the mysql database updated.

However.. I can't seem to find the right way of defining the variables.. keep getting "Variables undefined..etc" error messages.

It's probably an incredibly simple error.. but as a newbie in this I'm in need of an expert pair of eyes to find it!

Thanks in advance

Luds

"names1.php"

<html>
<head>
<title>names</title>
<meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=iso-8859-1&quot;>
</head>

<body>



<?php

echo &quot;Please enter the following information.
<form action='processform.php' method='post'>\n
<input type='text' name='firstName'>\n
<input type='text' name='lastName'>\n
<input type='text' name='address' size='30'>\n
<input type='text' name='email'>\n
<input type='submit' value='Go to next page'>\n
</form>\n&quot;;
?>




</body>
</html>

&quot;processform.php&quot;

<html>
<head>
</head>
<body>
<h2>You have just entered this information</h2>

<?php
$user=&quot;luds&quot;;
$host=&quot;localhost&quot;;
$password=&quot;&quot;;
$database=&quot;invester_names&quot;;
$connection = mysql_connect($host,$user,$password) or die
(&quot;Couldn't connect to server!&quot;);
$db = mysql_select_db($database,$connection) or die (&quot;Couldn't select database&quot;);

foreach ($HTTP_POST_VARS as $key => $value)

{
echo &quot;$key- $value<br>&quot;;
}

$query = &quot;INSERT INTO personal (firstName,lastName,address,email)
VALUES ('$firstName','$lastName','$address','$email')&quot;;
$result = mysql_query($query) or die (&quot;Couldn't execute query.&quot;);

echo &quot;Database Updated&quot;;
?>
</body>
</html>

 
he meant the line in which u get the error...

Known is handfull, Unknown is worldfull
 
Sorry...again!

Notice: Undefined variable: firstName in C:\Program Files\Apache Group\Apache2\htdocs\TMP4eb7o5824.php on line 23

Notice: Undefined variable: lastName in C:\Program Files\Apache Group\Apache2\htdocs\TMP4eb7o5824.php on line 23

Notice: Undefined variable: address in C:\Program Files\Apache Group\Apache2\htdocs\TMP4eb7o5824.php on line 23

Notice: Undefined variable: email in C:\Program Files\Apache Group\Apache2\htdocs\TMP4eb7o5824.php on line 23
Couldn't execute query.

 
No.

You'll use $_POST['variable'] for variables coming in from POST-method forms

You'll use $_GET['variable'] for GET-method submitted data

You'll use $_COOKIE['variable'] for cookies.

You'll use $_SESSION['variable'] for session variables.

Want the best answers? Ask the best questions: TANSTAAFL!!
 
Sorry to be a pain Sleipnir........ used $POST (cos I eventually don't want info in the URL and cookies aren't always turned on).. code for the second page is now.....


....
foreach ($_POST as $key => $value)

{
echo &quot;$key- $value<br>&quot;;
}

$query = &quot;INSERT INTO personal (firstName,lastName,address,email)
VALUES ($_POST['$firstName'],$_POST['$lastName'],$_POST['$address'],$_POST['$email'])&quot;;
$result = mysql_query($query) or die (&quot;Couldn't execute query.&quot;);

echo &quot;Database Updated&quot;;

......etc

Now I'm getting
Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in C:\Program Files\Apache Group\Apache2\htdocs\processform.php on line 27

That's the line that starts with .. foreach ($_POST... etc
Any ideas?
Thanks
luds

 
update! .....

WEll, I'm getting there.. I think!
Using $_POST I've got the information entered on the top of the page.... but I still can't fathom out where I'm going wrong in the query so I can write the info to the database!

I've swapped &quot; and ' and added $_POST....eg
$_POST['firstName']
.....before the variable names but to no avail!

At the moment I've got the code
<html>
<head>
</head>
<body>
<h2>You have just entered this information</h2>
<?php

$user=&quot;luds&quot;;
$host=&quot;localhost&quot;;
$password=&quot;&quot;;
$database=&quot;invester_names&quot;;
$connection = mysql_connect($host,$user,$password) or die
(&quot;Couldn't connect to server!&quot;);
$db = mysql_select_db($database,$connection) or die (&quot;Couldn't select database&quot;);
?>

Firstname<?php echo $_POST['firstName']; ?><BR>
Surname<?php echo $_POST['lastName']; ?><BR>
Address<?php echo $_POST['address']; ?><BR>
Email<?php echo $_POST['email']; ?><BR>

<?php
$query =
&quot;INSERT INTO personal
(lastName, firstName, address, email)
VALUES ('$lastName', '$firstName', '$address', '$email')&quot;;

$sql = mysql_query($query) or die(&quot;Invalid query: &quot; . mysql_error());
?>

</body>
</html>

and the errors
Notice: Undefined variable: lastName in C:\Program Files\Apache Group\Apache2\htdocs\processform.php on line 31

four times , once for each variable!

Still... it'll work out eventually! lol


 
The HTML form I changed slightly.. can't remember why! After ploughing through what seems like hundreds of posts on the web and documentation it;s all become a bit of a blur! lol

It's now..

<html>
<head>
<title>names</title>
<meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=iso-8859-1&quot;>
</head>

<body>
&quot;Please enter the following information.<BR>

<form action='processform.php' method='POST'>
<input type='hidden' name='firstName' value=&quot;<?php echo $_POST['firstName']; ?>&quot;>
First Name<input type=&quot;text&quot; name='firstName'><br>

Second Name<input type='hidden' name='lastName' value=&quot;<?php echo $_POST['lastName']; ?>&quot;>
<input type=&quot;text&quot; name=&quot;lastName&quot;><br>

Address<input type='hidden' name='address' size='30' value=&quot;<?php echo $_POST['address']; ?>&quot;>
<input type=&quot;text&quot; name=&quot;address&quot;><br>

Email<input type='hidden' name='email' value=&quot;<?php echo $_POST['email']; ?>&quot;>
<input type=&quot;text&quot; name=&quot;email&quot;><br>

<input type='submit' value='Go to next page'>
</form>


</body>
</html>

If I put that Print '<pre>' etc at the bottom of each page, just before </body>, I get...

Array
(
)






 
*looks sheepish*.... I thought that that form in my last post was a post method form???
<form action='processform.php' method='POST'>

and that this posts the values to the next page &quot;processform.php&quot; which can then display them and send them to the database???

Or have I got myself in the proverbial, here!



 
Yes, that script produces a POST-method form.

However, when the script above is invoked for the first time, nothing will be posted to the script. So your getting &quot;undefined variable&quot; errors is completely correct -- if nothing is submitted, $_POST is empty.

Want the best answers? Ask the best questions: TANSTAAFL!!
 
Yes....YEs..... YES!!!!!!!!!!!! lol

Don't know if it's the most 'economical' way of doing it, but I just added

$firstName=$_POST['firstName'];
$lastName=$_POST['lastName'];
$address=$_POST['address'];
$email=$_POST['email'];

after the database connection at the top of the processform.php... :¬)))))))

Thanks Sleipnir for your help and for continually pointing me in the right direction. Couldn't have got it without your help!
It has undoubtedly been a long exercise.. some 15 or so hours at least! lol... but I've learnt a lot along the way!

Right... off to get a drink! lol
.. and then go back to something that I'm better at... formatting these pages in HTML and CSS so they look better! lol

Thanks again

luds

 
Thanks so much luds for typing out EVERYTHING - This is truly helpful. I appreciate sleipnirs info, too, but seeing this come together in actual code has been invaluable. :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top