输入表格将数据作为数值而不是实际数据插入

时间:2013-09-07 13:40:19

标签: php mysql

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

提前致谢

我的代码如下:

register.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>

class.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();
            ?>

1 个答案:

答案 0 :(得分:2)

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

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

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

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

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

要使其有效,您还必须更改$name$email,因为您还在那里使用了isset