|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
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
|
|||
|
|||
|
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! |
|
#2
|
|||
|
|||
|
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. |
|
#3
|
|||
|
|||
|
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. |
|
#4
|
|||
|
|||
|
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! |
|
#5
|
|||
|
|||
|
RE: suppress undefined variable notice
Could you show us some code?
|
|
#6
|
|||
|
|||
|
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> <? } ?> |
|
#7
|
|||
|
|||
|
RE: suppress undefined variable notice
Are you doing something like:
if(array_key_exists('submit', $_POST)) $submit = $_POST['submit']; else $submit = ''; |
|
#8
|
|||
|
|||
|
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 |
|
#9
|
|||
|
|||
|
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()... |
|
#10
|
|||
|
|||
|
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! |
![]() |
| Viewing: Codewalkers Forums > PHP Related > PHP Coding > suppress undefined variable notice |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|
|
|