Click here to Skip to main content
15,887,175 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hello all, I have another project I’m working on a php login and I am inserting a record in the db I’m checking the table to see if it exists or not so there can’t be 2 users with the same username but I am working with create the form submits but nothing is inserted. Also appreciate suggestions in code to make my php better.

PHP
<?php
    //mySQL database config
require_once "config.php";

// Define all variables and initialize them as 'empty'
$name = $username = $password = $password2 = "";
$nameerror = $usernameerror = $passworderror = $password2error = "";

// Process data when the form is submitted.
if($_SERVER["REQUEST_METHOD"] == "POST") {

    //Name check
    if(empty(trim($_POST["name"]))) {
        $nameerror = "Please enter a name.";
    } else {
        $name = trim($_POST["name"]);
    }
    // Validate 'Username'
    if(empty(trim($_POST["username"]))) {
        $usernameerror = "Please enter a Username.";
    } else {
        // Prepare a SELECT statement.
        $sql = "SELECT userid FROM users WHERE username = :username";

        if($stmt = $pdoConn->prepare($sql)) {
            // Bind variables to prepared statement as parameters
            $stmt->bindParam(":username", $param_username, PDO::PARAM_STR);
            // Set parameters
            $param_username = trim($_POST["username"]);
            // Attempt to execute prepared statement
            if($stmt->execute()) {
                if($stmt->rowCount() == 1) {
                    $usernameerror = "Username is already taken.";
                } else {
                    $username = trim($_POST["username"]);
                }
            } else {
                echo "Something went wrong with SELECT, please try again later.";
            }
        }
        // Close $stmt
        unset($stmt);
    }
    // Validate Password
    if(empty(trim($_POST["password"]))) {
        $passworderror = "Please enter a password.";
    } else if (strlen(trim($_POST["password"])) < 6) {
        $passworderror = "Password must have at least 6 characters.";
    } else {
        $password = trim($_POST["password"]);
    }
    // Validate Confirm Password.
    if(empty(trim($_POST["password2"]))) {
        $password2error = "Please confirm your password";
    } else {
        $pass2 = trim($_POST["password2"]);
        if(empty($password2error) && ($password != $pass2)) {
            $password2error = "Passwords DID NOT match.";
        }
    }
    //Check for inputs on form to continue.
    // Error checks or input checks.
    if(empty($name) && empty($username) && empty($password) && empty($password2)) {
        // Prepare SELECT statement
        $sql = "INSERT INTO  users (name, username, password) " .
               "VALUES (:name, :username, :password)";
        if($stmt = $pdoConn->prepare($sql)) {
            // Bind variables to prepared statement as parameters
            $stmt->bindParam(":name", $param_name, PDO::PARAM_STR);
            $stmt->bindParam(":username", $param_username, PDO::PARAM_STR);
            $stmt->bindParam(":password", $param_pass, PDO::PARAM_STR);
            // Set parameters
            $para_name = $name;
            $param_username = $username;
            $param_pass = password_hash($password, PASSWORD_DEFAULT);
            // attempt to execute the prepared Statement
            if($stmt->execute()){
                header("Location: ../index.php");
            } else {
                echo "Something went wrong with INSERT";
            }

        }
        // Close Statement
        unset($stmt);
    }
    // Close connection
    unset($pdoConn);
}


?>


What I have tried:

I have a php login project with crud and it works in my other project but i am trying stuff a lil different here using prepared statement and I am not sure why I cannot get my record to insert into the db here
Posted
Comments
ZurdoDev 31-Jan-19 8:11am    
What is your question?
TheBigBearNow 31-Jan-19 17:19pm    
My project does create read and delete but when i do update I DO NOT get an update. It refreshes the page and does not update and does not go to header()
Richard Deeming 4-Feb-19 14:57pm    
There's nothing in the code you've shown that will execute an UPDATE query.

Your INSERT looks wrong - you're only executing it if all of the values you're going to insert are empty.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900