Sorry my english is not very good but i need your helps to find what's wrong in below php file.
I'm just learning PHP and am trying to capture info from a form and insert it into a table in a mySQL database.Before doing that,i want to check for duplicate username or email, so i added an if statement but no data was inserted into table and no error was shown.
. Could someone please tell me what I am doing wrong?
Before adding if statement, it worked.
<?php
include('config.php');
$name = $_POST['name'];
$user = $_POST['id'];
$password = $_POST['password'];
$mail = $_POST['email'];
$phone = $_POST['phone'];
$sex = $_POST['sex'];
$nationality = $_POST['nationality'];
$dob=$_POST['year'] . '-' . $_POST['month'] . '-' . $_POST['days'];
$question = $_POST['mistery_question'];
$reply = $_POST['reply'];
date_default_timezone_set("Asia/Calcutta");
$insertdate = date("Y-m-d H:i:s");
$sql = "INSERT INTO regist (Name, Username, Password, Mail, PhoneNumber, Sex, Religion, DateOfBirth, Question, Answer,date_time)
VALUES ('$name', '$user','$password','$email', '$phone', '$sex', '$nationality', '$dob', '$question', '$reply', '$insertdate')";
mysqli_close($conn);
?>
What I have tried:
After adding an if statement. It does not work
<?php
include('config.php');
$name = $_POST['name'];
$user = $_POST['id'];
$password = $_POST['password'];
$mail = $_POST['email'];
$phone = $_POST['phone'];
$sex = $_POST['sex'];
$nationality = $_POST['nationality'];
$dob=$_POST['year'] . '-' . $_POST['month'] . '-' . $_POST['days'];
$question = $_POST['mistery_question'];
$reply = $_POST['reply'];
date_default_timezone_set("Asia/Calcutta");
$insertdate = date("Y-m-d H:i:s");
$query = mysqli_query($conn,"SELECT * FROM regist WHERE Username='$user' OR Mail='$mail'");
if(mysqli_num_rows($query) > 0 ) {
echo "<script type='text/javascript'>alert('User name or Email id already exists.');
window.location='regist.html';history.back();
</script>";
}else{
$sql = "INSERT INTO regist (Name, Username, Password, Mail, PhoneNumber, Sex, Religion, DateOfBirth, Question, Answer,date_time)
VALUES ('$name', '$user','$password','$email', '$phone', '$sex', '$nationality', '$dob', '$question', '$reply', '$insertdate')";
}
mysqli_close($conn);
?>