Re: check out this , if block is not executing ............

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux