Php 我一直得到23750作为一个提交后的密码

Php 我一直得到23750作为一个提交后的密码,php,mysql,Php,Mysql,我在这里完全被难住了。我正在为这个类创建一个简单的成员系统,无论输入什么密码,我都会得到“23750”。首先,我认为这是一个数据库问题,所以我手动输入了一个,结果很好。然后,我回显了下一页的密码,我仍然得到23750。我从头开始重新创建了表单,23750这个数字甚至不存在于我的任何代码中。有什么想法吗?无论如何,这是代码 <?php // Check if user is logged in first if ($_SESSION['logged_in']) { // user is l

我在这里完全被难住了。我正在为这个类创建一个简单的成员系统,无论输入什么密码,我都会得到“23750”。首先,我认为这是一个数据库问题,所以我手动输入了一个,结果很好。然后,我回显了下一页的密码,我仍然得到23750。我从头开始重新创建了表单,23750这个数字甚至不存在于我的任何代码中。有什么想法吗?无论如何,这是代码

<?php 
// Check if user is logged in first
if ($_SESSION['logged_in']) { // user is logged in
    header("Location: members.php");
}
$pageTitle = 'Register';
require('includes/header.php'); ?>
        <div id="content">
            <h3>Register</h3>
            <?php

        // Check if form has been submitted
        if ($_SERVER['REQUEST_METHOD'] == 'POST') { // user submitted the form
            // Validate info
            // Grab variables
            $email = $_POST['email'];
            $email2 = $_POST['email2'];
            $fname = $_POST['fname'];
            $lname = $_POST['lname'];
            $gender = $_POST['gender'];
            $age = $_POST['age'];
            $address = $_POST['address'];
            $password = $_POST['password'];
            $password2 = $_POST['password2'];
            $pass_hint = $_POST['pass_hint'];

            // Check if form was completely filled out
            if ($email != "") { // email was filled out
                if ($email2) { // verify email was filled out
                    if ($fname) { // first name was filled out
                        if ($lname) { // last name was filled out
                            if ($gender != 'select') { // gender was filled out
                                if ($age) { // age was filled out
                                    if ($password) { // password was filled out
                                        if ($password2) { // verification password was filled out
                                            if (strlen($password) > 7) { // password is less than 8 characters}
                                                if ($pass_hint) { // password hint was filled out
                                                    if ($password !== $pass_hint) { // password hint does not equal password
                                                    // All information was entered
                                                    // Check if email and password match each other
                                                        if ($email === $email2) { // emails match
                                                            if ($password === $password2) { // passwords match
                                                                // all form data is good, now check with database
                                                                require('includes/database.php'); // Connect to databae
                                                                // Check if email exists
                                                                $qry = $dbc->prepare("SELECT * FROM users WHERE email='$email'");
                                                                $qry->execute();

                                                                // Count rows
                                                                $count = $qry->rowCount();
                                                                if ($count != 1) { // email does not exist
                                                                    // All information is good, insert to database
                                                                    // Query database
                                                                    $qry = $dbc->prepare("INSERT INTO users (email, fname, lname, gender, age, password, pass_hint) VALUES ('$email', '$fname', '$lname', '$gender', '$age', '$password', '$pass_hint');");
                                                                    $qry->execute();

                                                                    // Check if it was submitted successfully
                                                                    $count = $qry->rowCount();
                                                                    if ($count == 1) { // submitted successfully
                                                                        echo $password;
                                                                        echo 'You have been successfully registered, now forwarding you to the login page...';
                                                                        ?> <meta http-equiv="refresh" content="3;URL='login.php'" /> <?php
                                                                    } else { // error submitting info
                                                                        echo 'There was a system error, please contact the administrator';
                                                                        registration_form();
                                                                    }
                                                                } else { // email does exist
                                                                    echo 'A user with that email already exists!';
                                                                    registration_form();
                                                                }
                                                            } else { // passwords do not match
                                                                echo 'Your passwords must match!';
                                                                registration_form();
                                                            }
                                                        } else { // emails do not match
                                                            echo 'Your emails must match!';
                                                            registration_form();
                                                        }
                                                    } else { // password hint matches password given
                                                        echo 'Your password hint cannot equal your password!';
                                                        registration_form();
                                                    }
                                                } else { // password hint was not filled out
                                                    echo 'Please enter a password hint';
                                                    registration_form();
                                                }
                                            } else { // password is not 8 characters or longer
                                                echo 'Your password must be 8 characters of longer';
                                                registration_form();
                                            }
                                        } else { // verification password was not filled out
                                            echo 'Please enter your password twice!';
                                            registration_form();
                                        }
                                    } else { // password aws not filled out
                                        echo 'Please enter a password';
                                        registration_form();
                                    }
                                } else { // age was not filled out
                                    echo 'Please enter your age';
                                    registration_form();
                                }
                            } else { // gender was not filled out
                                echo 'Please select a gender';
                                registration_form();
                            }
                        } else { // last name was not filled out
                            echo 'Please enter your last name';
                            registration_form();
                        }
                    } else { // first name was not filled out
                        echo 'Please enter your first name';
                        registration_form();
                    }
                } else {
                    echo 'Please enter your cerification email';
                    registration_form();
                }
            } else { // email was not filled out
                echo "Please enter your email address!";
                registration_form();
            }


        } else { // user did not submit form, show reg form
            echo "  <form action='register.php' method='post'>
                            <p>email <input type='text' name='email'></p>
                            <p>confirm email <input type='text' name='email2'></p>
                            <p>first name <input type='text' name='fname'></p>
                            <p>last name <input type='text' name='lname'></p>
                            <select id='gender' name='gender'>
                                    <option value='1'>Male</option>
                                    <option value='2'>Female</option>
                            </select>
                            <p>age <input type='text' name='age'></p>
                            <p>address <input type='text' name='address'></p>
                            <p>password <input type='password' name='password'></p>
                            <p>confirm pass <input type='password' name='password'></p>
                            <p>hint <input type='text' name='pass_hint'></p>
                            <p><input type='submit' name='submit' value='submit'></p>
                    </form>";
        }

        ?>
    </div>
