1

I have a select input i need populate it from a db and select the value that matches a variable product. I have

while ($row=mysqli_fetch_array($result,MYSQLI_ASSOC)){
 echo "<option value='".$row['pname']."'>".$row['pname']."<?php
 if($row['pname']==$product) echo 'selected'</option>";

Select Entries are populated from pname and the option with name that equals $product should be selected. Im getting this error syntax error,

unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in

1
  • Show us the rest of that error message, the part that gives the line number, and indicate that line in the code. Although these errors usually arise because of errors in some line above it, this is the minimum required fro people to answer this question. Commented Mar 16, 2015 at 6:48

2 Answers 2

1

Check this one -

while ($row=mysqli_fetch_array($result,MYSQLI_ASSOC)) {
    $selected = '';

    if ($row['pname'] == $product) {
        $selected = 'selected';
    }

    echo '<option value="' . $row['pname'] . '" ' . $selected . '>' . $row['pname'] . '</option>';
}
Sign up to request clarification or add additional context in comments.

Comments

1

Try this

while ($row=mysqli_fetch_array($result,MYSQLI_ASSOC)) {
   echo "<option value='".$row['pname']."' ";
   if($row['pname']==$product) echo 'selected';
   echo ">".$row['pname']."</option>";
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.