1

我有这段代码(如下所示)和变量 $first_name=$_POST['first_name']; 似乎不起作用。我有用户名、密码和标题工作。这与使用下划线有关吗?我检查了它是否与数据库匹配。当用户创建帐户时,存储 first_name 的行会引发错误,并且不会显示在数据库中输入的名称。有任何想法吗?

<?php
/* This code will make a connection with database */
$con=mysql_connect("localhost","","");

/* Now, we select the database */
mysql_select_db("");

/* Now we will store the values submitted by form in variable */
$username=$_POST['username'];
$pass=$_POST['password'];
$title=$_POST['title'];
$first_name=$_POST['first_name'];
/* we are now encrypting password while using md5() function */
$password=md5($pass);
$confirm_password=$_POST['confirm_password'];

/* Now we will check if username is already in use or not */
$queryuser=mysql_query("SELECT * FROM Customer WHERE username='$username' ");
$checkuser=mysql_num_rows($queryuser);
if($checkuser != 0)
{ echo "Sorry, ".$username." is already been taken."; }
else {

/* now we will check if password and confirm password matched */
if($pass != $confirm_password)
{ echo "Password and confirm password fields were not matched"; }
else {

/* Now we will write a query to insert user details into database */
$insert_user=mysql_query("INSERT INTO Customer (username, password, title, first_name) VALUES ('$username', '$password', '$title', '$first_name')");

if($insert_user)
{ echo "Registration Succesfull"; }
else
{ echo "error in registration".mysql_error(); }

/* closing the if else statements */
}}

mysql_close($con);
?>

谢谢!

HTML 表单

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <meta name="apple-mobile-web-app-capable" content="yes" />
        <meta name="apple-mobile-web-app-status-bar-style" content="black" />
        <title>
        </title>
        <link rel="stylesheet" href="https://ajax.aspnetcdn.com/ajax/jquery.mobile/1.2.0/jquery.mobile-1.2.0.min.css" />
        <link rel="stylesheet" href="my.css" />
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js">
        </script>
        <script src="https://ajax.aspnetcdn.com/ajax/jquery.mobile/1.2.0/jquery.mobile-1.2.0.min.js">
        </script>
        <script src="my.js">
        </script>
        <!-- User-generated css -->
        <style>
        </style>
        <!-- User-generated js -->
        <script>
            try {

    $(function() {

    });

  } catch (error) {
    console.error("Your javascript has an error: " + error);
  }
        </script>
    </head>
    <body>
        <!-- Home -->
        <div data-role="page" id="page1">
            <div data-theme="a" data-role="header">
            <a data-role="button" data-theme="d" href="Login.html" data-icon="arrow-l" data-iconpos="left" class="ui-btn-left">
                    Back
                </a>
                <a data-role="button" href="index.html" data-icon="home" data-iconpos="right" data-theme="d"class="ui-btn-right">
                 Home  
                </a>
                <h3>
                    Registration
                </h3>
            </div>
            <div data-role="content">
                <h4>
                    Enter your details below to register:
                </h4>

            <form method="post" action="Login.php">
            <table border="0">
            <tr><td>Title: </td><td><input type="text" name="title" /></td></tr>

            <tr><td>First Name: </td><td><input type="text" name="name" /></td></tr>

            <tr><td>Username: </td><td><input type="text" name="username" /></td></tr>

            <tr><td>Password: </td><td><input type="password" name="password" /></td></tr>

            <tr><td>Confirm Password: </td><td><input type="password" name="confirm_password" /></td></tr>

            <tr><td></td><td><input type="submit" value="submit" /></td></tr>

            </table>
            </form>
                <div data-role="content">
                <h4>
                 Please ensure your username and password are kept secure.
                </h4>

                <br />
            </div>
        </div>
    </body>
</html>
4

4 回答 4

3

问题是您的输入文本的表单属性first name不是。所以你应该改变namefirst_name

$first_name=$_POST['first_name'];

$first_name=$_POST['name'];

或将您的 name 属性更改为first_name

在插入之前还要对所有字段进行适当的转义

<?php
/* This code will make a connection with database */
$con=mysql_connect("localhost","","");

/* Now, we select the database */
mysql_select_db("");

/* Now we will store the values submitted by form in variable */
$username= mysql_real_escape_string($_POST['username']);
$pass= mysql_real_escape_string($_POST['password']);
$title= mysql_real_escape_string($_POST['title']);
$first_name= mysql_real_escape_string($_POST['name']);

/* we are now encrypting password while using md5() function */
$password= md5($pass);
$confirm_password= mysql_real_escape_string($_POST['confirm_password']);

/* Now we will check if username is already in use or not */
$queryuser=mysql_query("SELECT * FROM Customer WHERE username='$username' ");
$checkuser=mysql_num_rows($queryuser);
if($checkuser != 0)
{ echo "Sorry, ".$username." is already been taken."; }
else {

/* now we will check if password and confirm password matched */
if($pass != $confirm_password)
{ echo "Password and confirm password fields were not matched"; }
else {

/* Now we will write a query to insert user details into database */
$insert_user=mysql_query("INSERT INTO Customer (username, password, title, first_name) VALUES ('$username', '$password', '$title', '$first_name')");

if($insert_user)
{ echo "Registration Succesfull"; }
else
{ echo "error in registration".mysql_error(); }

/* closing the if else statements */
}}

mysql_close($con);
?>
于 2013-04-08T16:30:50.680 回答
0

将其更改为:

$first_name = mysql_real_escape_string($_POST['first_name']);

您当前正在保存而没有进行任何清理或转义。

于 2013-04-08T16:17:10.533 回答
0

改变这个:

$insert_user=mysql_query("INSERT INTO Customer (username, password, title, first_name) VALUES ('$username', '$password', '$title', '$first_name')");

对此:

$insert_user=mysql_query("INSERT INTO Customer (username, password, title, first_name) VALUES ('".$username."', '".$password."', '".$title."', '".$first_name."')");

更新

请检查是否<input type="text" name="first_name">name="first_name"

于 2013-04-08T16:21:02.157 回答
0

好吧,看起来您的表单不包含first_name项目,但是name. 改回first_name

于 2013-04-08T16:29:50.050 回答