Re: test if $_POST variable is set [message #175836 is a reply to message #175831] |
Sat, 29 October 2011 13:26 |
Jerry Stuckle
Messages: 2598 Registered: September 2010
Karma:
|
Senior Member |
|
|
On 10/29/2011 1:40 AM, cerr wrote:
> Hi There,
>
> I have a form with various checkboxes, they got names i have from a db
> to identify them.
> Now I would like to check what has been posted by the form. For that I
> would like to read all the field names from the db and check if they
> have been set something like this:
>
> $result = mysql_query("SELECT * FROM specialty");
> while($row = mysql_fetch_array($result)) {
> if (isset($_POST[$row['name']])) {
> var_dump($POST);
> session_unset();
> }
>
> But this doesn't seem to work the way I expected it to? I.e. I would
> liek to check if $_POST['MyCheckbox'] is set now but MyCheckbox is a
> name from the database. How do I do this best, the easiest way?
>
> Thanks,
> Ron
What do you mean it "doesn't seem to work the way I expected it to"?
What do you expect to happen?
A couple of things:
First, you're not checking to see if your query worked. Always check
the return value from mysql_query() (and other external calls). There
could be any number of reasons why a query will fail; you're assuming it
worked but it might be failing.
Second, don't use "SELECT * .." when fetching from a database. Specify
the columns you wish (and only the columns you wish). "SELECT * ..."
will fetch unnecessary columns, especially if there is a change to the
database later (i.e. what happens if you add a 2M/row BLOB column to the
database later?).
Or what could be even worse - you're looking for a column in the
database called 'name'. What happens if later someone changed the
column to 'option'? Your 'SELECT *' will still work, but you'll no
longer fetch a value $row['name']. Depending on your code and error
message settings, this can be a very difficult problem to find. If you
specified 'SELECT name...' instead, the problem will be obvious.
As for the rest of your code - for every checkbox found checked you will
print the entire $_POST array, then unset all values in your $_SESSION
variable. Is this what you want?
Other than those comments, without knowing exactly what you expect, it's
hard to say what's not meeting your expectations.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
|
|
|