2

嗨,我有一个输入表单,我已将虚拟数据添加到此表单中,出于某种原因,它将数据添加为数值,即“1”。我不确定我哪里出错了,这与我COUNT在上传这些数据之前选择的电子邮件有什么关系吗?

提前致谢

我的代码如下:

注册.php

    <div id="maincontentWrapper">

    <div id="maincontent">
        <div id="contentWrapper"></div><!--End registerWrapper -->
            <article>
                <p>Welcome to iManage, please login in below.</p>
            </article>
        <div id="loginform">
            <div id="registerWrapper">
            <form id="registerForm" method="POST" action="insert.php">
            <h1><span class="log-in">Register</span></h1>
                    <p class="required"><span class="log-in">*Required Fields</span></p>
                    <div id="errorDiv"><?php 
                        if (isset($_SESSION['error']) & isset($_SESSION['formAttempt'])) {
                                unset($_SESSION['formAttempt']);
                                print "Errors encountered<br/>\n";
                                foreach ($_SESSION['error'] as $error) {
                                print $error . "<br />\n";
                            } //end foreach
                            } //end if 
                    ?></div>

             <p class="float">
            <label for="password"><i class="icon-lock"></i>Name*</label>
            <input type="text" id="name" name="name" placeholder="Name" class="showpassword"> 
            <span class="errorFeedback errorSpan" id="nameError">Name is required</span>
        </p>
        <p class="float">
            <label for="login"><i class="icon-user"></i>Username*</label>
            <input type="text" id="username" name="username" placeholder="Username">
                    <span class="errorFeedback errorSpan" id="usernameError">Username is required</span>
        </p>
        <p class="float">
            <label for="password"><i class="icon-lock"></i>Password*</label>
            <input type="password" id="password" name="password" placeholder="Password" class="showpassword"> 
                    <span class="errorFeedback errorSpan" id="passwordError">Password is required</span>
        </p>
           <p class="float">
            <label for="password"><i class="icon-lock"></i>Verify Password*</label>
            <input type="password" id="password2" name="password2" placeholder="Verify Password" class="showpassword"> 
                    <span class="errorFeedback errorSpan" id="password2Error">Passwords dont match</span>
        </p>

         <p class="float">
            <label for="password"><i class="icon-lock"></i>Email*</label>
            <input type="text" id="email" name="email" placeholder="Email" class="showpassword"> 
                    <span class="errorFeedback errorSpan" id="emailError">Email is required or you have not entered a valid email address</span>
        </p>
        <p class="clearfix"> 
            <input type="submit" name="submit" value="Register"></form>
        </p>   
            </div>

        </div>


    </div>
    </div>

    </div>

类.Users.php

<?php
include("connect/class.Connect.php");
class Users extends Database {


 function preventaccess () {
    if (!isset($_POST['submit'])) {
        die(header("Location: register.php"));
    }
}

function validate() {
    $_SESSION['formAttempt'] = true;

    if (isset($_SESSION['error'])) {
    unset($_SESSION['error']);
    }

     $_SESSION['error'] = array();

    $required = array("username","name","email","password","password2");

        //Check required fields
        foreach ($required as $requiredField) {
        if (!isset($_POST[$requiredField]) || $_POST[$requiredField] == "") {
        $_SESSION['error'][] = $requiredField . " is required.";
        }
        }


        if (!filter_var($_POST['email'],FILTER_VALIDATE_EMAIL)) {
        $_SESSION['error'][] = "Invalid e-mail address";
        }

        if ($_POST['password'] != $_POST['password2']) {
        $_SESSION['error'][] = "Passwords don't match";
        }
        //final disposition
        if (count($_SESSION['error']) > 0) {
        die(header("Location: register.php"));
        } else {
        unset($_SESSION['formAttempt']);
        }
}


public function insert() {

                    $result = $this->mysqli->prepare("SELECT COUNT(*) FROM users WHERE email=?");
                    $result->bind_param("s", $_POST['email']);
                    $result->execute();
                    $result->bind_result($email_count);
                    $result->fetch();//fecth
                    $result->close();   

                    if ($email_count > 0) {
                        echo "email exisits! click here to try <a href='register'>again</a>";
                        } else {
                            //escape the POST data for added protection
                            $username = isset($_POST['username']);
                            $cryptedPassword = crypt($_POST['password']);
                            $password = $cryptedPassword;
                            $name = isset($_POST['name']);
                            $email = isset($_POST['email']);
                            $stmt = $this->mysqli->prepare("INSERT INTO users (username, password, name, email) VALUES (?, ?, ?, ?)");
                            //var_dump($this->mysqli->error);
                            $stmt->bind_param('ssss', $username, $password, $name, $email); // bind strings to the paramater

                                /* execute prepared statement */
                                $stmt->execute();
                                printf("%d Row inserted.\n", $stmt->affected_rows);
                                /* close statement and connection */
                                $stmt->close();
                } // end email_count and insert to table
            } // end function



} // End users class
$run = new Users();
        $run->preventaccess();
        $run->validate();
        $run->insert();
            ?>
4

1 回答 1

2

你犯了一个小错误。你写过:

$username = isset($_POST['username']);

但我认为你真正想做的是:

$username = isset($_POST['username']) ? $_POST['username'] : null;

实际上,您不是在存储用户名等本身,而是在发送这些值时。然后将布尔值true转换为数字1

为了让它工作,你还必须改变它$name$email因为你也在isset那里使用过。

于 2013-09-07T13:45:08.720 回答