Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    2
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Data reuse problem

    Hello,

    I have a database with a table containing names (for example "Mr Joe Blogs" could be one of the entries.)

    On a page I have a form with a dropdown box populated from this table, which displays correctly. However, when I click submit only "Mr" is saved to the new entry in a different table. Here's my dropdown box code:

    PHP Code:
    $query "SELECT * FROM teachers";             
    $result mysql_query($query) or die(mysql_error());
    echo 
    "<select name=teacher>";
    while (
    $db_field mysql_fetch_assoc($result)) {
        
    $temp nl2br($db_field["Name"]);
        echo 
    "<option value=" $temp ">" $temp "</tr>";
    }
    echo 
    "</select>"
    In the code for collecting the information from the form and sending it to the database I have this line:
    PHP Code:
    $teacher $_POST['teacher']; 

    If I print $teacher to the screen this error shows up, but it does't show up in $temp or nl2br($db_field["Name"]) if I were to use it directly.

    Any clues why everything after the space is lost when I hit the submit button?

    David.
    Last edited by Inigoesdr; 01-18-2012 at 10:43 PM.

  • #2
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,474
    Thanks
    63
    Thanked 537 Times in 524 Posts
    Quote Originally Posted by david.moody View Post
    In the code for collecting the information from the form and sending it to the database I have this line:

    $teacher = $_POST['teacher'];


    If I print $teacher to the screen this error shows up, but it does't show up in $temp or nl2br($db_field["Name"]) if I were to use it directly.
    You're not helping yourself..

    WHAT error? - Does it say anything, show anything what?

    You've also shown very little code. The code you've shown appears to be constructing the options for a select box's html source and yet you're here complaining about the code that saves the data to your database
    I can't really think of anything to write here now...

  • #3
    Senior Coder djm0219's Avatar
    Join Date
    Aug 2003
    Location
    Wake Forest, North Carolina
    Posts
    1,312
    Thanks
    4
    Thanked 206 Times in 203 Posts
    The value in your select list must be enclosed in double quotes and you aren't closing your option tag correctly (not sure why you have a /tr instead of /option). Your select should also have double quotes are the name.

    PHP Code:
    echo '<select name="teacher">';
    while (
    $db_field mysql_fetch_assoc($result)) {
    $temp nl2br($db_field["Name"]);
    echo 
    '<option value="' $temp '">$temp</option>';
    }
    echo 
    '</select>'
    Last edited by djm0219; 01-19-2012 at 08:53 AM.
    Dave .... HostMonster for all of your hosting needs

  • #4
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    Quote Originally Posted by djm0219 View Post
    PHP Code:
    echo '<select name="teacher">';
    while (
    $db_field mysql_fetch_assoc($result)) {
    $temp nl2br($db_field["Name"]);
    echo 
    '<option value="$temp">$temp</option>';
    }
    echo 
    '</select>'
    That's not going to work; $temp won't be parsed inside of single quotes.
    PHP Code:
    echo '<select name="teacher">'
    while (
    $db_field mysql_fetch_assoc($result))

        
    $temp nl2br($db_field['Name']); 
        echo 
    '<option value="' $temp '">' $temp '</option>'

    echo 
    '</select>'

  • Users who have thanked Inigoesdr for this post:

    david.moody (01-19-2012)

  • #5
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    2
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Smile Found my own solution

    My problem was a really stupid one. Found the solution this morning after a good nights sleep.

    This is what I had (correcting the typo which was not the problem):

    echo "<option value=" . $temp . ">" . $temp . "</option>";

    Notice that I had omitted the single inverted comma sarounding the option value. Here's the corrected line, all works correctly now:

    echo "<option value='" . $temp . "'>" . $temp . "</option>";

  • #6
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,474
    Thanks
    63
    Thanked 537 Times in 524 Posts
    You also need to be using single quotes a bit more instead of doubles.

    See the 'Quotes' tip in my signature.
    I can't really think of anything to write here now...

  • Users who have thanked tangoforce for this post:

    david.moody (01-19-2012)


  •  

    Tags for this Thread

    Posting Permissions

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