Preventing Dublicate Email,Username,Number But Facing This Error [duplicate] - php

This question already has an answer here:
What to do with mysqli problems? Errors like mysqli_fetch_array(): Argument #1 must be of type mysqli_result and such
(1 answer)
Closed 1 year ago.
But i am facing this error
Fatal error: Uncaught TypeError: mysqli_query(): Argument #1 ($mysql) must be of type mysqli, bool given in F:\Xampp\htdocs\User-Registration-System\index.php:20 Stack trace: #0 F:\Xampp\htdocs\User-Registration-System\index.php(20): mysqli_query(false, 'SELECT * FROM `...') #1 {main} thrown in F:\Xampp\htdocs\User-Registration-System\index.php on line 20
<?php include 'connection.php';
if (isset($_REQUEST['register'])) {
$username = $_POST['username'];
$email = $_POST['email'];
$mobile = $_POST['mobile'];
$password = md5($_POST['password']);
$cpassword = md5($_POST['cpassword']);
if (
empty($_POST['username']) ||
empty($_POST['email']) ||
empty($_POST['mobile']) ||
empty($_POST['password']) ||
empty($_POST['cpassword'])
) {
echo 'Please fill all required fields!';
} else {
$usernameQuery = "SELECT * FROM `register` WHERE `username`='$username'";
$uq = mysqli_query($conn, $usernameQuery);
$emailQuery = "SELECT * FROM `register` WHERE `email`='$email'";
$eq = mysqli_query($conn, $emailQuery);
$mobileQuery = "SELECT * FROM `register` WHERE `mobile`=$mobile";
$pq = mysqli_query($conn, $mobileQuery);
if (mysqli_num_rows($uq > 0)) {
echo "username already exist";
} elseif (mysqli_num_rows($eq > 0)) {
echo "username already exist";
} elseif (mysqli_num_rows($pq > 0)) {
echo "someone already register with this phone number";
} elseif ($password === $cpassword) {
$iquery = "INSERT INTO `register`(`username`,`eamil`,`mobile`,`password`) VALUES('$username','$email','$mobile','$password')";
mysqli_query($conn, $iquery);
} else (header('location:index.php'));
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.0.0-beta3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-eOJMYsd53ii+scO/bJGFsiCZc+5NDVN2yr8+0RDqr0Ql0h+rP48ckxlpbzKgwra6" crossorigin="anonymous">
</head>
<body>
<div class="container">
<div class="h1">Register Here</div>
<form action="" method="post">
<div class="form-group">
<label for="exampleInputEmail1">Username</label>
<input type="text" class="form-control" name="username" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Enter email">
</div>
<div class="form-group">
<label for="exampleInputEmail1">Email</label>
<input type="email" class="form-control" name="email" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Enter email">
</div>
<div class="form-group">
<label for="exampleInputEmail1">Mobile</label>
<input type="text" class="form-control" name="mobile" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Enter email">
</div>
<div class="form-group">
<label for="exampleInputPassword1">Password</label>
<input type="password" class="form-control" name="password" id="exampleInputPassword1" placeholder="Password">
</div>
<div class="form-group">
<label for="exampleInputPassword1">Confirm Password</label>
<input type="password" class="form-control" name="cpassword" id="exampleInputPassword1" placeholder="Password">
</div>
<br>
<button type="submit" name="register" class="btn btn-primary">Submit</button>
</form>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap#5.0.0-beta3/dist/js/bootstrap.bundle.min.js" integrity="sha384-JEW9xMcG8R+pH31jmWH6WWP0WintQrMb4s7ZOdauHnUtxwoG2vI5DkLtS3qm9Ekf" crossorigin="anonymous"></script>
</body>
</html>

I solve this problem , actually i was using > in under function that is a mistake.
if (mysqli_num_rows($uq) > 0)){}

Related

Can not update my php and mysql database data

I need to update my data in database. I am successfully created the user registration and login form in php, so i need to update and delete data in database. And I am getting error message update function.
There is condition: id(primary) and email(unique) & username(unique).
Error message in this program:
"Error updating record: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 1."
<?php
session_start();
require('connect.php');
if(isset($_POST['submit'])){
$id = $_POST['id'];
$username = $_POST['username'];
$name = $_POST['name'];
$email = $_POST['email'];
$password = md5($_POST['password']);
$dob = $_POST['dob'];
$gender = $_POST['gender'];
$location = $_POST['location'];
$course = $_POST['course'];
$mobile = $_POST['mobile'];
$sql = " UPDATE user SET username ='$username', name = '$name', email = '$email', password='$password', dob ='$dob', gender ='$gender',location ='$location', course = '$course', mobile = '$mobile' WHERE id= $id ";
if (mysqli_query($mysqli, $sql)) {
$smsg = "Record updated successfully";
} else {
$fmsg = "Error updating record: " . mysqli_error($mysqli);
}
}
?>
<html>
<head>
<title>User Update PHP & MYSQL</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" >
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap-theme.min.css" >
<link rel="stylesheet" href="main.css" >
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<a class= "float-right" href = "logout.php"> LOGOUT </a>
<?php if(isset($smsg)){ ?><div class="alert alert-success" role="alert"> <?php echo $smsg; ?> </div><?php } ?>
<?php if(isset($fmsg)){ ?><div class="alert alert-danger" role="alert"> <?php echo $fmsg; ?> </div><?php } ?>
<form class="form-signin" method="POST">
<h2 class="form-signin-heading">Edit Profile</h2>
<div class="input-group">
<input type="hidden" name="id" placeholder="ID">
<input type="text" name="username" class="form-control" placeholder="username" required></div>
<label for="inputname" class="sr-only">Full Name</label>
<input type="text" name="name" id="inputname" class="form-control" placeholder="Full Name"required>
<label for="inputEmail" class="sr-only">Email address</label>
<input type="email" name="email" id="inputEmail" class="form-control" placeholder="Email address"required>
<label for="inputPassword" class="sr-only">Password</label>
<input type="password" name="password" id="inputPassword" class="form-control" placeholder="Password" required>
<label for="inputDoB" class="sr-only">DOB</label>
<input type="date" name="dob" id="inputDoB" class="form-control" placeholder="DOB"required>
<td>Gender:</td>
<input type="radio" name="gender" value="m">Male
<input type="radio" name="gender" value="f">female
</tr>
<label for="inputlocation" class="sr-only">Location</label>
<input type="text" name="location" id="inputlocation" class="form-control" placeholder="Location"required>
<label for="inputcourse" class="sr-only">Course</label>
<input type="text" name="course" id="inputcourse" class="form-control" placeholder="Course" required>
<label for="inputmobile" class="sr-only">Mobile</label>
<input type="text" name="mobile" id="inputmobile" class="form-control" placeholder="Mobile" required>
<button class="btn btn-lg btn-primary btn-block" type="submit" name="submit">Update</button>
</div>
</body>
</head>
</html>
You have not added value to id.
It is a hidden variable, so, it can only be set through PHP or JS not with user input.
Therefore, your query after where clause is blank and causing error.
Solution:
Assign id a value.
<input type="hidden" name="id" placeholder="ID" value="<?php echo $user_id;?>">
And to avoid this error, check if id is posted.
Modify:
if(isset($_POST['submit'])) {
To:
if(isset($_POST['submit']) && ! empty($_POST['id'])){

inserting a blank row before inserting the correct one mysql

I am working on a angular5 / php backend project, and I am having an issue where I have a registration form, when the user registers it creates a blank row (empty strings values) in the user table followed by the correct row (colomn values).
The following is my form:
<form *ngIf="!isLogin" (submit)="onRegisterSubmit()" [formGroup]="registerForm">
<div class="form-control">
<input type="text" name="firstname" id="firstname" class="form-control" placeholder="Firstname" formControlName="fn">
<div class="throw_error" *ngIf="registerForm.controls.fn.invalid && registerForm.controls.fn.touched">
<div *ngIf="registerForm.controls.fn.errors?.required">This field is required</div>
<div *ngIf="registerForm.controls.fn.errors?.minlength">This field must be at least 3 characters</div>
<div *ngIf="registerForm.controls.fn.errors?.maxlength">This field must have at most 10 characters</div>
</div>
</div>
<div class="form-control">
<input type="text" name="lastname" id="lastname" class="form-control" placeholder="Lastname" formControlName="ln">
<div class="throw_error" *ngIf="registerForm.controls.ln.invalid && registerForm.controls.ln.touched">
<div *ngIf="registerForm.controls.ln.errors?.required">This field is required</div>
<div *ngIf="registerForm.controls.ln.errors?.minlength">This field must be at least 3 characters</div>
<div *ngIf="registerForm.controls.ln.errors?.maxlength">This field must have at most 10 characters</div>
</div>
</div>
<div class="form-control">
<input type="email" name="email2" id="email2" class="form-control" placeholder="Email Address" formControlName="email2">
<div class="throw_error" *ngIf="registerForm.controls.email2.invalid && registerForm.controls.email2.touched">
<div *ngIf="registerForm.controls.email2.errors?.required">This field is required</div>
<div *ngIf="registerForm.controls.email2.errors?.email && !registerForm.controls.email2.errors?.required">This email is invalid</div>
</div>
</div>
<div class="form-control">
<input type="password" name="password2" id="password2" class="form-control" placeholder="Password" formControlName="password2">
<div class="throw_error" *ngIf="registerForm.controls.password2.invalid && registerForm.controls.password2.touched">
<div *ngIf="registerForm.controls.password2.errors?.required">This field is required</div>
<div *ngIf="registerForm.controls.password2.errors?.minlength">This field must be at least 6 characters</div>
<div *ngIf="registerForm.controls.password2.errors?.maxlength">This field must have at most 15 characters</div>
</div>
</div>
<div class="form-control">
<input type="password" name="confirmPassword" id="confirm-password" class="form-control" placeholder="Confirm Password" formControlName="confPass">
<div class="throw_error" *ngIf="registerForm.controls.confPass.touched && registerForm.controls.confPass.errors?.MatchPassword">Passwords do not match</div>
</div>
<div class="form-control">
<div class="row">
<div class="col-sm-3"></div>
<div class="col-sm-6">
<input type="submit" name="register-submit" id="register-submit" [disabled]="registerForm.invalid"
class="form-control btn btn-register" value="Submit">
<span class="throw_error" id="success">{{resultReg}}</span>
</div>
<div class="col-sm-3"></div>
</div>
</div>
</form>
This is my typescript code for the registration function:
onRegisterSubmit() {
const regFormValue = this.registerForm.value;
this.http.post("http://localhost/ProjetErgonomie/RegUser.php", regFormValue)
.subscribe(data => {
this.receivedData = data;
if (this.receivedData.success) {
this.resultReg = "User added successfully";
} else {
this.resultReg = this.receivedData.errors;
}
}, (error) => {
console.log(error);
});
}
And this is my RegUser.php page:
<?php
header("Access-Control-Allow-Headers: Content-Type");
header("Access-Control-Allow-Origin: *");
require_once ('./DBConnect.php');
$conn = mysqli_connect($servername, $username, $password, $db ,$port);
$regUserData = json_decode(file_get_contents("php://input"),true);
$fn = $regUserData['fn'];
$ln = $regUserData['ln'];
$email = $regUserData['email2'];
$pass = $regUserData['password2'];
$confPass = $regUserData['confPass'];
$errorsReg = "";
$dataReg=array();
if($fn === '' || $ln === '' || $email === '' || $pass === '' || $confPass === ''){
$errorsReg = 'Please fill all fields';
$dataReg['success'] = FALSE;
}
else{
$checkUserQuery = "select * from user where Email = '".$email."'";
$result = $conn->query($checkUserQuery);
if(mysqli_num_rows($result)>0){
$errorsReg = 'User already exists';
$dataReg['success'] = FALSE;
}
else{
$sql = "INSERT INTO user (Firstname, Lastname, Email, Password, NewUser)
VALUES ('".$fn."','".$ln."','".$email."','".md5($pass)."',1)";
mysqli_query($conn,$sql);
$dataReg['success'] = TRUE;
}
}
$dataReg['errors']=$errorsReg;
echo json_encode($dataReg);
can you please tell me why am I getting a blank row before having the correct row inserted?

login and registration in php wrong redirection

register form css and html
<div class="col-md-12">
<div class="box">
<div class="box-header">
<center>
<h2>Register A New Account</h2>
</center>
</div>
<!-- box-header ends -->
<form action="signup.php" enctype="multipart/form-data" method="post">
<div class="form-group">
<label>FIRST NAME</label> <input class="form-control" name="first" required="" type="text">
</div>
<!--form group -->
<div class="form-group">
<label>LAST NAME</label> <input class="form-control" name="last" required="" type="text">
</div>
<!-- form group -->
<div class="form-group">
<label>Email</label> <input class="form-control" name="email" required="" type="text">
</div>
<!-- form group -->
<div class="form-group">
<label>USER NAME</label> <input class="form-control" name="uid" required="" type="text">
</div>
<!-- form group -->
<div class="form-group">
<label>PASSWORD</label> <input class="form-control" id="pwd" name="pwd" onchange="validatePassword()" required="" type="password">
</div>
<!-- form group -->
<div class="form-group">
<label>CONFIRM PASSWORD</label> <input class="form-control" id="cpwd" name="cpwd" required="" type="password">
</div>
<!-- form group -->
<div class="form-group">
<label>COUNTRY</label> <input class="form-control" name="country" required="" type="text">
</div>
<!-- form group -->
<div class="form-group">
<label>CITY</label> <input class="form-control" name="city" required="" type="text">
</div>
<!-- form group -->
<div class="form-group">
<label>ADDRESS</label> <input class="form-control" name="address" required="" type="text">
</div>
<!-- form group -->
<div class="form-group">
<label>CONTACT</label> <input class="form-control" name="phone" required="" type="text">
</div>
<!-- form group -->
<div class="form-group">
<label>PROFILE PICTURE</label> <input class="form-control" name="image" required="" type="file">
</div>
<!-- form group -->
<div class="text-center">
<button class="btn btn-primary" name="submit" type="submit"><i class="fa fa-user-md" style="padding:0px 5px;"></i>Sign Up</button>
</div>
<!-- text-center -->
</form>
<!-- form ends -->
</div>
<!-- box ends -->
</div>
<!-- col-m-9 ends -->
php code to enter into database and validate
<?php
use PHPMailer\PHPMailer\PHPMailer;
if (isset($_POST['submit'])) {
include_once 'includes/db.php';
$first = mysqli_real_escape_string($conn, $_POST['first']);
$last = mysqli_real_escape_string($conn, $_POST['last']);
$email = mysqli_real_escape_string($conn, $_POST['email']);
$uid = mysqli_real_escape_string($conn, $_POST['uid']);
$pwd = mysqli_real_escape_string($conn, $_POST['pwd']);
$country = mysqli_real_escape_string($conn, $_POST['country']);
$city = mysqli_real_escape_string($conn, $_POST['city']);
$phone = mysqli_real_escape_string($conn, $_POST['phone']);
$address = mysqli_real_escape_string($conn, $_POST['address']);
$img = mysqli_real_escape_string($conn, $_FILES['image']['name']);
$temp_name = mysqli_real_escape_string($conn, $_FILES['image']['tmp_name']);
$userip = getUserIP();
move_uploaded_file($temp_name, "customer/customer_images/$img");
//Error handlers
//Check for empty fields
if (empty($first) || empty($last) || empty($email) || empty($uid) || empty($pwd) || empty($country) || empty($city) || empty($phone) || empty($address) || empty($img)) {
echo "<script>alert('Empty Fields.Please fill all the details');
window.location.href='../signup.php?signup=empty';
</script>";
exit();
} else {
//check if input character are valid
if (!preg_match("/^[a-zA-z]*$/", $first) || !preg_match("/^[a-zA-z]*$/", $last)) {
echo "<script>alert('Invalid Characters');
window.location.href='../signup.php?signup=invalid';
</script>";
exit();
} else {
//check valid email
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
echo "<script>alert('Ivalid email id');
window.location.href='../signup.php?signup=email';
</script>";
exit();
} else {
//check if username is same as others'
$sql = "SELECT * FROM users WHERE user_uid='$uid' AND user_email='$email'";
$result = mysqli_query($conn, $sql);
$resultCheck = mysqli_num_rows($result);
if ($resultCheck > 0) {
echo "<script>alert('email or username already exist.Please try again with new email id');
window.location.href='../signup.php?signup=userexist';
</script>";
exit();
} else {
//Hash the password
$hashedpwd = password_hash($pwd, PASSWORD_BCRYPT);
$token = 'qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM0123456789!##$%^&*()';
$token = str_shuffle($token);
$token = substr($token, 0, 10);
$sel_cart = "select * from cart where ip_add='$user_ip'";
$run_cart = mysqli_query($conn, $sel_cart);
$cart_check = mysqli_num_rows($run_cart);
//Insert user into database
$sql = "INSERT INTO users (user_first,user_last,user_email,user_uid,user_pwd,isEmailConfirmed,token,country,city,address,phone,image,userip) VALUES('$first','$last','$email','$uid','$hashedpwd','0','$token','$country','$city','$address','$phone','$img','$userip');";
mysqli_query($conn, $sql);
if ($cart_check > 0) {
$_SESSION['user_email'] = $email;
echo "<script>alert('Registration successfull.Please check your mail to verfiy the details.Thank you');
window.open('checkout.php','_self');
</script>";
} else {
$_SESSION['user_email'] = $email;
echo "<script>alert('Registration successfull.Please check your mail to verfiy the details.Thank you');
window.location.href='../signup.php?signup=successfull';
window.open('index.php','_self');
</script>";
}
}
}
}
}
} else {
header("Location: ../signup.php");
exit();
}
Guys this my code for registration. The problem here is that when a user who is not registered and adds a product into his cart goes to registration page. After signing up he needs to go the checkout page . If there are no items inside the cart then the page needs to redirected to index.php . The issue i face is the data gets entered into database when the cart has one item and then after registering it redirects to index.php rather than checkout.php .
There is small variable name mistake in
$sel_cart = "select * from cart where ip_add='$user_ip'";
You have added $user_ip instead of $userip. update your code by following,
$sel_cart = "select * from cart where ip_add='$userip'";

Validating a unique userid in PHP, returning error if not unique [duplicate]

This question already has answers here:
How to prevent duplicate usernames when people register?
(4 answers)
Closed 1 year ago.
I'm trying to force users to pick a unique username and returning an error message if they don't. All other validation (password matching, etc.) is working but validating an unused username just returns ID Registration failed message.
I've updated this with the HTML as requested.
I just want it to tell the user the errors I've outlined in those cases.
<?php
require('connect.php');
if(isset($_POST) & !empty($_POST)){
// If the values are posted, insert them into the database.
// if (isset($_POST['app_id']) && isset($_POST['password'])){
$app_id = mysqli_real_escape_string($connection, $_POST['app_id']);
$first = mysqli_real_escape_string($connection, $_POST['first']);
$last = mysqli_real_escape_string($connection, $_POST['last']);
$gender = mysqli_real_escape_string($connection, $_POST['gender']);
$birth = mysqli_real_escape_string($connection, $_POST['birth']);
$email = mysqli_real_escape_string($connection, $_POST['email']);
$password = mysqli_real_escape_string($connection, md5($_POST['password']));
$confirmpassword = mysqli_real_escape_string($connection, md5($_POST['confirmpassword']));
if($password == $confirmpassword){
$fmsg = "";
//username validation
$newidvalq = "SELECT * FROM 'user' WHERE app_id='$app_id'";
$newidres = mysqli_query($connection, $newidvalq);
$idcount = 0;
$idcount = mysqli_num_rows($newidres);
if($idcount >= 1){
$fmsg .= "That app ID is already being used, please try a different ID";
}
//email validation
$emailvalq = "SELECT * FROM 'user' WHERE email='$email'";
$emailres = mysqli_query($connection, $emailvalq);
$emailcount = 0;
$emailcount = mysqli_num_rows($emailres);
if($emailcount >= 1){
$fmsg .= "That email is already being used";
}
//DB Insert
$query = "INSERT INTO `user` (app_id, first, last, gender, birth, password, email) VALUES ('$app_id', '$first', '$last', '$gender', '$birth', '$password', '$email')";
//Result Validation
$result = mysqli_query($connection, $query);
if($result){
$smsg = "app ID Created Successfully";
}else{
$fmsg .= "app Registration Failed";
}
}else{
$fmsg = "Your Passwords do not match";
}
}
?>
<html>
<head>
<title>User Registeration Using PHP & MySQL</title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" >
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" >
<link rel="stylesheet" href="styles.css" >
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<form class="form-signin" method="POST">
<?php if(isset($smsg)){ ?><div class="alert alert-success" role="alert"> <?php echo $smsg; ?> </div><?php } ?>
<?php if(isset($fmsg)){ ?><div class="alert alert-danger" role="alert"> <?php echo $fmsg; ?> </div><?php } ?>
<h2 class="form-signin-heading">Please Register</h2>
<div class="input-group">
<span class="input-group-addon" id="basic-addon1">#</span>
<input type="text" name="app_id" class="form-control" placeholder="app ID" value="<?php if(isset($app_id) & !empty($app_id)) {echo $app_id;} ?>" required>
</div>
<input type="text" name="first" class="form-control" placeholder="First Name" value="<?php if(isset($first) & !empty($first)) {echo $first;} ?>"required>
<input type="text" name="last" class="form-control" placeholder="Last Name" value="<?php if(isset($last) & !empty($last)) {echo $last;} ?>"required>
<input type="text" name="gender" class="form-control" placeholder="Gender" value="<?php if(isset($gender) & !empty($gender)) {echo $gender;} ?>"required>
<input type="date" name="birth" class="form-control" placeholder="Birthday" required>
<label for="inputEmail" class="sr-only">Email Address</label>
<input type="email" name="email" id="inputEmail" class="form-control" placeholder="Email address" value="<?php if(isset($email) & !empty($email)) {echo $email;} ?>"required autofocus>
<label for="inputPassword" class="sr-only">Password</label>
<input type="password" name="password" id="inputPassword" class="form-control" placeholder="Enter a Password" required>
<label for="inputPassword" class="sr-only">RetypePassword</label>
<input type="password" name="confirmpassword" id="inputPassword" class="form-control" placeholder="Confirm Your Password" required>
<div class="checkbox">
<label>
<input type="checkbox" value="remember-me"> Remember me
</label>
</div>
<button class="btn btn-lg btn-primary btn-block" type="submit">Register</button>
<a class="btn btn-lg btn-primary btn-block" href="login.php">Login</a>
</form>
</div>
</body>
</html>
assign $idcount with 0 first before using and then in the if condition use ($idcoun>=1)
And According to your logic if the userid and email exist also then also you are inserting the data which make no sense according to your need
Enclose your INSERT block in:
if( $fmsg == "" ) { ..to here.. }
So that if there was an error, no record will be inserted to database, and error will be displayed.
Add action attribute to form tag like:
<form class="form-signin" method="POST" action="">
It is required in some versions of html not html5.

PHP form handling with post prints nothing

I am trying check if username or email already exists . This is my form
<form role="form" action="kys_SignUp.php" method="post">
<div class="form-group">
<label for="email" >Email address:</label>
<input type="email" style="width: 300px" class="form-control" name="email" id="email" required>
</div>
<div class="form-group">
<label for="Username" >Username:</label>
<input type="text" style="width: 300px" class="form-control" name="username" id="Username" required>
</div>
<div class="form-group">
<label for="password" >Password:</label>
<input type="password" style="width: 300px" class="form-control" id="password" name="password" required>
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
I am sending the data in Post Method. My server side script looks like this
<?php
include "kys_DbConnect.php";
$email = $username = $password = "";
if($_SERVER["REQUEST_METHOD"] == "POST"){
$email = cleanData($_POST["email"]);
$username = cleanData($_POST["username"]);
$password = cleanData($_POST["password"]);
}
$stmt = $con->prepare("SELECT * FROM kys_users WHERE username=? OR email=?");
$stmt->bind_param("ss",$username,$email);
$stmt->execute();
$stmt->bind_result($id,$email,$username,$password);
$stmt->fetch();
if($username == ""){
header("Location : http://localhost/KeyStroke/index.html ");
exit();
}
else{
echo "Username or Email already exists";
}
function cleanData($data){
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
This is my kys_DbConnect.php
<?php
$serverName = "localhost";
$username = "jois";
$password = "iluvcoding";
$db_name = "kys_userdata";
$con = new mysqli($serverName,$username,$password,$db_name);
if($con->connect_error){
echo "Connection Error";
}
?>
I don't know why but it print's nothing nor redirects to index.html (index.html exists). How can I figure out what's wrong with my code?

Categories