Jump to content

problem - spaces in an associative array values from $_POST[] variable


Go to solution Solved by mac_gyver,

Recommended Posts

My web registration form has an input field for organization, which, along with other POST variables are inserted into a  postgres user table.

Rather than an input field, I have a <select> html element with <option> tags to create a drop-down box with values from an organization table generated by a SELECT statement.

Code is as follows

<select name='org' id='org' class='form-control' required>

                                      <?php

                                          try {

                                              $result=$pdo->query("SELECT org FROM user_org ORDER BY org");

                                               foreach ($result as $row) {

                                               echo "<option value={$row['org']}>{$row['org']}</option>";

                                               $org= eval("echo '{$row['org']}';"); 

                                               

                                                }

                                              } catch(PDOException $e) {

                                                        echo "Error: ".$e->getMessage();

                                                    }

                                                ?>

                                            </select>

My PDO object has the attributes

 PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,

    PDO::ATTR_EMULATE_PREPARES=> false

Problem is spaces don't seem to be accommodated for values in the key=>value pair for associative arrays. The code works, but the organization value is truncated as soon as a space is encountered.

Example "University of Nairobi" gets truncated -  only the first word "University" is inserted into the user table.

Any workaround for this?

 

 

 

  • Solution

a space in an attribute value is a stop character, indicating the end of the value. to make this work, you need to enclose the attribute value in quotes (single or double.) you should actually always enclose attribute values in quotes, for consistency, even in those cases where they are not required.

however, the value attributes should be the organization ids, not the organization names, so that when you store the submitted data, you are storing the  organization id. this will result in the least amount of data storage, the fastest queries, and allow you to edit the organization name or even display the organization name in a different language, without affecting the stored data.

next, for the 'required' attribute to work, the first option choice needs to have an empty value attribute and serve as a prompt, e.g. <option value=''>Select an Organization</option>.

lastly, there's no good reason to catch and handle an exception from a SELECT query. any error from this type of query, is either due to a programming mistake or the database server is not running, is not recoverable by the user, and the user doesn't need to know anything about its occurrence. simply do nothing in this case and let php catch and handle any database exception from a SELECT query.

Thanks very much mac_gyver. I thought about using organization codes, and even set up a lookup table to do so. But this will require more coding to to display the options in a drop down list, and I guess I was looking for an inferior quick fix.

 

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.