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

时间:2022-02-25 21:22:27

Hi I've got an input form I've added dummy data into this form and for some reason it is adding the data as numerical values, i.e. "1". I'm not sure where I've gone wrong, does it have anything to do with me selecting the COUNT of the e-mail before I upload this data?

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

Thanks in advance

提前致谢

My code is below:

我的代码如下:

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 个解决方案

#1


2  

Your made a little mistake. You've written:

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

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

but what I think you really wanted to do is:

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

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

In fact, you are not storing username, etc. itself but if these values are sent. The boolean value true is then converted to the number 1.

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

To make it work you also have to change it for $name and $email since you've also used isset there.

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

#1


2  

Your made a little mistake. You've written:

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

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

but what I think you really wanted to do is:

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

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

In fact, you are not storing username, etc. itself but if these values are sent. The boolean value true is then converted to the number 1.

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

To make it work you also have to change it for $name and $email since you've also used isset there.

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