Is array_key_exists('submit_input_name',$_POST) better than ($_POST["MM_insert"] == "submit_input_name")
Asked Answered
D

1

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.

Derail answered 23/4, 2013 at 0:9 Comment(2)
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 latterAstrograph
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 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.Percussive
Q
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.

Quesenberry answered 23/4, 2013 at 0:30 Comment(3)
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?Derail
If you're asking whether isset and array_key_exists produce different results, see #3211435.Quesenberry
As a side note, the actual problem was as simple as a missing "c" in <?php echo $row_getGowns['iridescence']; ?>Derail

© 2022 - 2024 — McMap. All rights reserved.