dpm91915 2014-02-17 06:19
浏览 51
已采纳

在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.

  • 写回答

2条回答 默认 最新

  • dongziya9863 2014-02-17 06:34
    关注

    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;'>";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 MATLAB联合adams仿真卡死如何解决(代码模型无问题)
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改