douxiaqin2062
2014-01-11 05:16
浏览 121
已采纳

在PHP中通过Header重定向

I am using wampserver for php and mysql database. in my php file, after executing some code to insert in mysql database, I want to redirect browser to another html page that has php code in it. This page needs value of the variable id to fetch data from my sql database. So I send value of id by below code in the end of php code.

header('Location: lostItem.php?id=$id');

The recieving file has below code to get value of id.

$id= $_GET['id'];

But, it turns out that there is no value of id passed i.e. the receiving file shows url :

http://localhost/Lost%20and%20Found/lostItem.php?id=$id 

instead of showing

http://localhost/Lost%20and%20Found/lostItem.php?id=11

I already have another page that sends same data (value of id) to receiving file. It has below code in it for that.

echo "<a class='listOfItems' href='lostItem.php?id=$id'>";
echo $item;
echo "</a>";

And that works fine. But when I try to do same thing by using header, it doesn't work. Before using header, I have made sure that variable $id has right integer value. But it doesn't send that value by using header.

Is it that data can't be sent by this method using header? If so, please suggest an alternative method.

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dqj29136 2014-01-11 05:19
    已采纳

    basic php notation, in single quotes variables are not interpreted

    header("Location: lostItem.php?id=$id");
    

    to be strict, location should use a full URI not a relative one

    header("Location: http://www.example.com/lostItem.php?id=$id");
    
    评论
    解决 无用
    打赏 举报
查看更多回答(2条)

相关推荐 更多相似问题