0

我在将一些数据提交到我的数据库到 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>
4

5 回答 5

1

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

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

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

于 2013-08-17T15:00:12.110 回答
0

您不能在标题之前或在同一指令/条件内同时使用 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

于 2013-08-17T16:08:43.307 回答
0

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

第二步:关闭数据库连接

第3步:只需放>>> header('Location: example.php');

于 2013-08-17T17:06:31.727 回答
0

不要echo header("Location: example.php)". 它是一个 php 函数,所以你只需编写

header("Location:example.php");
于 2013-08-17T15:02:35.033 回答
-4

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

 <html>
 <?php
 header('Location: http://www.example.com/');
 exit;
 ?>
于 2013-08-17T15:03:25.557 回答