General Programming Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingGeneral Programming Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old April 7th, 2004, 01:47 PM
Cabadobedia Cabadobedia is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 3 Cabadobedia User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Just trying to finish this online tutorial!

I've looked at a few PHP/MySQL ebooks, online tutorials... etc, and none of them seem to agree on anything!

From one example to the next the code just doesn't match, at all. One used a constant mysql_connect("localhost"); variable in everything he did with sql! I *really* didn't understand that one.

Anyway, I just want to understand why this isn't working, and what I can do to make it work. I've turned the Global_Whatchamidooky on, but still not working. (I'd rather not go that route anyway, when I find hosting I can't be assured they'll have it turned on).

The error I keep getting is the "undefined variables" error. I get it in *every* manual example and online tutorial. I really must be looking in the wrong places...
I've tried definding the variables with " $variable = $_POST['variable']; " but that results in an error such as..
"Notice: Undefined index: first in C:\Documents and Settings\Blake Gilroy\My Documents\My Webs\webworks\php\tut1\insert.php on line 14"

Here's the complete page I'm working with atm. It seemed (initially) to be the most recent and "together" example.
(the header and footer includes aren't part of the sample, I just wanted things to look pretty on my end)

PHP Code:
<? include("../header.html"); ?>
<form action="insert.php" method="post">
First Name: <input type="text" name="first"><br>
Last Name: <input type="text" name="last"><br>
Phone: <input type="text" name="phone"><br>
Mobile: <input type="text" name="mobile"><br>
Fax: <input type="text" name="fax"><br>
E-mail: <input type="text" name="email"><br>
Web: <input type="text" name="web"><br>
<input type="Submit">
</form>
<?
$database
="test2";
mysql_connect("localhost");
@
mysql_select_db($database) or die( "Unable to select database");
$query "INSERT INTO testtable VALUES ('','$first','$last','$phone','$mobile','$fax','$e  mail','$web')";
mysql_query($query);
mysql_close();
?> 
<? include("../footer.html"); ?>

Reply With Quote
  #2  
Old April 7th, 2004, 03:13 PM
dhouston's Avatar
dhouston dhouston is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: May 2003
Location: Tennessee
Posts: 1,355 dhouston User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via ICQ to dhouston
PHP Code:
<?php include("../header.html"); ?> 

<form action="insert.php" method="post">
First Name: <input type="text" name="first"><br>
Last Name: <input type="text" name="last"><br>
Phone: <input type="text" name="phone"><br>
Mobile: <input type="text" name="mobile"><br>
Fax: <input type="text" name="fax"><br>
E-mail: <input type="text" name="email"><br>
Web: <input type="text" name="web"><br>
<input type="Submit">
</form>

<?php
//Only do this part if you've actually got post vars.
if($_POST){ 
    
$database="test2";
    
mysql_connect("localhost");
    @
mysql_select_db($database) or die( "Unable to select database");
    
$query "INSERT INTO testtable VALUES ('','" $_POST["first"] . "','" $_POST["last"] . "','" $_POST["phone"] . "','" $_POST["mobile"] . "','" $_POST["fax"] . "','" $_POST["email"] . "','" $_POST["web"] . "')";
    
mysql_query($query) or die(mysql_error());
    
mysql_close();
}

include(
"../footer.html"); 

?>


I changed "<?" to "<?php" because that's standard now. Additionally, I stuck the actual processing into an if block so that it's only evaluated if POST variables have been sent (so if the form has been submitted -- else, you're attempting to use variables that haven't been filled, and you get the notices you're reporting). I also added a die statement to your query statement to facilitate debugging. And finally, I converted all your variables to references to the $_POST array. The code you've supplied wouldn't work with register_globals turned off, and my philosophy tends to be that it's better to just use the $_POST array rather than assigning values to other variables, as it keeps you always in scope and makes the code more self-documenting, if a little more unsightly.
__________________
Please don't PM me asking for solutions outside the scope of a thread.
Keeping all responses in a thread stands to help others who come along later,
which is after all what this forum's all about.

Reply With Quote
  #3  
Old April 7th, 2004, 03:46 PM
Cabadobedia Cabadobedia is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 3 Cabadobedia User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Excellent, thank you very much. I have one question (since the perpose of doing this was to understand it)
ok.. maybe a few questions

PHP Code:
 $query "INSERT INTO testtable VALUES ('','" $_POST["first"] . "','" $_POST["last"] . "','" $_POST["phone"] . "','" $_POST["mobile"] . "','" $_POST["fax"] . "','" $_POST["email"] . "','" $_POST["web"] . "')"


On that line what is the perpose of the underlined area's ('','" . $_POST["first"] . "','" . $_POST...
I guess it's the syntax I'm mostly not understanding here.
I'll just guess, please let me know if I'm kind of getting it.
The whole set of values is enclosed in the initial double brackets. The first comma... I understand that the first variable is enclosed in the two comma's but I don't understand why.
Then the first variable (first) is inclosed in the single quotes, but why is there a double quote after that single quote, why's that?
The period seems to be another beggining and end spot too, does that exist because this is all part of the $_POST array?

If you could even point me somewhere that would explain all those things to me that would be muchly appreciated. I know *how* to do it now, I would just like to understand what I'm doing

Reply With Quote
  #4  
Old April 8th, 2004, 10:00 AM
dhouston's Avatar
dhouston dhouston is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: May 2003
Location: Tennessee
Posts: 1,355 dhouston User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via ICQ to dhouston
This is just the notation I tend to prefer because it keeps me from having variables/arrays all jammed together inside double quotes. Basically, the dot is a concatenation operator, which you can use to glue strings and variables together. Since I don't like having arrays and variables inside long double-quoted strings, I break the string and variable/array portions apart and use the dot operator to glue them together.

There's one thing that may be confusing here -- the first variable after "VALUES(" is an empty set of single quotes rather than a double-quote. It looks sort of like a double-quote on my screen, and I could see how that would be confusing indeed.

Reply With Quote
  #5  
Old April 12th, 2004, 03:35 PM
Cabadobedia Cabadobedia is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 3 Cabadobedia User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thanks for all your help. I had taken a break from my php/mysql endeavor to move and your explanation of your notation filled in the blanks I had from before. I now feel comfortable moving on to the few more complicated things.

Thanks again!

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingGeneral Programming Help > Just trying to finish this online tutorial!


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
Stay green...Green IT