Tutorials
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Codewalkers ForumsOtherTutorials

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:
You eat, breathe and sleep innovation. Build your mobile intelligence with BlackBerry® experts this July. Register Today!
  #1  
Old August 4th, 2004, 05:42 PM
Anonymous Anonymous is offline
Registered User
Codewalkers God 35th Plane (22000 - 22499 posts)
 
Join Date: Apr 2007
Posts: 22,309 Anonymous User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 24
if statement

I tried to use this and get these errors --
Notice: Undefined variable: submit in c:inetpubwwwrootaddedit.php on line 3

Notice: Undefined variable: update in c:inetpubwwwrootaddedit.php on line 12

this is what I am typing in - actually I am copying it from your tutorials
http://codewalkers.com/tutorials/9/14.html

why do I have this issue with if statements and else statements and else if?

Is there a problem with the script?
Is there a setting I dont have setup on my php.ini file correctly?
I am new to this whole thing and would like some info on what I am doing wrong.
BTW
On this tutorial page http://codewalkers.com/tutorials/9/12.html there is more errors of script that needs to be fixed.
On the script you have :
$result = mysql_ query("SELECT * FROM personnel",$db);

Should be
$result = mysql_query("SELECT * FROM personnel",$db);

I mean I know you all may be pros at this and spot it quickly but some of us like me - who has never used PHP before and is learning this took me like 2 hours to figure out that there was a typo on your part. Is this else and if statements typos????

Reply With Quote
  #2  
Old August 4th, 2004, 07:51 PM
Ashkhan Ashkhan is offline
Contributing User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 372 Ashkhan User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 24 m 57 sec
Reputation Power: 2
RE: if statement

There is nothing wrong with the code. Author only assumes you have register_globals=on in your php.ini:

Quote:
If you don't have the register_globals=on option enabled, be sure to initialize the variables with $_GET like $first=$_GET[first];


That's why you variables are not defined. Newbie can easily skip this little notice. That option was set to off by default in versions above 4.1 or so.

In other words, instead of $variable use $_GET['variable'].


Or you can add following loop at the beginning of your script. The loop will neccessary assingments for you.

php Code:
Original - php Code
  1.  
  2. foreach($_GET as $key => $value) { $$key = $value; }

Reply With Quote
  #3  
Old August 4th, 2004, 07:55 PM
Anonymous Anonymous is offline
Registered User
Codewalkers God 35th Plane (22000 - 22499 posts)
 
Join Date: Apr 2007
Posts: 22,309 Anonymous User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 24
RE: if statement

Ok, how do you initialize the variables with $_GET like $first=$_GET[first];

register_globals is set to on , I had changed this way earlier. I dont know how to initialize variables. Do I put this into a php webpage? help

Reply With Quote
  #4  
Old August 4th, 2004, 08:01 PM
Ashkhan Ashkhan is offline
Contributing User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 372 Ashkhan User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 24 m 57 sec
Reputation Power: 2
RE: if statement

It should work if you set the register_globals to on.

By initialization he means simply this assignment:

$submit = $_GET['submit'];


Reply With Quote
  #5  
Old August 4th, 2004, 08:07 PM
Ashkhan Ashkhan is offline
Contributing User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 372 Ashkhan User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 24 m 57 sec
Reputation Power: 2
RE: if statement

To test it, create a new file.

Then copy this text:

<?php
if (isset($_GET['name'])) { echo "used GET".$_GET['name']; }
else { echo "without GET $name"; }
?>

Save it as test.php.

In your browser call it like this: http://yourserver/test.php?name=Joe

See which method works.

Reply With Quote
  #6  
Old August 4th, 2004, 08:26 PM
Anonymous Anonymous is offline
Registered User
Codewalkers God 35th Plane (22000 - 22499 posts)
 
Join Date: Apr 2007
Posts: 22,309 Anonymous User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 24
RE: if statement

I did it and it came up with "used GETJoe" on a webpage without the quotes.

If its all working then why am I getting this error then when I run a php with an if statement in there or else statement? This is strange. Im running a php VB bullentin BB on the server too and am writing this to you from the server and the bb runs great and I know there is if statements all within it. But I was doing the tutorial and I get errors when I run the page that has if statements and else statements. The tutorial is helpful but I cant run that one page.

