PHP-重定向到另一个页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51188404/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:51:58  来源:igfitidea点击:

PHP- Redirect to another page

phpredirect

提问by skz

My question is very simple. but I am struggling with it too much. The problem is once I hit the submit button that page should redirect to some other page. Kindly just tell me what I have done wrong? Because I have tried everything.

我的问题很简单。但我太挣扎了。问题是一旦我点击提交按钮,页面应该重定向到其他页面。请告诉我我做错了什么?因为我什么都试过了。

Code:

代码:

Update-docket.php

更新 docket.php

<?php
//error_reporting(0);
$link = mysqli_connect("localhost", "home_db", "root", "");

// Check connection 
if($link === false){
    die("ERROR: Could not connect. " . mysqli_connect_error());
}

// Escape user inputs for security
$sql = 'SELECT * FROM prod_details';
$result = mysqli_query($link,$sql);

while ($row = mysqli_fetch_array($result))
{ 
     $Quantity1 = $_POST['p_'.$row['id']. ''];          
     $id = $row['id'];
     $store = $row['rstore'];
     // echo $store;

     foreach ($_POST as $key => $value) 
     {}

     if(!empty($Quantity1)) {
         $query="update prod_details SET rdocket='$Quantity1' WHERE id='$id'";
         if(mysqli_query($link, $query)) {
            header('Location: /docket-details.php');
            exit();
         } else {
            echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
         }
      }
  }

docket-details.php

案卷详细信息.php

<form class="form-horizontal" action="update-docket.php" method="post" name="myform" autocomplete="off">
  <button  type='submit'  name='submit' class='btn c-theme-btn c-btn-square c-btn-uppercase c-btn-bold'>Submit</button>

Please help!!!

请帮忙!!!

回答by treyBake

You're Locationline is incorrect, redirect using:

你的Location线路不正确,重定向使用:

header('Location: /url');

https://secure.php.net/manual/en/function.header.php

https://secure.php.net/manual/en/function.header.php

update

更新

I reckon your if statement is not being met. Try enabling error logging again and var_dumping inside each conditional section to see where the real problem lies. Now you've updated your code to use headerit should in theory work. But my guess is $_POST not returning what you want/expect.

我认为您的 if 语句没有得到满足。尝试再次启用错误日志记录并在每个条件部分中启用 var_dumping 以查看真正的问题所在。现在您已经更新了代码以使用header它理论上应该可以使用。但我的猜测是 $_POST 没有返回您想要/期望的内容。

回答by Zlatan

It's also best to not output anything before the header() .. eg; don't echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);

最好不要在 header() 之前输出任何内容 .. 例如;不要回显“错误:无法执行 $sql。”。mysqli_error($link);

回答by Clas ?stman

It seems your file is named "Update-docket.php", with a capital U. But in the action of the form you call for action="update-docket.php".

您的文件似乎被命名为“Update-docket.php”,大写字母 U。但是在您调用 action="update-docket.php" 的表单操作中。

回答by Reflective

Edited!

已编辑!

Try this:

尝试这个:

header('Location: /docket-details.php');
// i'm sorry, forgot to add exit
exit();

of course you can fill the full url if you refer.

当然,如果您参考,您可以填写完整的网址。

Important: No output should be made before this line as it will generate a header. Not a single echobefore sending the response header!

重要提示:在此行之前不应进行任何输出,因为它会生成一个标题。echo在发送响应头之前没有一个!

回答by RB_

It should be something like that:

它应该是这样的:

if(!empty($Quantity1)) {
    $query="update prod_details SET rdocket='$Quantity1' WHERE id='$id'";
    $res = mysqli_query($link, $query);

    if($res) {
        header('Location: /docket-details.php', true, '200');
    } else {
        echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
    }
}

回答by SuperDJ

you have "update-details.php" but you reference "update-docket.php" in your form action. So the form action redirects to a different file.

您有“update-details.php”,但您在表单操作中引用了“update-docket.php”。所以表单操作重定向到不同的文件。

As stated by @thisGuyHasTwoThumbs your Location line is incorrect.

正如@thisGuyHasTwoThumbs 所述,您的位置行不正确。

header('Location: /url');
exit();

You also want to exit()as it will prevent any execution of code after that line.

您还想这样做,exit()因为它会阻止在该行之后执行任何代码。

https://secure.php.net/manual/en/function.header.phphttps://secure.php.net/manual/en/function.exit.php

https://secure.php.net/manual/en/function.header.php https://secure.php.net/manual/en/function.exit.php

Jus for the details:

详情请看:

"update prod_details SET rdocket='$Quantity1' WHERE id='$id'"

Wil look much cleaner and is easier to read like:

Wil 看起来更干净,更容易阅读,例如:

"UPDATE `prod_details` SET `rdocket` = '$Quantity1' WHERE `id` ='$id'"

Keeps MySQL words in capital. Wrap column and table names in backticks will prevent "MySQL serverd word error"

将 MySQL 单词保持为大写。用反引号包裹列名和表名将防止“ MySQL serverd word error

Making it:

进行中:

<form class="form-horizontal" action="update-docket.php" method="post" name="myform" autocomplete="off">

<?php
if (!empty( $Quantity1) )
{
    $query = "UPDATE `prod_details` SET `rdocket` = '$Quantity1' WHERE `id` ='$id'";
    if( mysqli_query( $link, $query ) )
    {
        header( 'Location: /docket-details.php' );
        exit();
    } else {
        echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
    }
}

The variable $Quantity1is open for SQL-injection. To resolve this you can escapethe variable if it is a string but you should use prepared statement.

该变量$Quantity1对 SQL 注入是开放的。要解决此问题,如果变量是字符串,您可以对其进行转义,但您应该使用预准备语句

回答by khyati naik

header(location:pagename with extension)

标题(位置:带扩展名的页面名称)

If the above code is not running properly and it give error that "header does not change" the put the below line on the top of the page and bottom of the page respectively

如果上面的代码运行不正常,并给出“标题没有改变”的错误,请将下面的行分别放在页面顶部和页面底部

ob_start() - on the top of the page ob_end_flush() - bottom of the page

ob_start() - 在页面顶部 ob_end_flush() - 在页面底部