2

Edited version with working code:

Table schema:

category (catid, catname)

product (productid, catid, productname)

if(isset($_POST['submit'])); {

if(isset($_GET['id']) && is_numeric($_GET['id']) && $_GET['id'] > 0) {
//get the id
$productid = (int) $_GET['id'];
} else {
    echo 'error'; }

$sql = $link->prepare("SELECT p.productid, p.catid pid, p.productname, c.catid cid, c.catname FROM product p
JOIN category c
WHERE p.productid = $productid");
$sql-> execute();
$result = $sql-> fetchall(PDO::FETCH_ASSOC);
    $option = '';
foreach ($result as $row) {
    $pid = $row['pid'];
    $productname = $row['productname'];
    $catid = $row['cid'];
    $catname = $row['catname'];
    $option .= '<option name="'.$catname.'" value="'.$catid.'" '.($pid==$catid ? 'selected="selected"' : '').'>'.$catname.'</option>'."\r\n";
}

then in page:

<select name="xxx">
<? echo $option ?>
</select>
6
  • What is not working? Do you get any error while running this code? Commented Jul 13, 2012 at 3:45
  • Can you be a little more specific about what is not working? Also, you don't need the carriage return line feeds ("\r\n"). Commented Jul 13, 2012 at 3:46
  • What happens when you run that query manually? Commented Jul 13, 2012 at 4:04
  • I'm not an SQL expert, but you may have to change FROM category c to FROM category AS c, and product p to product AS p. Also, sanitize your input data or face the consequences! Commented Jul 13, 2012 at 4:06
  • 1
    here you write wrong logic ($catid==$catid ) Commented Jul 13, 2012 at 4:21

1 Answer 1

1

In your foreach loop you are executing only a query not iterating the result set data. If it is not, then please explain your query() method.

Sign up to request clarification or add additional context in comments.

1 Comment

Yea helps to have extra eyes I see what happened now. edited my original post to show working code

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.