SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Wizard
    Join Date
    Feb 2007
    Location
    Southern California
    Posts
    1,340
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Code Check: PHP/MySQL doesn't populate the option drop-down list

    Can you see why the following code does not populate the drop-down list?

    Code:
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    <p><select size="3" name="chooseMagName">
    <option value="">Monthly Issues:</option>
    <?php
    $sqlquery2 = "SELECT mag_name GROUP BY mag_name
    	FROM cover_tiers
    	WHERE 1=1
    	ORDER BY mag_name ASC";
    	
    	if (!$sqlquery2) {
    	die('Invalid query: ' . mysql_error()); }	
    
    $sqlquery2 = mysql_query($sqlquery2);
    
    while ($result2 = mysql_fetch_array($sqlquery2))
    	{
    	$mag_name = htmlspecialchars($result2['mag_name']);
    	echo"<option value="$mag_name">$mag_name</option>\n";
    	}
    ?>
    </select></p>
    I am getting the error:

    Parse error: syntax error, unexpected T_VARIABLE, expecting ',' or ';' in /home/teamae/public_html/admin/media_tracker/v2/v2_cover_tier_home.php on line 115

    Line 115 is the echo line near the end.

    Thanks!
    Steve Husting

  2. #2
    SitePoint Enthusiast
    Join Date
    Mar 2011
    Posts
    70
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Try adding a space between echo and use single quotes. Also you should not use mysql_query, either use mysqli or PDO. The mysql extension is no longer supported.

    PHP Code:
    echo "<option value='{$mag_name}'>{$mag_name}</option>\n"

  3. #3
    SitePoint Wizard
    Join Date
    Feb 2007
    Location
    Southern California
    Posts
    1,340
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    If I post it this way:

    $mag_name = htmlspecialchars($result2['mag_name']);
    echo "<option value="$mag_name">$mag_name</option>\n";

    ... then the error continues.

    If I post it this way with . $mag_name . :

    $mag_name = htmlspecialchars($result2['mag_name']);
    echo "<option value=" . $mag_name . ">$mag_name</option>\n";

    ... then the page displays without the error, but the $mag_name variable's options don't appear.
    Steve Husting

  4. #4
    SitePoint Enthusiast
    Join Date
    Mar 2011
    Posts
    70
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I just edited my reply, sorry for the error. You have to either use single quotes to surround the value or escape the double quotes because the doubles are closing the string. Using just the "." operator is failing to place quotes around the value in the printed string. The following code will do what you want

    PHP Code:
    echo "<option value='{$mag_name}'>{$mag_name}</option>\n"
    // or
    echo "<option value=\"{$mag_name}\">{$mag_name}</option>\n"

  5. #5
    SitePoint Wizard
    Join Date
    Feb 2007
    Location
    Southern California
    Posts
    1,340
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I copied/pasted in each of your ideas in turn, and neither one echoed the variables. No error messages, either.
    Steve Husting

  6. #6
    SitePoint Enthusiast
    Join Date
    Mar 2011
    Posts
    70
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Does the html structure for the options print? Put a var_dump($result2); inside your while loop and see if it prints the values.

  7. #7
    SitePoint Wizard
    Join Date
    Feb 2007
    Location
    Southern California
    Posts
    1,340
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    The GROUP BY seems to be the problem. Without those two words, it will print it, but will repeat the row content as many times as it appears. I thought GROUP BY would show only one occurrence of the name. So the GROUP BY syntax is wrong somehow.

    This works:

    $sqlquery2 = "SELECT mag_name
    FROM cover_tiers
    WHERE 1=1
    GROUP BY mag_name
    ORDER BY mag_name ASC";
    Steve Husting

  8. #8
    SitePoint Enthusiast
    Join Date
    Mar 2011
    Posts
    70
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Glad it's working, errors with no warnings are always a pain to track down.


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
  •