Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Re: [PHP-DB] check out this , if block is not executing ............
amol patil wrote:
> entered information still not shown in table of database
>
> is below code is right ?
> form name and type values are 'submit' . then
>
> what is wrong with this code,
>
>
> <?php
>
> if(isset($_POST['submit']))
> {
>
>  $dbh=mysql_connect ("localhost", "root") or die ('I cannot connect to
> the database because: ' . mysql_error());
> mysql_select_db ("dollar1_allinfo");
>
>  mysql_query("INSERT INTO totalinfo (Username,Password) VALUES
> ('$loginusername','$loginpassword')")or die (mysql_error());

How many times are you going to post this. The problem isn't with your
database (yet). Why don't you go back to the basics and learn how to use
PHP variables from forms with register_globals OFF (I assume). Or learn
some basic debugging like others have told you.

Assume you have a form like this:

<form method="get" action="test.php">
<input type="text" name="username" value="" />
<input type="submit" name="submit" value="Enter" />
</form>

Now, here's what's going to happen. There are two ways to "submit" this
form. You can put some text in the box and click the submit button or
you can put some text in the box and hit the enter key (that will
normally submit the form, also).

When the form is submitted the first way and register_globals is ON in
php.ini, then you'll have two variables available to you:

$username = the value in the text box
$submit = its value will be "Enter" since you clicked on it

When the form is submitted the second way, $submit will not be set at
all (no value) and $username will still be what was in the text box.

So if you are checking for if($submit) and register_globals is ON, then
only the second method is going to come out to TRUE and execute your
database code. You could check for if(!empty($username)) if you didn't
want to rely upon the submit button, btw.

When the form is submitted either way and register_globals is OFF,
$username and $submit will not exist. Checking for if($submit) will
never be true. Instead, you're going to have two other variables
available to you:

$_GET['username'] = the value in the text box
$_GET['submit'] = its value will be "Enter" (if the button was clicked)

So if you wanted your database code to run if the form was filled out,
you could check for if($_GET['submit']) or if(!empty($_GET['username']).

In either case instead of checking for if($variable), you should
actually be checking for if(isset($variable)) to prevent any warnings
from being shown if $variable is not set.

Also, the $_GET variables shown above are only set because you used
method="get" in your form. If you had used method="post", then you'd
have $_POST['username'] and (possibly) $_POST['submit']. You also have
$_REQUEST['username'] and $_REQUEST['submit'] when using either "get" or
"post" in your form method ($_REQUEST is a combination of get, post and
cookie data).

I know that's a lot of info and we haven't even gotten into why your
database query will probably fail eventually. Wherever you learned PHP
didn't do a very good job or if you're learning it yourself, you need to
spend some more time on the basics before getting this "difficult",
apparently. Take your database out of the equation and spend some more
time learning how forms, register_globals and variables work.

--

---John Holmes...

Amazon Wishlist: www.amazon.com/o/registry/3BEXC84AB3A5E/

php|architect: The Magazine for PHP Professionals – www.phparch.com

Report this thread to moderator Post Follow-up to this message
Old Post
John Holmes
12-27-04 08:55 PM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

PHP DB archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 08:29 AM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.