Php 重定向到URL查询字符串
我正在尝试将页面重定向到: php?rest\u id=$rest\u id&area=$rest\u city 例如: product\u page.php?rest\u id=3&area=Enfield 此时页面正在重定向到: 虽然我已指明:Php 重定向到URL查询字符串,php,Php,我正在尝试将页面重定向到: php?rest\u id=$rest\u id&area=$rest\u city 例如: product\u page.php?rest\u id=3&area=Enfield 此时页面正在重定向到: 虽然我已指明: if (isset($_GET['rest_id'])){ $rest_id = mysqli_real_escape_string($dbc,$_GET['rest_id']); } if (isset($_GET['area'
if (isset($_GET['rest_id'])){
$rest_id = mysqli_real_escape_string($dbc,$_GET['rest_id']);
}
if (isset($_GET['area'])){
$area = mysqli_real_escape_string($dbc,$_GET['area']);
}
if (isset($_GET['add_item'])) {
(int)$_GET['add_item']]){
$_SESSION['Shopping_cart_' . (int) $_GET['add_item']]+='1';
//redirect
echo"<script>window.open('product_page.php?rest_id=$rest_id&area=$rest_city','_self')</script>"; //header does not work
}
if(isset($\u GET['rest\u id'])){
$rest\u id=mysqli\u real\u escape\u字符串($dbc,$\u GET['rest\u id']);
}
如果(isset($_GET['area'])){
$area=mysqli\u real\u escape\u字符串($dbc,$\u GET['area']);
}
如果(isset($\u GET['add\u item'])){
(int)$\u GET['add\u item']]{
$_会话['Shopping\u cart.'(int)$_GET['add\u item']]+='1';
//重定向
echo“window.open('product\u page.php?rest\u id=$rest\u id&area=$rest\u city','u self');//标头不起作用
}
我如何让回声重新指向正确的页面
我尝试过的:
- 在同一if资源中调用add_item、rest_id和rest_city
- 在回显后移动$\u GET rest\u id和rest\u city结束标记
以上两个选项只是阻止页面一起重新定向,这是我第一次这样做,任何建议或提示都将不胜感激。代码中存在许多语法错误。此外,回显字符串的方式也是错误的。您需要使用PHP串联运算符将变量与字符串连接起来,否则使用双引号
下面修复了您的错误:
if (isset($_GET['rest_id'])){
$rest_id = mysqli_real_escape_string($dbc,$_GET['rest_id']);
}
if (isset($_GET['area'])){
$area = mysqli_real_escape_string($dbc,$_GET['area']);
}
if (isset($_GET['add_item'])) {
$_SESSION['Shopping_cart_'] = (int) $_GET['add_item']+='1';
echo"<script>window.open('product_page.php?rest_id=" . $rest_id . "&area=" . $rest_city . "','_self')</script>";
}
if(isset($\u GET['rest\u id'])){
$rest\u id=mysqli\u real\u escape\u字符串($dbc,$\u GET['rest\u id']);
}
如果(isset($_GET['area'])){
$area=mysqli\u real\u escape\u字符串($dbc,$\u GET['area']);
}
如果(isset($\u GET['add\u item'])){
$_会话['Shopping\u cart']=(int)$_GET['add\u item']+='1';
echo“window.open('product_page.php?rest_id=“.$rest_id.”和area=“.$rest_city.”,'u self');
}
代码中存在许多语法错误。此外,回显字符串的方式也是错误的。您需要使用PHP连接运算符将变量与字符串连在一起,否则使用双引号
下面修复了您的错误:
if (isset($_GET['rest_id'])){
$rest_id = mysqli_real_escape_string($dbc,$_GET['rest_id']);
}
if (isset($_GET['area'])){
$area = mysqli_real_escape_string($dbc,$_GET['area']);
}
if (isset($_GET['add_item'])) {
$_SESSION['Shopping_cart_'] = (int) $_GET['add_item']+='1';
echo"<script>window.open('product_page.php?rest_id=" . $rest_id . "&area=" . $rest_city . "','_self')</script>";
}
if(isset($\u GET['rest\u id'])){
$rest\u id=mysqli\u real\u escape\u字符串($dbc,$\u GET['rest\u id']);
}
如果(isset($_GET['area'])){
$area=mysqli\u real\u escape\u字符串($dbc,$\u GET['area']);
}
如果(isset($\u GET['add\u item'])){
$_会话['Shopping\u cart']=(int)$_GET['add\u item']+='1';
echo“window.open('product_page.php?rest_id=“.$rest_id.”和area=“.$rest_city.”,'u self');
}
您应该使用双qotes[“string$variable string”]让变量在字符串中工作,否则您应该用[]符号连接字符串
echo"<script>window.open('product_page.php?rest_id=$rest_id&area=$rest_city','_self')</script>";
echo“window.open('product\u page.php?rest\u id=$rest\u id&area=$rest\u city','u self');
这将不起作用,因为只有一个qoute[']
关于您应该使用双qotes[“string$variable string”]让变量在字符串中工作,或者如果不是,您应该将字符串与[]符号连接起来
echo"<script>window.open('product_page.php?rest_id=$rest_id&area=$rest_city','_self')</script>";
echo“window.open('product\u page.php?rest\u id=$rest\u id&area=$rest\u city','u self');
这将不起作用,因为只有一个qoute[']
关于我正在创建一个购物车。因此,如果在sessionmy登录表单中添加了一个项目,我没有one@Monroe我指的是你的表格。
$\u从哪里获得['rest\u id']
被分配哦,抱歉,误解。这是我数据库中的一个餐厅id。创建时每个餐厅都有一个rest\u id。@Monroe我认为问题在于变量的分配。由于它回显为空,这意味着没有分配值,可能查询失败?我正在创建一个购物车。因此,如果在会话中添加了一个项目,请添加1nmy登录表单?我没有one@Monroe我指的是你的表格。$\u从哪里获得['rest\u id']
已分配哦,抱歉,误解。这是我数据库中的一个餐厅id。创建时每个餐厅都有一个rest\u id。@Monroe我认为问题在于变量的分配。由于它回显为空,这意味着没有分配值,可能查询失败?