PHP Coding
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Codewalkers ForumsPHP RelatedPHP Coding

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Codewalkers Forums Sponsor:
AT&T devCentral & BlackBerry(r) Webcast Series: BlackBerry and GPS -Build Location Awareness into your BlackBerry Applications, July 10th-1:00PM EST. Register Today!
  #1  
Old August 21st, 2002, 11:45 AM
cateyes cateyes is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Cyprus
Posts: 68 cateyes User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
suppress undefined variable notice

Can someone rescue me please? I am doing my first php tutorial. It uses the logic of

if($submit)
{ etc. etc

to perform some steps if the $submit button has been pressed(and therefore automatically defined by the system). If the $submit button has not been pressed, it performs different steps. Everything works fine, except I get a Notice on my page stating that the variable $submit is undefined. I would like to supress this message. Is this possible? If so, How? As this is my first day with php, a very simplistic answer would be greatly appreciated. Please note that the tutorial instructed me to set
register_globals = on
in the php.ini file and I have done this.
Thankyou!

Reply With Quote
  #2  
Old August 21st, 2002, 01:32 PM
EvilivE EvilivE is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Milwaukee, WI USA
Posts: 291 EvilivE User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
Send a message via Yahoo to EvilivE
RE: suppress undefined variable notice

Last I checked php.net strongly suggests that you have register_globals off. I would follow their recommendation over anyone elses opinions, php.net is considered "the final answer" right?

Anyways you can suppress it by placing a @ prior to if. However, this _DOES_NOT_ make $submit defined!!! I find it strange that your code works as expected if $submit is undefined.

Reply With Quote
  #3  
Old August 21st, 2002, 06:36 PM
guyer guyer is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Bloomington, IN USA
Posts: 47 guyer User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
Send a message via Yahoo to guyer
RE: suppress undefined variable notice

Evil is right about the globals but if you're just playing around, it doesn't really matter.

Check your error_reporting setting in php.ini. You can also set it at runtime by using the error_reporting() function. The @ will work but if you never want that warning, just change the php.ini.

Reply With Quote
  #4  
Old August 22nd, 2002, 08:34 AM
cateyes cateyes is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Cyprus
Posts: 68 cateyes User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
RE: suppress undefined variable notice

I have followed EvilIvE's advice and set register_globals off and created each of the variables. This worked fine. I tried to place the @ before the if, but I got an error message that said
"parse error unexpected T_IF
Any Ideas?
Thanks!

Reply With Quote
  #5  
Old August 22nd, 2002, 01:22 PM
EvilivE EvilivE is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Milwaukee, WI USA
Posts: 291 EvilivE User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
Send a message via Yahoo to EvilivE
RE: suppress undefined variable notice

Could you show us some code?

Reply With Quote
  #6  
Old August 22nd, 2002, 03:57 PM
cateyes cateyes is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Cyprus
Posts: 68 cateyes User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
RE: suppress undefined variable notice

ok, the code is below. I have placed a comment where there are problems. This code is actually from a codewalkers tutorial on creating dynamic sites. It is the first time I tried to use php - and I am unsure how to fix it.

Thanks for your time!




<?

//The following line causes the error "Undefined variable submit"
if ($submit)
{
$db = mysql_connect("our ip address","userid","password");
mysql_select_db("learndb",$db);

$first = $_POST[first];
$last= $_POST[last];
$nickname = $_POST[nickname];
$email= $_POST[email];
$salary= $_POST[salary];


$sql = "INSERT INTO personnel (firstname, lastname, nick, email, salary) values ('$first', '$last', '$nickname','email', '$salary')";
$result = mysql_query($sql);
echo "Thank you! Information entered.n";
}
//The following line causes the error "Undefined variable update"
else if($update)
{
$db = mysql_connect("our ip address","userid","password");
mysql_select_db("learndb",$db);

//Is it correct to repeat this here?
$first = $_POST[first];
$last= $_POST[last];
$nickname = $_POST[nickname];
$email= $_POST[email];
$salary= $_POST[salary];

$sql = "UPDATE personnel SET firstname = '$first', lastname = '$last', nick = '$nickname', email = '$email', salary = '$salary',WHERE id = $id";
$result = mysql_query($sql);
echo "Thank you! Information updated.n";
}
//The following line causes the error "Undefined variable id"
else if($id)
{
$db = mysql_connect("our ip address","userid","password");
mysql_select_db("learndb",$db);
$result = mysql_query("SELECT * FROM personnel WHERE id = $id",$db);
$myrow = mysql_fetch_array($result);
?>
<form method = "post" action= "<?php echo $PHP_SELF?>">
<input type= "hidden" name="id" value="<?php echo $myrow["id"]?>">
First name:<input type = "Text" name = "first" value="<?php echo $myrow["firstname"]?>"><br>
Last name:<input type = "Text" name = "last" value="<?php echo $myrow["lastname"]?>"><br>
Nick name:<input type = "Text" name = "nickname" value = "<?php echo $myrow["nick"]?>"><br>
E-mail:<input type = "Text" name = "email" value = "<?php echo $myrow["email"]?>"><br>
Salary:<input type = "Text" name = "salary" value = "<?php echo $myrow["salary"]?>"><br>
<input type = "Submit" name = "update" value = "Update Information">
</form>
<?
}
else
{
?>
<form method = "post" action="<?php echo $PHP_SELF?>">
First name:<input type = "Text" name="first"><br>
Last name:<input type = "Text" name="last"><br>
Nick name:<input type = "Text" name="nickname"><br>
Email:<input type = "Text" name="email"><br>
Salary:<input type = "Text" name="salary"><br>
<input type = "Submit" name = "submit" value = "Enter information">
</form>
<?
}
?>

Reply With Quote
  #7  
Old August 22nd, 2002, 04:05 PM
Gipz Gipz is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Stockholm, Sweden
Posts: 98 Gipz User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
Send a message via ICQ to Gipz
RE: suppress undefined variable notice

Are you doing something like:


if(array_key_exists('submit', $_POST))
$submit = $_POST['submit'];
else
$submit = '';

Reply With Quote
  #8  
Old August 22nd, 2002, 04:27 PM
EvilivE EvilivE is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Milwaukee, WI USA
Posts: 291 EvilivE User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
Send a message via Yahoo to EvilivE
RE: suppress undefined variable notice

First thing to do is to cahnge all instances of: $PHP_SELF to: $_SERVER['PHP_SELF']
The reason for this is because $PHP_SELF works when registered_globals are on, and $_SERVER['PHP_SELF'] is the way to do it when registered_globals are off.

The same thing goes for the "if" statements. Just like when you initialized your variables, to check your variables you need your if's like this:
if($_POST['submit'])
if($_POST['update'])
if($_GET['id'])

However, I would use something like this:
if(!empty($_POST['submit']))
but thay might not be necessary.

As far as initializing your vars in the different areas of your code. hmmmm ... well the answer is yes and no. Yes, if you want to initialize them at all. However it is a waste of memory to initialize them. $_POST['first'] is already a variable taking up space in memory. So if you:
$first = $_POST['first'];
you are now using twice the amount of memory required to run your program.

Lastly, you need quotes where you init your vars.

HTH

Reply With Quote
  #9  
Old August 22nd, 2002, 07:29 PM
zombie zombie is offline
Codewalkers Intermediate (1500 - 1999 posts)
 
Join Date: Apr 2007
Location: serbia
Posts: 1,876 zombie User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 3
RE: suppress undefined variable notice

1) instead

if ($submit) or
if ($_POST['submit']) or
if (!empty($_POST['submit']) or everything else, use just

if (isset($_POST['submit']))



2) the parse error is there because as the manual says, u can prepend @ at any expression, and if is a statement, not an expression...

if u r not shure what an expression is, note this: in a line $var = <smth>, everything that can go instead of <smth> is an expression...

so u shoul do it like this

if (@$_POST['submit'])

but better way is the first with isset()...

Reply With Quote
  #10  
Old August 27th, 2002, 11:21 AM
cateyes cateyes is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Cyprus
Posts: 68 cateyes User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
RE: suppress undefined variable notice

It is finally working!!! And I just want to say thanks to all of you. Just to let you know... I followed EvilIve's advice and changed all instances of $PHP_SELF to $_SERVER['PHP_SELF'] . I also changed the syntax of the IF statements as he suggested and used quotes when I initialized my vars. However I used Zombie's suggestion of Isset which worked great. In the end it was necessary for me to initialize the vars or I repeatedly received errors.
Using your help, I was also able to get a search rountine working also. Thanks again for your help!

Reply With Quote
Reply

Viewing: Codewalkers ForumsPHP RelatedPHP Coding > suppress undefined variable notice


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway