简体   繁体   English

为什么不将数据存储在我的数据库中?

[英]Why won't the data be stored in my database?

Sorry I am new to php so please be patient with me. 抱歉,我是php新手,请耐心等待。 I am creating a user interface and when I register it says I have registered but it doesn't store the data into the database. 我正在创建一个用户界面,当我注册它时说我已经注册,但是它没有将数据存储到数据库中。 can someone please help me! 有人可以帮帮我吗!

 <?PHP
$uname = "";
$pword = "";
$errorMessage = "";
$num_rows = 0;

function quote_smart($value, $handle) {

   if (get_magic_quotes_gpc()) {
       $value = stripslashes($value);
   }

   if (!is_numeric($value)) {
       $value = "'" . mysql_real_escape_string($value, $handle) . "'";
   }
   return $value;
}

if ($_SERVER['REQUEST_METHOD'] == 'POST'){


    $uname = $_POST['username'];
    $pword = $_POST['password'];

    $uname = htmlspecialchars($uname);
    $pword = htmlspecialchars($pword);

    $uLength = strlen($uname);
    $pLength = strlen($pword);

    if ($uLength >= 10 && $uLength <= 20) {
        $errorMessage = "";
    }
    else {
        $errorMessage = $errorMessage . "Username must be between 10 and 20 characters" . "<BR>";
    }

    if ($pLength >= 8 && $pLength <= 16) {
        $errorMessage = "";
    }
    else {
        $errorMessage = $errorMessage . "Password must be between 8 and 16 characters" . "<BR>";
    }



    if ($errorMessage == "") {

    $user_name = "root";
    $pass_word = "";
    $database = "user authentication";
    $server = "127.0.0.1";

    $db_handle = mysql_connect($server, $user_name, $pass_word);
    $db_found = mysql_select_db($database, $db_handle);

    if ($db_found) {

        $uname = quote_smart($uname, $db_handle);
        $pword = quote_smart($pword, $db_handle);


        $SQL = "SELECT * FROM login WHERE USERNAME = $uname";
        $result = mysql_query($SQL);
        $num_rows = mysql_num_rows($result);

        if ($num_rows > 0) {
            $errorMessage = "Username already taken";
        }

        else {

            $SQL = "INSERT INTO login (L1, L2) VALUES ($uname, md5($pword))";

            $result = mysql_query($SQL);

            mysql_close($db_handle);

        //=================================================================================
        //  START THE SESSION AND PUT SOMETHING INTO THE SESSION VARIABLE CALLED login
        //  SEND USER TO A DIFFERENT PAGE AFTER SIGN UP
        //=================================================================================

            session_start();
            $_SESSION['login'] = "1";

            header ("Location: page1.php");

        }

    }
    else {
        $errorMessage = "Database Not Found";
    }




    }

}


?>

    <html>
    <head>
    <title>Basic Login Script</title>


    </head>
    <body>


<FORM NAME ="form1" METHOD ="POST" ACTION ="signup.php">

Username: <INPUT TYPE = 'TEXT' Name ='username'  value="<?PHP print $uname;?>" maxlength="20">
Password: <INPUT TYPE = 'TEXT' Name ='password'  value="<?PHP print $pword;?>" maxlength="16">

<P>
<INPUT TYPE = "Submit" Name = "Submit1"  VALUE = "Register">


</FORM>
<P>

<?PHP print $errorMessage;?>

    </body>
    </html>

You might also want to rather make use of PDO then you don't have to to do the cleanup of the user input as PDO will take care of that for you. 您可能还想使用PDO,然后不必清理用户输入,因为PDO会为您处理。 You might want to creat a file that hold all your connection details like this: 您可能想要创建一个包含所有连接详细信息的文件,如下所示:

   <?php

          define('DB_HOST', 'localhost');
          define('DB_NAME', 'user authentication');
          define('DB_USER', 'root');
          define('DB_PASS', '');
          define('DSN', 'mysql:host='. DB_HOST . ';dbname=' . DB_NAME);

    ?>

You then might want to create a class to do the connection to your database like: 然后,您可能想要创建一个类来建立与数据库的连接,例如:

<?php

    class database{

    public function databaseConnect(){
            /*Creates a new instance of the PDO called $db.
             * NOTE: the use of DSN, DB_USER and so on.  These variable live in the dbsettings file.
            */
            $db = new PDO(DSN,DB_USER,DB_PASS);
            $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            return $db;
        }
    }

?>

Then you might want to create a class to register your user like: 然后,您可能想要创建一个类来注册用户,如下所示:

<?php

    //Include the database class file to allow access to properties and methods within that class.
    require_once 'class.database.php';


    //echo 'I am database class file now included in the users class file. <br />';

    //This method will be user to check if the user enter the correct username password pair.
    class users{


        public function checkValidUser($username){
            $userExists = false;
            try {
                $db = database::databaseConnect();

                $stmt = $db->prepare('SELECT uname FROM table WHERE uname=:username');
                $stmt->bindParam(':uname', $username, PDO::PARAM_STR);
                $stmt->execute();

                if ($stmt->rowCount() == 1){
                    $userExists = true;
                }

                $db = null;

            } catch (PDOException $e) {
                $userExists = false;
            }
            return $userExists;
        }

        public function addUser($firstname, $lastname, $username,$password){
            $success = true;
            //Connect to the database
            try {
                $db = database::databaseConnect();
                //$db->databaseConnect();

                $stmt = $db->prepare('INSERT INTO table (FirstName, LastName, Username, Password) VALUES (:firstname, :lastname, :username, :password)');
                $stmt->bindParam(':firstname', $firstname, PDO::PARAM_STR);
                $stmt->bindParam(':lastname', $lastname, PDO::PARAM_STR);
                $stmt->bindParam(':username', $username, PDO::PARAM_STR);
                $stmt->bindParam(':password', $password, PDO::PARAM_STR);
                $success = $stmt->execute();

                if ($success){

                    $success = true;
                }

                $db = null;

            } catch (PDOException $e) {
                //echo 'There was an error adding a new user.  Please go back and try again.  If this problem persits please contact the administrator.';
                $success = false;
            }
            return $success;
        }


?>

Hope that this helps. 希望这会有所帮助。

enter link description here $SQL = "INSERT INTO login (L1, L2) VALUES ($uname, md5($pword))"; 在这里输入链接描述 $SQL = "INSERT INTO login (L1, L2) VALUES ($uname, md5($pword))";

You're not inserting the values into proper fields, it appears. 您没有将值插入正确的字段中,而是出现了。 You're inserting the $uname into L1 and md5($pword) into L2 but in the select query above, you have a different field name for username and I presume the same for password. 您正在将$uname插入L1并将md5($pword)插入L2,但是在上面的select查询中,您为用户名使用了不同的字段名,对于密码,我假设使用相同的字段名。

$SQL = "SELECT * FROM login WHERE USERNAME = $uname";

Most likely, your insert query should be something like: 您的插入查询最有可能是这样的:

$SQL = "INSERT INTO login (USERNAME, PASSWORD) VALUES ('{$uname}', MD5('{$pword}'))";

I added single quotes around the username and password since presumably they are strings. 我在用户名和密码周围加上了单引号,因为大概是字符串。 Also, I added curly braces around the variables to segregate what is SQL from what is PHP. 另外,我在变量周围添加了花括号,以将SQL和PHP分开。

One last thing, I would check into doing this with PDO as Willem suggested 最后一件事,我将按照Willem的建议使用PDO进行检查

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM