0

I've got a handle on MySQL and HTML, but am still learning PHP. I'm on a bit of a schedule, so when I noticed that Dreamweaver would write PHP for me I started using that feature. I immediately noticed that, of course, the code it inserts isn't that great.

When investigating "Notice: Undefined index:" I came across PHP error: Notice: Undefined index:.

DeaconDesperado pointed out that alibenmessaoud's code was trying to process before post values were set. So I looked into my code for the same problem and noticed that Dreamweaver is using

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "name_of_your_submit_input"))

instead of

if(isset($_POST) && array_key_exists('name_of_your_submit_input',$_POST))

Am I misunderstanding Dreamweaver's code? Isn't checking if the post is the submit name the same as checking if it exists? Am I misunderstanding array_key_exists()? Last question, does it matter that my check is above the form itself?

Thanks for putting up with a newbie who hasn't finished the w3schools PHP tutorial yet.

Community
  • 1
  • 1
Ken Hikage
  • 51
  • 1
  • 8
  • Your second example, is checking that the overall `$_POST` is set.. So even if the input of `MM_insert` is not set, but another post is forwarded.. Your script will continue to the second param, it's better to stop it at the first check rather than latter – Daryl Gill Apr 23 '13 at 00:11
  • I hate to say it, but _that_ particular PHP-portion of Dreamweaver is... OK and just fine. `isset($_POST)` is .... catching a very, very rare edge-case... That is just as easily handled with `isset($_POST["MM_insert"])`. (it is checking whether the superglobal $_POST exists, which will only not exist if [variables_order](http://www.php.net/manual/en/ini.core.php#ini.variables-order) does not include `P`, which you will probably never encounter in your lifetime. The OP there probably believed it wasn't set on GET requests. Trust me, it is. And `isset($_POST["MM_insert"])` catches it as well. – Wrikken Apr 23 '13 at 00:32

1 Answers1

0

The two examples you've given don't do the same thing, therefore neither is better.

Your first example is asking whether key MM_insert exists in $_POST and that it's value is name_of_your_submit_input.

Whereas your second example is asking whether the key name_of_your_submit_input exists, which could also look like this:

if (isset($_POST["name_of_your_submit_input"]))

In any case, both examples wouldn't cause that PHP Notice.

noetix
  • 4,773
  • 3
  • 26
  • 47
  • But, they both do the job of verifying that the script isn't ran until the form is sent, right? Which is what eliminates alibenmessaoud's issue from being the cause of my problem, right? – Ken Hikage Apr 23 '13 at 01:42
  • If you're asking whether `isset` and `array_key_exists` produce different results, see http://stackoverflow.com/questions/3210935/difference-between-isset-and-array-key-exists. – noetix Apr 23 '13 at 03:26
  • As a side note, the actual problem was as simple as a missing "c" in `` – Ken Hikage Apr 23 '13 at 15:57