注册后数据不会被发送到DB

时间:2015-07-01 02:43:46

标签: php mysql

当我点击提交时,它会将我发送到标题位置,该位置确认他们已注册的用户但数据未发送到数据库,因此您无法在注册后登录,不确定我有什么问题我工作了早些时候,找不到我的错误。一些帮助将不胜感激!

<form action = "Register.php" method="POST">
<?php 
if(isset($_POST['submit'])) {
$username = $_POST['username'];
$password = $_POST['password'];
$email = $_POST['email'];
$Fname = $_POST['Fname'];
$Lname = $_POST['Lname'];
$Display1 = $_POST['Display1'];
$Display2 = $_POST['Display2'];
if(empty($username) || empty($password) || empty($email) || empty($Fname) || empty($Lname) || empty($Display1)) {
    echo '<p>Fields Empty!</p>';
} else if(mysql_num_rows($query) > 0){
    $query = mysql_query("SELECT * FROM users WHERE username ='$username' AND password ='$password'");
    echo'<p>Username or Password Already Exists!</p>';
} else {
            mysql_query("INSERT INTO users(id, username, password, Display1, Display2, email, Fname, Lname, user_level, type) VALUES (``, '$username', '$password', '$Display1', '$Display2', '$email', '$Fname', '$Lname', '2', 'a')");

            $subject = "Membership Confirmation";
            $message = "Hello, You have registered an account on ";
            $from = "From: ";

            header("location:signuppayment.php");
            mail($email, $subject, $message, $from);
}

}

?>
<fieldset>
<legend>Register</legend>
<label>Username: <input class="text" type="text" name="username" /></label>
<label>Password: <input class="text" type="password" name="password" />   </label>
<label>Email: <input class="text" type="text" name="email" /></label>
<label>First name: <input class="text" type="text" name="Fname" /></label>
<label>Last name: <input class="text" type="text" name="Lname" /></label>
<label>Display name 1: <input class="text" type="text" name="Display1" />  </label>
<label>Display name 2: <input class="text" type="text" name="Display2" /></label>

</fieldset>
<div class ="registerForm">
<input type="submit" name="submit" value="Register"/>
</form>

2 个答案:

答案 0 :(得分:1)

用以下内容替换您的代码;

<?php 
if(isset($_POST['submit'])) {
$username = mysql_real_escape_string($_POST['username']);
$password = mysql_real_escape_string($_POST['password']);
$email = mysql_real_escape_string($_POST['email']);
$Fname = mysql_real_escape_string($_POST['Fname']);
$Lname = mysql_real_escape_string($_POST['Lname']);
$Display1 = mysql_real_escape_string($_POST['Display1']);
$Display2 = mysql_real_escape_string($_POST['Display2']);
if(empty($username) || empty($password) || empty($email) || empty($Fname) || empty($Lname) || empty($Display1)) {
    echo '<p>Fields Empty!</p>';
    //set your header location here if you want to tell the user the fields are empty incase you have seprate php action file;
    //exit;
} 
    $query = mysql_query("SELECT * FROM users WHERE username ='$username' AND password ='$password'");
    if(mysql_num_rows($query) > 0)
    echo'<p>Username or Password Already Exists!</p>';
    //set your header location here if you want to tell the user that username and password already exist incase you have seprate php action file;
    //exit;
    } else {
    mysql_query("INSERT INTO users(id, username, password, Display1, Display2, email, Fname, Lname, user_level, type) VALUES (``, '$username', '$password', '$Display1', '$Display2', '$email', '$Fname', '$Lname', '2', 'a')");
            //You can also set if statement here so to be sure that email triggered after successfull data insertion
            $subject = "Membership Confirmation";
            $message = "Hello, You have registered an account on ";
            $from = "From: ";

            mail($email, $subject, $message, $from);
    }
            header("location:signuppayment.php");
            exit;
}
?>

//you can set inputs 'required' like this to be sure no empty field submitted, better use jQuery or javascript for validation.
<input class="text" type="text" name="username" required />

答案 1 :(得分:1)

尝试类似......

$db = new mysqli("localhost", "user", "password", "database");
if($db->connect_errno) {
    die('Sorry, there is trouble making database connections at the moment.');
}

if(isset($_POST['submit']){
    if(empty($_POST['username']) 
    || empty($_POST['password']) 
    || empty($_POST['email']) 
    || empty($_POST['Fname']) 
    || empty($_POST['Lname']) 
    || empty($_POST['Display1'])
    || empty($_POST['Display2'])) {

        echo '<p>Please make sure all fields are filled.</p>';

    } else {

        $check = $db->query("SELECT username FROM users WHERE username = {$username}");

        if($check->num_rows) {
            echo 'Username taken.';
        } else {

        $query = $db->prepare("INSERT INTO users (username, password, Display1, Display2, email, Fname, Lname) VALUES (?, ?, ?, ?, ?, ?, ?)");
        $query->bind_param('sssssss', $_POST['username'], $_POST['password'], $_POST['Display1'], $_POST['Display2'], $_POST['email'], $_POST['Fname'], $_POST['Lname']);
        $query->execute();
        header("location:signuppayment.php");
        }
    }
}