为什么我的重定向不起作用?

时间:2015-11-20 22:41:24

标签: php mysql

将表单的数据发送到MySQL后,我想将用户重定向到另一个页面。 (这将是支付系统页面,但是现在我没有支付页面的URL,因此我尝试重定向到index.php)不幸的是,重定向无效。我在这里做错了什么?

mysql数据库正在接收表单的数据。我查了一下。

PHP文件:

<?php
//Only process the form if $_POST isn't empty
if (!empty( $_POST) ) {
    // Connect to MySQL
    $conn = mysqli_connect('localhost', 'root', 'root', 'lpw_');

    // Check our connection
    if (!$conn) echo 'Could not connect DB';

    // Insert our data
    $sql = "INSERT INTO play ( mobilenumber, mntimes, landlinenumber, lntimes, 
            otherphonenumber, opntimes, firstname, lastname, age, city, country, email ) 
            VALUES ( '{$conn->real_escape_string($_POST['mobilenumber'])}',
                '{$conn->real_escape_string($_POST['mntimes'])}',
                '{$conn->real_escape_string($_POST['landlinenumber'])}',
                '{$conn->real_escape_string($_POST['lntimes'])}',
                '{$conn->real_escape_string($_POST['otherphonenumber'])}',
                '{$conn->real_escape_string($_POST['opntimes'])}',
                '{$conn->real_escape_string($_POST['firstname'])}', 
                '{$conn->real_escape_string($_POST['lastname'])}',
                '{$conn->real_escape_string($_POST['age'])}',
                '{$conn->real_escape_string($_POST['city'])}',
                '{$conn->real_escape_string($_POST['country'])}',
                '{$conn->real_escape_string($_POST['email'])}' )";
    $insert = mysqli_query($conn, $sql);

    // Print response from MySQL
    if ($insert) {
        header("Location: index.php");
    } else {
        die("Error: {$conn->mysql_errno} : {$conn->error}");
    }
    // Close DB connection
    $conn->close();
}

HTML页面:

<form action="" method="post">
  <div>
        <label for="mobilenumber">Mobile Number *</label>
    <div>
       <input name="mobilenumber" type="text" id="mobilenumber" placeholder="Mobile Number">
    </div>
  </div>

  ...

  //the same kind of code, only diferent values.

  ...

  <input type="submit" name="Submit" id="Submit " value="Next step"/>

2 个答案:

答案 0 :(得分:0)

试试这段代码:

header('location:http://localhost/yourprojectname/index.php');

答案 1 :(得分:0)

exit();之后调用header("Location: index.php");,否则脚本执行将不会终止。单独设置标题不足以重定向。

if ($insert) {
    header("Location: index.php");
    exit();
}else{
   die("Error: {$conn->mysql_errno} : {$conn->error}");
}