SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    Mar 2007
    Posts
    77
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP code not working... Help needed please...

    Hi all I am having a problem with a bit of my code. I will include the code. It is a drop down menu where the user can select thier age. I am going to include the code i used to create the menu and how I then insert the information into the database. It always comes up 0.

    drop down menu on html page:
    Code:
       <tr>
          <td width="165" height="27" align="right">
            Age:
          </td>
          <td width="615" height="27" align="left">
            <select name=UserAge>
                            <option value="">Leave Blank</option>
                            <? for($i = '18'; $i <= '75'; $i++)
                            {
                                    echo "<option value=\"$i\">$i</option>\n\t";
                            }
                            ?>
                    </select>
          </td>
       </tr>
    insert into db:
    Code:
    UserAge = ' ".intval($UserAge) . "',

  2. #2
    SitePoint Enthusiast mrsmiley's Avatar
    Join Date
    Jul 2004
    Location
    Melbourne
    Posts
    96
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You may not have register_globals turned on. In fact more and more PHP installs dont have it, and its off by default on newer versions. Get into the habit of using $_GET, $_POST, etc. You should also get into the habit of opening your php code sections with <?php instead of just <? otherwise you will get into trouble when you start processing xml documents.

    Ok in terms of things that I notice are wrong with the code:


    Unless there is a good reason do this:

    for($i = 18; $i <= 75; $i++)

    instead of

    for($i = '18'; $i <= '75'; $i++)

    There is no need to treat the numbers as strings and force PHP to convert it backwards and forwards for you.

    In your insert code you should try this:

    UserAge = ' ". intval($_POST['UserAge']) . "',

    assuming you are returning the data values by post request.

    Hope that helps. The quotes thing isn't the problem in this case, just something I noticed, but I'm pretty sure the cause of the problem you mentioned is the register_globals setting

  3. #3
    SitePoint Enthusiast
    Join Date
    Mar 2007
    Posts
    77
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by mrsmiley View Post
    You may not have register_globals turned on. In fact more and more PHP installs dont have it, and its off by default on newer versions. Get into the habit of using $_GET, $_POST, etc. You should also get into the habit of opening your php code sections with <?php instead of just <? otherwise you will get into trouble when you start processing xml documents.

    Ok in terms of things that I notice are wrong with the code:


    Unless there is a good reason do this:

    for($i = 18; $i <= 75; $i++)

    instead of

    for($i = '18'; $i <= '75'; $i++)

    There is no need to treat the numbers as strings and force PHP to convert it backwards and forwards for you.

    In your insert code you should try this:

    UserAge = ' ". intval($_POST['UserAge']) . "',

    assuming you are returning the data values by post request.

    Hope that helps. The quotes thing isn't the problem in this case, just something I noticed, but I'm pretty sure the cause of the problem you mentioned is the register_globals setting
    I made the changes you suggested but no luck I still get zero.

  4. #4
    SitePoint Wizard bronze trophy bigalreturns's Avatar
    Join Date
    Mar 2006
    Location
    The Wirral, England
    Posts
    1,293
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Could you post more of the code you use to process the form. The HTML select looks like it should be created properly, so the problem is more likely to be in the processing page.
    Just to confirm the select is created properly, look at the HTML generated and make sure it looks as you expect.
    "The proper function of man is to live - not to exist."
    Get a Free TomTom



Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •