使用MySql和PHP验证html页面的数据库连接

时间:2013-02-26 05:52:55

标签: php html mysql

这是我的代码,用于创建一个简单的登录页面,其中包含用户名,密码,确认密码以及自己标识select选项: -

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Sign up</title>
<style type="text/css">
    div#registration
    {
        text-align:center;
        margin-left:auto;
        margin-right:auto;
        width:40%;
        border:solid 2px green;
    }
</style>

</head>



<body>
<h1> Sign Up! </h1>
<h2> Enter username and password. Dont forget to recognize yourself </h2>

<?php

        $mysqli = new mysqli("localhost", "root", "", "signup");
        if($mysqli===false)
        {
            die("ERROR: Could not connect to database." .mysqli_connect_error());
        }

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


            echo'<div id="message">';

            $inputError = false;
            if(empty($_POST['username'] ))
            {
                echo 'ERROR: Please enter a valid user name';
                $inputError = true;
            }
            else
            {
                $username= $mysqli->escape_string($_POST['username']);
            }


            if($inputError != true && empty($_POST['password'] ))
            {
                echo 'ERROR: Please enter valid password';
                $inputError = true;
            }
            else
            {
                $password= $mysqli->escape_string($_POST['password']);
            }


            $inputError = false;
            if(empty($_POST['cfmpassword'] ))
            {
                echo 'ERROR: empty field';
                $inputError = true;
            }
            else if($_POST['password'] != $_POST['cfmpassword'])
            {
                echo 'ERROR: passwords does not match';
            }
            else
            {
                $cfmpassword = $mysqli->escape_string($_POST['cfmpassword']);
            }


        if($inputError != true && empty($_POST['desig'] ))
        {
            echo 'ERROR: Please enter valid password ';
            $inputError = true;
        }
        else
        {
            $desig= $mysqli->escape_string($_POST['desig']);
        }

        if ($inputError !=true)
        {
                $sql= "INSERT INTO database (Username, password, confirm password, Designation) VALUES ('$username','$password','$cfmpassword','$desig')";

                if ($mysqli->query($sql) === true)
                {
                    echo 'new record added with ID:' . $mysqli->insert_id;
                }
                else
                {
                    echo "ERROR: Could not execute query: $sql." .$mysqli->error;
                }
        }
        echo '</div>';

        $mysqli->close();
    }
?>

<form action="signup.php" method="post">

Username:
<input type="text" size="20" name="username" />
<p/>

Password:
<input type="password" size="20" name="password" />
<p/>

Confirm Password:
<input type="password" size="20" name="cfmpassword" />
<p/>

Identify yourselves:
<select name="desig">

<option name="admin">Admin</option>
<option name="Faculty">Faculty</option>
<option name="Student">Student</option>
</select>
<p/>

<input type="submit" name="submit" value="Submit" />

</form>







</body>
</html>

这是我更新的代码,现在验证工作完全正常,但我收到了错误。这是我得到的错误。这段代码中可能出现的错误是什么?

ERROR: Could not execute query: INSERT INTO database (Username, password, confirm password, Designation) VALUES ('suhas','99','99','Admin').You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'database (Username, password, confirm password, Designation) VALUES ('suhas','99' at line 1

1 个答案:

答案 0 :(得分:0)

中的

'$inputError = false' 

应该是

'$inputError == false'

错过了一个'='