在while循环中添加指向数据库调用元素的链接

问题描述:

I want to add link to the searched element which is done through ajax. In search.php I've following code but apparently it's not working.

while($row = mysql_fetch_array($res_old))
{
  echo "<li style='border-bottom: 1px solid #A5ACB2; padding:5px; margin-left:-40px;margin-right:5px;>";
  $productid=$row['productid'];
  echo "<a href='search.php?productid='$productid'>";
  echo $row['brand'];
  echo "&nbsp";
  echo $row['product_name'];
  echo "&nbsp";
  echo $row['short_desc'];
  echo "&nbsp";
  echo "&#8377;";
  echo $row['price'];
  echo "</a>";
  echo "<br>";
  echo "</li>";
}

Anybody can shade some light on why it's not working? Just link is not being added, results are coming fine.

The actual problem here is not a missing tag, but unmatched quotes.. or rather, misplaced quotes.

Here is your current opening tag:

<a href='search.php?productid='$productid'>

If you will notice the syntax highlighting, it becomes clear what is wrong here; since attribute values can be denoted by quotes, you actually end your attribute value at the first quote it sees, thus giving a malformed a tag.

If you want your code to work, you have to either escape those quotes, or do something else:

<a href='search.php?productid=$productid'>

For instance, the above has removed the quotes, so the tag parses correctly now.

Of course, this isn't the only error:

echo "<li style='border-bottom: 1px solid #A5ACB2; padding:5px; margin-left:-40px;margin-right:5px;>";
//                                                                            Right here           ^, this should be an ending single quote.

On this line, you never close your style attribute with a quote, thus messing up any following html.

The corrected line is as follows:

echo "<li style='border-bottom: 1px solid #A5ACB2; padding:5px; margin-left:-40px;margin-right:5px;'>";

Well, I am not sure whether this is the reason but aren't you missing a quote after 'productid' interpolation to balance the quote before 'search.php'?

Your Code:

echo "<a href='search.php?productid='$productid'>";

Possible Solution:

echo "<a href=\"search.php?productid='$productid'\">";

PS: I don't have much php knowledge :)