</body>

登记

至少有2个与代码无关的问题,但:

if ($_SESSION['logged_in']) {
还添加一个isset()

密码

确认通行证

你这里没有密码2

在我这方面,通过删除mysql逻辑和添加旁路,我在表单中输入了密码。

(这不是答案,但需要空间,所以请随意投票否决。)

要“修复”嵌套代码,请执行以下操作:

$error = true;

if ( !$email ) {
  echo 'Invalid e-mail!';
  registration_form();
}

else if ( !$email2 ) {
  echo 'Invalid e-mail 2!';
  registration_form();
}

else if ( !$fname ) {
  echo 'Invalid first name!';
  registration_form();
}

else if ( !$lname ) {
  echo 'Invalid last name!';
  registration_form();
}

else {
  $error = false;
}

if ( !$error ) {
  // Yeeeeh. Do register stuff?
}

它仍然凌乱,但不那么凌乱。你也是。

嵌套的if语句非常混乱。这是一个明显的坏代码气味,需要修复。你的密码是不是你的数据库中的一个int字段,而不是一个字符串?@ JohnConde,你推荐什么样的注册表格来验证所有这些?对于更干净的东西,你可以考虑使用一个库或框架,如果你的项目大到足以证明你这么做的话。在CakePHP框架中查看这个验证:这个问题似乎离题了,因为它包含一个打字错误(由OP评论)
$error = true;

if ( !$email ) {
  echo 'Invalid e-mail!';
  registration_form();
}

else if ( !$email2 ) {
  echo 'Invalid e-mail 2!';
  registration_form();
}

else if ( !$fname ) {
  echo 'Invalid first name!';
  registration_form();
}

else if ( !$lname ) {
  echo 'Invalid last name!';
  registration_form();
}

else {
  $error = false;
}

if ( !$error ) {
  // Yeeeeh. Do register stuff?
}