http://codewalkers.com/tutorials/9/14.html

here is the code thats on the page

<HTML>
<?php
if($submit)
{
$db = mysql_connect("localhost", "root","");
mysql_select_db("learndb",$db);
$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";
}
else if($update)
{
$db = mysql_connect("localhost", "root","");
mysql_select_db("learndb",$db);
$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";
}
else if($id)
{
$db = mysql_connect("localhost", "root", "");
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>
E-mail:<input type="Text" name="email"><br>
Salary:<input type="Text" name="salary"><br>
<input type="Submit" name="submit" value="Enter information"></form>
<?
}
?>
</HTML>

Reply With Quote
  #7  
Old August 5th, 2004, 11:44 AM
Anonymous Anonymous is offline
Registered User
Codewalkers God 35th Plane (22000 - 22499 posts)
 
Join Date: Apr 2007
Posts: 22,309 Anonymous User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 24
RE: if statement

any ideas??

Reply With Quote
  #8  
Old August 5th, 2004, 08:02 PM
Ashkhan Ashkhan is offline
Contributing User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 372 Ashkhan User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 24 m 57 sec
Reputation Power: 2
RE: if statement

I made a few changes.

php Code:
Original - php Code
  1.  
  2. <HTML>
  3. <?php
  4. foreach($_POST as $key => $value) { $$key = $value; }
  5. $update = $_GET['update'];
  6. $id = $_GET['id'];
  7.  
  8. if($submit)
  9. {
  10. $db = mysql_connect("localhost", "root","");
  11. mysql_select_db("learndb",$db);
  12. $sql = "INSERT INTO personnel (firstname, lastname, nick, email, salary)
  13. VALUES ('$first','$last','$nickname','$email','$salary')";
  14. $result = mysql_query($sql);
  15. echo $sql;
  16. echo "<p>Thank you! Information entered.</p>n";
  17. echo "Do you want to <a href='$PHP_SELF?id=y'> UPDATE</a> your info?<br />";
  18. }
  19. else if($update)
  20. {
  21. $db = mysql_connect("localhost", "root","");
  22. mysql_select_db("learndb",$db);
  23. $sql = "UPDATE personnel SET firstname='$first',
  24. lastname='$last', nick='$nickname', email='$email', salary='$salary' WHERE id=$id";
  25. $result = mysql_query($sql);
  26. echo $sql;
  27. echo "Thank you! Information updated.n";
  28. }
  29. else if($id)
  30. {
  31. $myrow["firstname"] = "nuff";
  32. $db = mysql_connect("localhost", "root", "");
  33. mysql_select_db("learndb",$db);
  34. $result = mysql_query("SELECT * FROM personnel WHERE id=$id",$db);
  35. $myrow = mysql_fetch_array($result);
  36. ?>
  37. Update
  38. <form method="post"action="<?php echo $PHP_SELF?>?update=y">
  39. <input type="hidden"name="id"value="<?php echo $myrow["id"]?>">
  40. First name:<input type="Text"name="first"value="<?php echo $myrow["firstname"]?>"><br>
  41. Last name:<input type="Text" name="last" value="<?php echo $myrow["lastname"]?>"><br>
  42. Nick Name:<input type="Text" name="nickname" value="<?php echo $myrow["nick"]?>"><br>
  43. E-mail:<input type="Text" name="email" value="<?php echo $myrow["email"]?>"><br>
  44. Salary:<input type="Text" name="salary" value="<?php echo $myrow["salary"]?>"><br>
  45. <input type="Submit" name="update" value="Update information"></form>
  46. <?
  47. }
  48. else
  49. {
  50. ?>
  51. <form method="post" action="<?php echo $PHP_SELF?>">
  52. First name:<input type="Text" name="first" value="nuff"><br>
  53. Last name:<input type="Text" name="last"><br>
  54. Nick Name:<input type="Text" name="nickname"><br>
  55. E-mail:<input type="Text" name="email"><br>
  56. Salary:<input type="Text" name="salary"><br>
  57. <input type="Submit" name="submit" value="Enter information"></form>
  58. <?
  59. }
  60. ?>
  61. </HTML>

Reply With Quote
Reply

Viewing: Codewalkers ForumsOtherTutorials > if statement


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 |