简体   繁体   English

PHP-重定向到另一个页面

[英]PHP- Redirect to another page

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 案卷,details.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!!! 请帮忙!!!

You're Location line 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. 我认为您的if陈述未得到满足。 Try enabling error logging again and var_dumping inside each conditional section to see where the real problem lies. 尝试再次启用错误日志记录,并在每个条件部分中启用var_dumping以查看真正的问题所在。 Now you've updated your code to use header it should in theory work. 现在,您已经更新了代码,以使用理论上可以正常使用的header But my guess is $_POST not returning what you want/expect. 但是我的猜测是$ _POST没有返回您想要/期望的东西。

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. 当然,如果您引用,则可以填写完整的URL。

Important: No output should be made before this line as it will generate a header. 重要说明:在此行之前不应进行任何输出,因为它将生成标头。 Not a single echo before sending the response header! 发送响应头之前,没有一个echo

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);
    }
}

you have "update-details.php" but you reference "update-docket.php" in your form action. 您有“ update-details.php”,但在表单操作中引用了“ update-docket.php”。 So the form action redirects to a different file. 因此,表单操作将重定向到另一个文件。

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.php https://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. 使MySQL字词大写。 Wrap column and table names in backticks will prevent " MySQL serverd word error " 在反引号中包装列和表名将防止出现“ 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 $Quantity1 is open for SQL-injection. 变量$Quantity1已打开以进行SQL注入。 To resolve this you can escape the variable if it is a string but you should use prepared statement . 要解决此问题,可以对变量为字符串进行转义 ,但应使用预处理语句

It's also best to not output anything before the header() .. eg; 最好不要在header()之前输出任何内容。 don't echo "ERROR: Could not able to execute $sql. " . 不要回显“错误:无法执行$ sql。”。 mysqli_error($link); mysqli_error($链接);

看来您的文件名为“ Update-docket.php”,大写字母U。但是在表单的操作中,您需要输入action =“ update-docket.php”。

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()-在页面底部

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM