用户信息不会发送到数据库

时间:2014-02-07 02:12:09

标签: php html login registration

我是设计网站的新手,我最近想在我的网站上添加登录/注册表单我有一个数据库,并编写了所有表格,但是当我尝试注册时,我无法访问,因为我的信息没有添加到数据库。我一直在研究这个问题,关于如何改进我的表格和注册系统的任何建议都会有所帮助。提前谢谢!

以下是我的表格:

注册:

<?php 
session_start(); 
include "dbConfig.php"; 
if (isset($_GET["op"]) == "login") {
    if (!$_POST["username"] || !$_POST["password"]) { 
        die("You need to provide a username and password."); 
    } 
    // Create query 
    $q = "SELECT * FROM `gd_database` " 
        ."WHERE `username`='".$_POST["username"]."' " 
        ."AND `password`=PASSWORD('".$_POST["password"]."') " 
        ."LIMIT 1"; 
    // Run query 
    $r = mysql_query($q); 
    if ( $obj = @mysql_fetch_object($r) ) { 
        // Login good, create session variables 
        $_SESSION["valid_id"] = $obj->id; 
        $_SESSION["valid_user"] = $_POST["username"]; 
        $_SESSION["valid_time"] = time(); 
        // Redirect to member page 
        Header("Location: members.php"); 
    } else { 
        // Login not successful 
        die("Sorry, could not log you in. Wrong login information."); 
    } 
} else { 
    echo "<form action=\"?op=login\" method=\"POST\">"; 
    echo "Username: <input name=\"username\" size=\"15\">"; 
    echo "Password: <input type=\"password\" name=\"password\" size=\"8\">"; 
    echo "<input type=\"submit\" value=\"Login\">"; 
    echo "</form>"; 
} 
?>

登录:

<?php 
session_start(); 
include "dbConfig.php"; 
if (isset($_GET["op"]) == "login") { 
    if (!$_POST["username"] || !$_POST["password"]) { 
        die("You need to provide a username and password."); 
    } 
    // Create query 
    $q = "SELECT * FROM `gd_database` " 
        ."WHERE `username`='".$_POST["username"]."' " 
        ."AND `password`=PASSWORD('".$_POST["password"]."') " 
        ."LIMIT 1"; 
    // Run query 
    $r = mysql_query($q); 
    if ( $obj = @mysql_fetch_object($r) ) { 
        // Login good, create session variables 
        $_SESSION["valid_id"] = $obj->id; 
        $_SESSION["valid_user"] = $_POST["username"]; 
        $_SESSION["valid_time"] = time(); 
        // Redirect to member page 
        Header("Location: members.php"); 
    } else { 
        // Login not successful 
        die("Sorry, could not log you in. Wrong login information."); 
    } 
} 
else 
{ 
    echo "<form action=\"?op=login\" method=\"POST\">"; 
    echo "Username: <input name=\"username\" size=\"15\">"; 
    echo "Password: <input type=\"password\" name=\"password\" size=\"8\">"; 
    echo "<input type=\"submit\" value=\"Login\">"; 
    echo "</form>";
} 
?>

2 个答案:

答案 0 :(得分:0)

您的登录和注册脚本是相同的代码,他们认为您已经有一个帐户正在尝试验证凭据。您需要先使用INSERT语句将表单中的数据插入数据库中。现在,您只是在空表中检查帐户。

查找一些注册用户的教程。 Tutsplus很不错。

答案 1 :(得分:0)

这是注册代码

     register.php

    <?php       

        include ("dbConfig.php");

        if ( $_GET["op"] == "reg" )

        {

            $bInputFlag = false;

            foreach ( $_POST as $field )

            {

                if ($field == "")

                {

                    $bInputFlag = false;

                }

                else

                {

                    $bInputFlag = true;

                }

            }

            if ($bInputFlag == false)

            {

                die( "Problem with your registration info. "

                ."Please go back and try again.");

            }

            $q = "INSERT INTO `dbUsers` (`username`,`password`,`email`) "

            ."VALUES ('".$_POST["username"]."', "

            ."PASSWORD('".$_POST["password"]."'), "

            ."'".$_POST["email"]."')";

            $r = mysql_query($q);

            if ( !mysql_insert_id() )

            {

                die("Error: User not added to database.");

            }

            else

            {

                Header("Location: register.php?op=thanks");

            }

        }

        elseif ( $_GET["op"] == "thanks" )

        {

            echo "<h2>Thankyou for registering!</h2>";

        }

        else

        {

            echo "<form action=\"?op=reg\" method=\"POST\">\n";

            echo "Username: <input name=\"username\" MAXLENGTH=\"16\"><br />\n";

            echo "Password: <input type=\"password\" name=\"password\" MAXLENGTH=\"16  

\ “&GT; \ n” 个;

            echo "Email Address: <input name=\"email\" MAXLENGTH=\"25\"><br />\n";

            echo "<input type=\"submit\">\n";

            echo "</form>\n";

        }

    ?>

为了将数据添加到数据库中,您需要使用查询INSERT.hope此代码可以帮助您解决问题。