Extracting HTML Select tag values using PHP, specifically the value 0

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • robski
    New Member
    • Feb 2007
    • 2

    #1

    Extracting HTML Select tag values using PHP, specifically the value 0

    Hello there,

    I am new to this forum and have done my best to look around for a solution to this problem.
    I am using PHP to generate an HTML Select drop down menu, with 4 options:

    function access_full($fi le, $access_level) {
    $template = 'templates/select_tags.tmp ';
    $select = file_get_conten ts($template);

    $template = 'templates/drop_down_optio n.tmp';
    $option = file_get_conten ts($template);

    for($i = 0; $i < 4; $i++) {
    $option = str_replace('#v alue#', $i, $option);
    $description = access($i);
    $option = str_replace('#d escription#', $description, $option);

    if($i == $access_level) {
    $option = str_replace('#s elected#', 'selected', $option);
    } else {
    $option = str_replace('#s elected#', '', $option);
    }

    $select = str_replace('#n ext_option#', $option, $select);
    $option = file_get_conten ts($template);
    }

    $select = str_replace('#n ext_option#', '', $select);
    $file = str_replace('#a ccess_level#', $select, $file);

    return $file;

    }

    I am using HTML templates together with the string replace function for good practise so as to keep the coupling between my code down to a minimum.

    The value selected from this drop down menu is subsequently used in an SQL statement and a database record updated:

    $new_access_lev el = clean($_POST['access_level']);

    $sql = "UPDATE users SET ACCESS_LEVEL='$ new_access_leve l' WHERE USER_ID='$user_ id'";
    qry($sql);

    The specific problem I am having is that when a user selects the option with value="0". The value stored in $new_access_lev el is '' - nothing!
    I have tried using quotes either side of the 0, both '0' and "0", but none of these solutions seem to work!

    I hope I am not the first person to experience this problem. If there is not solution, I can increment all the values by 1, but this will be rather time consuming as my website is becoming rather large now.


    All and any help would be much appreciated.

    Thanks in advance, Rob.
    [More code and info available if necessary]
  • ronverdonk
    Recognized Expert Specialist
    • Jul 2006
    • 4259

    #2
    Welcome to TSDN!

    To start, before you continue you are required to read the Posting Guidelines (top of this forum) and comply with the rules therein. One of these rules is to enclose all shown code within [php]...[/php ] tags for easier readability for our members and to prevent breaking the box because of the line lengths.

    From what you post it is difficult to pinpoint the problem. So it would make life easier to collect some information.

    1. are you sure that the 'clean' function does not remove or strip 0 values?
    2. print out the sql statement, before it is executed, to see if the 0 value is actually in the query.
    Code:
    $sql = "UPDATE users SET ACCESS_LEVEL='$new_access_level' WHERE USER_ID='$user_id'";
    echo "$sql<br>";
    3. could it be that the column you are updating does not accept 0 values because of the 'enum' setting of that column
    4. to see if there is any error in the execution of the sql statement, capture any sql errors by issuing the following INSERT command. You probably have to change the 'qry' function for that
    Code:
    mysql_query ($sql) or die ("INSERT error: ".mysql_error());
    Ronald :cool:

    Comment

    • robski
      New Member
      • Feb 2007
      • 2

      #3
      Apologies Ronald, it was late last night, so I only skimmed the guidelines :S

      I have reworded my question below:

      I am using PHP to generate an HTML Select drop down menu when a page is loaded. The page will take values stored in a database, subsequently selecting the correct option from the HTML Select tag.

      The tag values range from 0 to 3 and when fully generated, the source looks as follows:

      [HTML]
      <select class="drop_dow n" id="access_leve l" name="access_le vel">
      <option value="0" >Author</option>
      <option value="1" selected>Review er</option>
      <option value="2" >Secondary Administrator</option>
      <option value="3" >Primary Administrator</option>
      </select>
      [/HTML]

      The user is then allowed to change the selected value and to update the account profile. PHP is used to harvest the value from the source and it is then combined into an SQL statement:

      [PHP]
      $new_access_lev el = clean($_POST['access_level']);

      $sql = "UPDATE users SET ACCESS_LEVEL='$ new_access_leve l' WHERE USER_ID='$user_ id'";
      qry($sql);
      [/PHP]

      I have used two functions, one which I have written myself and the other is from 'Larry Ulmans PHP and MYSQL for dynamic websites', here:

      [PHP]
      function clean($input) {

      global $conn; // Connection to a MySQL database
      include_once('c onnect.inc.php' );

      if(!empty($inpu t)) {
      if(ini_get('mag ic_quotes_gpc') ) {
      $input = stripslashes(ht mlentities($inp ut));
      }
      return mysql_real_esca pe_string(trim( $input), $conn);
      } else {
      return null;
      }
      }

      function qry($sql) {
      return @mysql_query($s ql);
      }
      [/PHP]


      Now I was going to say, all and any help would be greatly appreciated, but I have just found the problem!
      In my clean function, I was using
      Code:
      !empty()
      , therefore anything non-zero would be allowed through. Otherwise null was returned. Hence the problem. I'd completely forgotten about that line of code :S

      All fixed now. Thank you for getting me to be more thorough Ronald. And I hope my stupidity helps someone else :d

      Regards, Robski

      Comment

      • ronverdonk
        Recognized Expert Specialist
        • Jul 2006
        • 4259

        #4
        Stupidity does not exist in programming. It happens to everyone. I am glad I doubted the 'clean()' function.

        See you again.

        Ronald :cool:

        Comment

        Working...