php注册表单问题

时间:2016-09-08 07:32:35

标签: php

我是php的新手,我正在尝试注册用户代码,但它无法正常工作。如果我注释掉标题('位置:login.php');然后它将数据保存在数据库中,如果我取消注释这个标题(' location:login.php');然后它不会将数据保存在数据库中并打开登录页面。

<html>
    <head>
        <title>records...</title>
    </head>
    <body>
        <form action="registration.php" method="post">
            <table border="1" align="center" cellspacing="2">
                <tr>
                    <th>ID</th>
                    <td><input type="text" name="id"></td>
                </tr>
                <tr>
                    <th>username</th>
                    <td><input type="text" name="username"></td>
                </tr>
                <tr>
                    <th>password</th>
                    <td><input type="password" name="password"></td>
                </tr>
                <tr>
                    <th>email</th>
                    <td><input type="text" name="email"></td>
                </tr>
                <tr>
                    <th>contact</th>
                    <td><input type="text" name="contact"></td>
                </tr>
                <tr>
                    <td colspan="2" align="center" style="color: red"><input type="submit" name="submit" value="submit"></td>
                </tr>
            </table>
        </form>
        <?php
            error_reporting(0);

            if(isset($_REQUEST["submit"])){
                //header('location:login.php');

                require 'conn.php';
                $id=$_REQUEST['id'];
                $username=$_REQUEST['username'];
                $password=$_REQUEST['password'];
                $email=$_REQUEST['email'];
                $contact=$_REQUEST['contact'];
                $query="insert into dhr values('$id','$username','$password','$email','$contact')";

                $result=  mysql_query($query);
                if($result>0){
                    echo"insert successfully..";
                } else {
                    echo "not inserted".  mysql_error();
                }
            }
        ?>
    </body>
</html>

3 个答案:

答案 0 :(得分:1)

在数据库中保存值之前,您正在重定向。在数据库中保存值后使用header()功能。这样做。这是您完整正确的代码。

<?php
    error_reporting(0); 
    if(isset($_REQUEST["submit"])){ 
        require 'conn.php';
        $id = $_REQUEST['id'];
        $username = $_REQUEST['username'];
        $password = $_REQUEST['password'];
        $email = $_REQUEST['email'];
        $contact = $_REQUEST['contact'];
        $query = "insert into dhr values('$id','$username','$password','$email','$contact')";               
        if(mysql_query($query)){
            //echo"insert successfully..";
            header('location:login.php');
        }else{
            echo "not inserted".  mysql_error();
        }   
    }   
?>
<html>
    <head>
        <title>records...</title>
    </head>
    <body>
        <form action="registration.php" method="post">
            <table border="1" align="center" cellspacing="2">
                <tr>
                    <th>ID</th>
                    <td><input type="text" name="id"></td>
                </tr>
                    <tr>
                    <th>username</th>
                    <td><input type="text" name="username"></td>
                </tr>
                <tr>
                    <th>password</th>
                    <td><input type="password" name="password"></td>
                </tr>
                <tr>
                    <th>email</th>
                    <td><input type="text" name="email"></td>
                </tr>
                <tr>
                    <th>contact</th>
                    <td><input type="text" name="contact"></td>
                </tr>
                <tr>
                    <td colspan="2" align="center" style="color: red"><input type="submit" name="submit" value="submit"></td>
                </tr>


            </table>
        </form>

    </body>
</html>
  

请记住,在任何实际输出之前必须调用header()   通过普通HTML标记,文件中的空行或PHP发送。   click here for more details about header function

答案 1 :(得分:0)

这是因为您只是在检查form是否已发布后重定向

if(isset($_REQUEST["submit"])){


                header('location:login.php');// this will redirect you without executing your code below this Beacuse you have written it here

只需将其写在insert query下面 像

if($result>0){

               echo"insert successfully..";
                header('location:login.php');// if you want redirect from here
            }

答案 2 :(得分:0)

you not to be define id in this form.because ID should be autoincrement. then after you create a login page(login.php) then two fields username and password same as just like work on registration page. 


<html>
    <head>
        <title>records...</title>
    </head>
    <body>
        <form action="" method="post">
            <table border="1" align="center" cellspacing="2">

                <tr>
                    <th>username</th>
                    <td><input type="text" name="username"></td>
                </tr>
                <tr>
                    <th>password</th>
                    <td><input type="password" name="password"></td>
                </tr>
                <tr>
                    <th>email</th>
                    <td><input type="text" name="email"></td>
                </tr>
                <tr>
                    <th>contact</th>
                    <td><input type="text" name="contact"></td>
                </tr>
                <tr>
                    <td colspan="2" align="center" style="color: red"><input type="submit" name="submit" value="submit"></td>
                </tr>
            </table>
        </form>
        <?php
           if(isset($_REQUEST["submit"])){
                require 'conn.php';
                $username=$_REQUEST['username'];
                $password=$_REQUEST['password'];
                $email=$_REQUEST['email'];
                $contact=$_REQUEST['contact'];
                $query="insert into dhr(username,password,email,contact)values('$id','$username','$password','$email','$contact')";

                $result=  mysql_query($query);
                if($result>0){
                   // echo"insert successfully..";
                      header('location:login.php');
                } else {
                    echo "not inserted".  mysql_error();
                }
            }
        ?>
    </body>
</html>