HTML表单在转到另一个网页时使用PHP将数据插入SQL

时间:2014-12-11 16:08:30

标签: php html mysql forms

我希望我的表单使用PHP将数据插入到SQL数据库中,这是一个单独的文件,而在表单提交后转到另一个网页。

<form action="http://localhost:8888/phpv1/studentadded.php" method="post">
<input type="submit" name="submit" value="Send">

目前我使用以下代码,因为它调用PHP并使其将数据提交到SQL数据库。然而,它转到空白网页(PHP页面),而不是转到备用网页。我应该添加什么代码,以便在提交时转到备用网页? 谢谢:))

这是我的完整php脚本:

<?php

如果(isset($ _ POST [&#39;提交&#39;])){

$data_missing = array();    

if(empty($_POST['email_banned'])){

    // Adds name to array
    $data_missing[] = 'Email';

} else {

    // Trim white space from the name and store the name
    $email_banned = trim($_POST['email_banned']);

}

if(empty($_POST['notes'])){

    // Adds name to array
    $data_missing[] = 'Notes';

} else {

    // Trim white space from the name and store the name
    $notes = trim($_POST['notes']);

}

if(empty($data_missing)){

    require_once('mysqli_connect.php');

    $query = "INSERT INTO banned_emails (id, email_banned, created_on, notes) VALUES ( NULL, ?, NOW(), ?)";

    $stmt = mysqli_prepare($dbc, $query);


    //i Interger
    //d Doubles         
    //s Everything Else


    mysqli_stmt_bind_param($stmt, "ss", $email_banned, $notes);

    mysqli_stmt_execute($stmt);

    $affected_rows = mysqli_stmt_affected_rows($stmt);

    if($affected_rows == 1){

        echo 'Student Entered';

        header("Location: http://localhost:8888/phpv1/test2.php");

        mysqli_stmt_close($stmt);

        mysqli_close($dbc);

    } else {

        echo 'Error Occurred<br />';
        echo mysqli_error();

        mysqli_stmt_close($stmt);

        mysqli_close($dbc);

    }

} else {

    echo 'You need to enter the following data<br />';

    foreach($data_missing as $missing){

        echo "$missing<br />";

    }

}

}

&GT;

我是否将标题放在正确的位置? (我显然会改变标题的内容)

2 个答案:

答案 0 :(得分:3)

您应该编辑studentadded.php,以便在处理完数据后将其重定向到所需的目的地。

header("Location: http://example.com/");

答案 1 :(得分:0)

我会这样做:

if(isset($_POST['submit'])) {

  //Your insert into the DB etc 

  header("location: http://www.google.com");
}