Forum Moderators: coopster

Message Too Old, No Replies

very new to PHP

can you help

         

bilenkyj

7:27 pm on Sep 18, 2004 (gmt 0)

10+ Year Member



i have only just started an need a lot of help even getting it all running,
i have phpdev and myphpadmin all running, my test pages all come up, problem is i can seem to be able to display variable values, i've set variables to global in my php.ini file - can i just confirm that it is ment to be present in your windows directory on windows XP?

here is my very simple script
im just trying to sent a value to another page and display it

<html>
<head>
<title>who are you?</title>
</head>
<body>

<form action = "youare.php" method="POST">

please enter your name:<br />
I am.......
<?php print('<input type="text" name ="person" value="' . $person . '" size ="15" />');?>

<input type="submit" value ="go!" size ="15" />

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

<html>
<head>
<title>you are</title>
</head>

the page that recieves it all

<body>
<?php
print('well, hello ' . $person . ', nice to meet you!');
print('<br />');
print('<a href="whoareyou.php?person=' . urlencode($person) . '">back to who are you?</a>');
?>
</body>
</html>

i get the following

well, hello , nice to meet you!
back to who are you?

i had this issue before but cant seem to fix it

can you help?

Zipper

8:06 pm on Sep 18, 2004 (gmt 0)

10+ Year Member



Welcome to Webmasterworld, bilenkyj!

If you are running this on a windows machine use $_POST['person'] & $_GET['person'] for POST & GET requests respectively.

bilenkyj

8:19 pm on Sep 18, 2004 (gmt 0)

10+ Year Member



cheers buddy, its all good

bilenkyj

8:44 pm on Sep 18, 2004 (gmt 0)

10+ Year Member



there is another thing if anyone can help, im trying to add some user inputted data into a table in my database,

i run the pages and add data myself, i get no error msgs and it confirms that its been added but when I check phpmyadmin the table remains empty, can you help?

<html>
<head>
<title>Please enter details</title>
</head>

<body>
<form action="insert.php" method ="POST">

<?php

echo "please insert details";
echo "<p>";

print('<Last name: <input type="text" name="last_name" size="15" />');
echo "<br />";

print('First name: <input type="text" name="first_name" size="15" />');
echo "<br />";

print('address: <input type="text" name="theaddress" size="15" />');
echo "<br />";

print('City: <input type="text" name="city" size="15" />');
echo "<br />";

print('State: <input type="text" name="state" size="15" />');
echo "<br />";

print('Zip_code: <input type="text" name="zip_code" size="15" />');
echo "<p>";
?>

<input type="submit" value="submit!">

</form>

</body>

</html>

recieving page

<?php

print('Hello ' . $first_name . ' thank you for adding your details and welcome');
echo "<p>";

$link = mysql_connect();

if (!$link) {
die('Could not connect: ' . mysql_error());
}

$databasename='mydatabase';

mysql_select_db($databasename, $link)
or die('Could not connect: ' . mysql_error());

echo "we have now connected";

$lastname = $_POST[last_name];
$firstname = $_POST[first_name];
$theaddress = $_POST[theaddress];
$city = $_POST[city];
$state = $_POST[state];
$zipcode = $_POST[zip_code];

//now inserting the data to addresses table

$query="UPDATE addresses SET last_name='$lastname', theaddress='$theaddress', first_name='$firstname', city='$city', state=$state', zip_code = '$zipcode'";
mysql_query($query, $link);

echo "<p>";
echo "data has been added to database!";

mysql_close($link);

?>

edit - I get actually Gget this as a response

Hello aaron thank you for adding your details and welcome
we have now connected Notice: Use of undefined constant last_name - assumed 'last_name' in c:\phpdev\www\insert.php on line 19 Notice: Undefined index: last_name in c:\phpdev\www\insert.php on line 19 Notice: Use of undefined constant first_name - assumed 'first_name' in c:\phpdev\www\insert.php on line 20 Notice: Use of undefined constant theaddress - assumed 'theaddress' in c:\phpdev\www\insert.php on line 21 Notice: Use of undefined constant city - assumed 'city' in c:\phpdev\www\insert.php on line 22 Notice: Use of undefined constant state - assumed 'state' in c:\phpdev\www\insert.php on line 23 Notice: Use of undefined constant zip_code - assumed 'zip_code' in c:\phpdev\www\insert.php on line 24

data has been added to database!
do i just edit my php.ini file to get ride of some of this?

Zipper

9:03 pm on Sep 18, 2004 (gmt 0)

10+ Year Member



it appears that you're not connecting to mysql.

you should provide mysql_connect() with 3 parameters.
syntax : mysql_connect(server, username, password)

if you're on your local machine something like this would do,

mysql_connect("localhost", "root", "");

when you're using $_POST[] use single quotes ($_POST['variable']) to get rid of those notices. also you don't need to print all html using php. (unless ur doing for leaning purposes) . one more thing, you're trying to UPDATE the database, not INSERT. So you won't have any more than one row in your table.

Oh! and don't try to bring up php.ini for every error msg u get. It's better off that you don't edit it at all from now on, unless its absolutely essential. ;)

mincklerstraat

9:34 am on Sep 19, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The best way to get rid of the 'undefined .... assumed' business is instead of using:
$_POST[name]
to use:
$_POST['name']
When php sees just a group of letters not in quotes, it assumes this is a 'constant', which is like a special kind of variable that can be set, but can't vary - so it sees name there in those brackets and says, ok, gotta constant there, what's the value of the constant called name? And it spits out this error, but then goes on to correct things by assuming it's a string - a string is just a data type, not a variable or function or anything - basically a datatype that's not numeric, like a text. So 'name' is a string. And strings in general need to be surrounded by quotes. You sometimes see examples of PHP written in long strings inside of double quotes which then don't put the things corresponding to name here in quotes 'name' here is technically called the key of an array - look up arrays some time in the manual, getting to know them early will help you a lot. Anyways, this can be more confusing when you begin than it's really worth. If you always use single quotes, and remember never to put variables inside these quotes (use a concatenator - '.' instead) you'll do better, and furthermore, your php code will run a tiny bit faster. So you write your code like this:
print('Hello ' . $_GET['first_name'] . ' thank you for adding your details and welcome');
even though this would work, and can save typing, but in the long run is more confusing and makes code slower:
print("Hello $_GET[first_name] thank you for adding your details and welcome");
If you're lazy, don't want to change your code, and want to get these errors out of there you can also add this to the top of your code:
error_reporting(E_ALL & ~E_NOTICE); - this will hide those errors that PHP detects but automatically corrects for itself. It's not really advisable when you're writing code.