the aim of this code is to make this sign up users and check if they
werr already signed up before
with the code below, this is what happens.
on signup, it validates email and username that are already signedup which
works just fine.
PROBLEM: it takes(sign's up) a new username and flags an existing email(email already exists)
also, it takes in(sign's up) an new email and flags an existing username(username already exist)
but also signup new emails and new usernames both.
i have been trying to make this work to no avail.
how do i make it check first and validate first before signing up any data
first code is for is signup page
second code for controller
<?php
require "function2.php";
$errors = array();
if($_SERVER['REQUEST_METHOD'] == "POST"){
$errors = signup($_POST);
if(count($errors) == 0){
header("Location:verify.php");
// echo "Account Success!!!";
// $errors[] = "Account Success!!!";
die;
}
}
?>
<?php
require "function2.php";
$errors = array();
if($_SERVER['REQUEST_METHOD'] == "POST"){
$errors = signup($_POST);
if(count($errors) == 0){
header("Location:verify.php");
// echo "Account Success!!!";
// $errors[] = "Account Success!!!";
die;
}
}
?>
<?php
session_start();
function signup($data){
$errors = array();
#validate
if(!preg_match('/^[a-zA-Z]+$/', $data['username'])){
$errors[] = "Alert!!!, username must be letters with no space";
}
if(!filter_var($data['email'], FILTER_VALIDATE_EMAIL)){
$errors[] = "Alert!!!, enter correct email address";
}
if(strlen(trim($data['password1'])) < 5){
$errors[] = "Alert!!!, password must be more than 5 characters";
}
if($data['password1'] != $data['password2']){
$errors[] = "Alert!!!, password do not match";
}
//save to db
if(count($errors) == 0){
$arr['username'] = $data['username'];
$arr['email'] = $data['email'];
$arr['password1'] = $data['password1'];
$arr['date'] = date("Y-m-d H-i-m");
#check email/username does exists
$con = mysqli_connect("localhost", "root", "", "stervest_db" );
if(isset($_POST['insert'])){
$email = $_POST['email'];
$username = $_POST['username'];
$a = "select username from user2 where username = '$username' limit 1";
$aa =mysqli_query($con, $a);
if(mysqli_num_rows($aa)>0){
$errors[] = "Alert!!, username already exists";
}
$b = "select email from user2 where email = '$email' limit 1";
$bb =mysqli_query($con, $b);
if(mysqli_num_rows($bb)>0){
$errors[] = "Alert!!, email already exists";
}
}
$query ="insert into user2(username, email, password1, date) values(:username, :email, :password1, :date)";
$row = signup_push($query, $arr);
}
//var_dump($data);
return $errors;
}
function signup_push($query, $vars = array()){
$string ="mysql:host=localhost;dbname=stervest_db";
$conn = new PDO($string, 'root', '');
if(!$conn){
return false;
}
$stm = $conn->prepare($query);
$check = $stm->execute($vars);
if($check){
$data = $stm->fetchAll(PDO::FETCH_OBJ);
if(count($data)>0){
return $data;
}
}
return false;
}
function checked_verified(){
$vars = array();
$vars['email']= $data['email'];
$vars['email_verified'] = $_SESSION['USER']->email_verified;
$query = "select * from user2 where email = :email && email_verified = :email_verified limit 1";
$row = signup_push($query, $vars);
}
<?php
require "function2.php";
$errors = array();
if($_SERVER['REQUEST_METHOD'] == "POST"){
$errors = signup($_POST);
if(count($errors) == 0){
header("Location:verify.php");
// echo "Account Success!!!";
// $errors[] = "Account Success!!!";
die;
}
}
?>