Jump to content

Parse error: parse error, unexpected $


lpxxfaintxx

Recommended Posts

[code] <html>
  <p>Category:
    <label>
    <select name="Category" id="Category"><option value="NULL">Choose A Category:</option>
    <?php $query = 'SELECT * FROM registered_cat ORDER BY cat_name ASC';
    $result = mysql_query ($query);
    while ($row = mysql_fetch_array
        ($result, MYSQL_NUM)) {
        echo "<option value='$row[0]'>$row[1]</option>"; ?>
    </select></html>[/code]

I don't know where it went wrong, but its not working no matter what I tried. Can anyone spot the error?
Link to comment
https://forums.phpfreaks.com/topic/5409-parse-error-parse-error-unexpected/
Share on other sites

you have two problems, both in the same block. you need to treat array values differently to normal variables when putting them in double quotes (use curly braces {}), and you also need a closing bracket for your while loop. so:

[code]
while ($row = mysql_fetch_array($result, MYSQL_NUM))
{
   echo "<option value='{$row[0]}'>{$row[1]}</option>"; <-- curly braces
} <--- you forgot this
?>

[/code]

that should do you
Cheers
Thanks, I'll check it out now.

edit: Ok, I have this now:

[code]<?php
$username = $userdata['user_name'];
?>
<center><form enctype='multipart/form-data' action='upload.php' method='post'>
  <p>Image Name:
    <label>
    <input type="text" name="textfield" />
    </label>
  </p>
  <p>Image Description: <br />
      <label>
      <textarea name="description" cols="50" rows="2"></textarea>
      </label>
  </p>
  <p>Category:
    <label>
    <select name="Category" id="Category"><option value="NULL">Choose A Category:</option>
    <?php $query = 'SELECT * FROM registered_cat ORDER BY cat_name ASC';
    $result = mysql_query ($query);
    while ($row = mysql_fetch_array
        ($result, MYSQL_NUM)) {
echo "&lt;option value='$row[0]'>{$row[1]}</option>";} ?>

    </select>


    </label>
    <br />
      <input type='hidden' name='MAX_FILE_SIZE' value='1048576'>
      <input name='userfile' type='file'>
    
      <input type='submit' value='Upload'>
  </p>
  <p>&nbsp;</p>
</form>[/code]

but the options "Category" doesn't grab the information from the database. It just comes out blank, the MySQL database exists and has values in it, so it must be the PHP.
The "&lt;" in this line
[code]<?php echo "&lt;option value='$row[0]'>{$row[1]}</option>";} ?>[/code]
was just to show one of the other people that your original code on this line was fine (It prevents the browser from interpreting the line). I didn't mean for you to change your line, put back the "<" character there.

Ken

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.