Php Form Submission To Mysql Database
I have a registration form. In the database, the username and email are unique index. When the form submits and username or email are already present in the database, the values ar
Solution 1:
*Sweet And Short *
First check that username or email is exist or not using select
query if resulting is 0 (it means not exists), Insert
query will run ahead
<?phpif($_POST['register']){
$uname = $_POST['uname'];
$email = $_POST['email'];
$name= $_POST['name'];
$pass= $_POST['pass'];
$result = mysqli_query($con, 'SELECT * from TABLE_NAME where email_id = "'.$email.'" or username = "'.$uname.'" ');
if(mysqli_num_rows($result) > 0){
echo"Username or email already exists.";
}else{
$query = mysqli_query($con , 'INSERT INTO TABLE_NAME (`email_id`, `username`,`name`,`pass`) VALUES("'.$email.'", "'.$email.'", "'.$uname.'","'.$name.'", "'.$pass.'")');
if($query){
echo"data are inserted successfully.";
}else{
echo"failed to insert data.";
}
}
}
?>
Solution 2:
Solution 3:
Try the following Code
include ("db.php");
if (isset($_POST['register']))
{
echo$name = ($_POST["name"]);
echo$email = ($_POST["email"]);
echo$uname = ($_POST["uname"]);
echo$password = ($_POST["pass"]);
$var = mysqli_query('SELECT * from company_profile where email_id = "'.$email.'" or username = "'.$uname.'" ');
$num = mysqli_num_rows($var);
if($num==0)
{
$result = INSERT INTO company_profile(user_name, password, company_name, email, phone, country, activation_string) VALUES ('$uname','$password','$name','$email','','','');
$res = mysqli_query($result);
if($res)
{
echo"Records Inserted Successfully!!";
}
else
{
echo"Records Inserted Failed!!";
}
}
else
{
echo"User with the Details Already exists!!"
}
}
Post a Comment for "Php Form Submission To Mysql Database"