繁体   English   中英

提交数据后尝试重定向

[英]trying to redirect after submitting data

在将一些数据提交到example.php的数据库中后,我尝试进行重定向,我具有表单并且提交完美,但无法使重定向正常工作,我尝试了几种我在互联网上看到的方法,但是没有任何效果

<html>
    <head>

        <title>MySQLi Create Record</title>

    </head>
<body>

<?php
$action = isset($_POST['action']) ? $_POST['action'] : "";

if($action=='create'){
        //include database connection
        include 'db_connect.php';

        //write query
        $query = "insert into referb 
                                set
                                        make = '".$mysqli->real_escape_string($_POST['make'])."', 
                                        model = '".$mysqli->real_escape_string($_POST['model'])."',
                                        unitt  = '".$mysqli->real_escape_string($_POST['unitt'])."'";

        if( $mysqli->query($query) ) {
                echo " header( 'Location: example.php' ) ;";
        }else{
                echo "Database Error: Unable to create record.";
        }
        $mysqli->close();
}

?>

<!--we have our html form here where user information will be entered-->
<form action='#' method='post' border='0'>
    <table>
        <tr>
            <td>Manufactor</td>
            <td><input type='text' name='make' /></td>
        </tr>
        <tr>
            <td>Model</td>
            <td><input type='text' name='model' /></td>
        </tr>
        <tr>
            <td>Unit Type</td>
            <td>
            <select name='unitt'>
  <option>Laptop</option>
  <option>Computer</option>
  <option>Tablet</option>
</select></td>
        </tr>

            <td></td>
            <td>
                <input type='hidden' name='action' value='create' />
                <input type='submit' value='Save' />

                                <a href='index.php'>Back to index</a>
            </td>
        </tr>
    </table>
</form>

</body>
</html>

对header()的调用不应回显。 该行应更改为:

header('Location: example.php');
exit();

之后调用exit()将停止脚本的进一步执行。

不要echo header("Location: example.php)". 这是一个php函数,因此您只需编写

header("Location:example.php");

您不能在标头之前或同一指令/条件中同时具有echo。

但是,您可以设置if指令。

您有echo " header( 'Location: example.php' ) ;";

它需要读为header('Location: file.php'); 只要。

进行标头重定向的正确方法是:

header('Location: example.php');

您可以执行以下操作:

if( $mysqli->query($query) ) {
    header('Location: example.php');
    }else{
            echo "Database Error: Unable to create record.";
        }
    $mysqli->close();
}

访问在该PHP.net header()函数: http://php.net/manual/en/function.header.php

步骤1:首先执行查询以将数据插入数据库

步骤2:关闭数据库连接

第3步:只需输入>>>标头(“位置:example.php”);

这是一个将用户重定向到另一个URL的php脚本,将其放在正确的条件或您的代码说明之后

 <html>
 <?php
 header('Location: http://www.example.com/');
 exit;
 ?>

暂无
暂无

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

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