dongzhimin2231
dongzhimin2231
2019-03-26 17:27
浏览 63

php如何从id获取标题网址

I am creating PHP products view details page this script disaply like this URL http://localhost/zblog/source.php?srcid=1 I want to get title URL from id like this http://localhost/zblog/source.php?title=some-title

Here is my code

   <?php
    include("config.php");
    if(isset($_GET['srcid'])) {
    $srcid = $_GET['title'];
    if($stmt = $con->prepare("SELECT * from products WHERE srcid=?")){
    $stmt->bind_param("s", $srcid);
    $stmt->execute();
    }
    $result = $con->query($sql); 
    if($result->num_rows > 0){          
    while($row = $result->fetch_assoc()){

       }
     }
    }
    ?>
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douwulu2576
    douwulu2576 2019-03-26 17:31

    Your logic is skewed, if the URL is:

    http://localhost/zblog/source.php?srcid=1
    

    then you aren't passing the title but if the URL like this:

    http://localhost/zblog/source.php?title=some-title
    

    then your if statement will never run because:

    if(isset($_GET['srcid'])) {
    

    it relies on $_GET['srcid'].

    If you want to use the title then amend that if statement to look for $_GET['title'] instead of $_GET['srcid'] or, try passing both parameters in the URL together.

    点赞 评论
  • duanhuan6336
    duanhuan6336 2019-03-26 17:33

    This would only work if your url included ?srcid=1&title=some-title

    This is because of your if statement which only runs if srcid is set.

    change

    if(isset($_GET['srcid'])) {
    

    to

    if(isset($_GET['title'])) {
    
    点赞 评论

相关推荐