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

在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条)

报告相同问题?

悬赏问题

  • ¥15 echarts动画效果失效的问题。官网下载的例子。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加