Martin Gardener
Martin Gardener

Reputation: 1001

Data not being inserted into the XAMPP Database

I have a problem while registering a person. I have tried logging in and it works perfectly.

Script for the database

CREATE TABLE userinfor (
    userid INT(4) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(25) NOT NULL,
    password VARCHAR(15) NOT NULL,
    email VARCHAR(35) NOT NULL
);

PHP Code

$emailErr = $passMismatch = "";
if( isset($_POST['submit']) ){
    if ( $_SERVER["REQUEST_METHOD"] == "POST" ) {
        $name = $_REQUEST['name'];
        $pwd = $_REQUEST['password'];
        $cpwd = $_REQUEST['cpassword'];
        $email = $_REQUEST['email'];

        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
            $emailErr = "ERROR::Invalid E-Mail Format"; 
        } else $emailErr = "";

        if ( $pwd != $cpwd ) {
            $passMismatch = "ERROR::Passwords Don't Match";
        } else $passMismatch = "";

        $host = "localhost";
        $username = "root";
        $password = "";
        $database = "ontheway";

        $conn = mysqli_connect($host, $username, $password, $database);

        if ( empty($emailErr) && empty($passMismatch) ) {
            $sql = "INSERT INTO userinfo (userid, name, password, email)
                    VALUES (NULL, $name', $pwd', '$email')";


            $result = mysqli_query($conn, $sql);
            if ( !$result ) {
                echo "chala nahi query";
            }

            if ( $result ) {
                session_start();
                $_SESSION['name'] = $name;
                $_SESSION['pass'] = $pass;

                header("location: home.php");    
            }
        }
    }
}

I tried echoing for problems but i can't find any, $name, $pwd, $cpwd, $email are all being echoed. Even the errors are also being perfectly being displayed on form. Database is also being connected. What seems to be the problem? help please :)

I always get "chala nahi query" that means The query sql didn't work each time i run this.

Form Code

Name:

                            <input type="text" name="name" /> <br /> 

                            <p>Password: </p>
                            <input type="password" name="password" /> <br />
                            <?php echo '<h1>'.$passMismatch.'</h1>'; ?>

                            <p>Confirm Password: </p>
                            <input type="password" name="cpassword" /> <br />

                            <p>Email: </p>
                            <input type="email" name="email" /> <br />
                            <?php echo '<h1>'.$emailErr.'</h1>'; ?>

                            <input type="submit" name="submit" />

Upvotes: 0

Views: 5107

Answers (2)

Vivek Singh
Vivek Singh

Reputation: 2447

missing quotes around the variable also no need to insert the userid as it is already autoincremented

$sql = "INSERT INTO userinfo (`name`, `password`, `email`)
                    VALUES ('".$name."', '".$pwd."', '".$email."')";

Upvotes: 1

gavgrif
gavgrif

Reputation: 15499

you are missing a single quote in the query around the $name and $pwrd variables in the query- it should be;

$sql = "INSERT INTO userinfo (userid, name, password, email)
                    VALUES (NULL, '$name', '$pwd', '$email')";

You should be investigate PDO and bound parameters - much safer. Also - you should be sanitising the input and you should only have 1 H1 element on your page.

Upvotes: 1

Related Questions