• Register
0 votes
26 views

Problem :

I am beginner to PHP. I want to store my data in my MySQL database so I have written below code my code works perfectly but want to add the check if a email already exists in my database then I want to redirect to the another page.

My Code as shown below :

<?php

  if(empty($_POST[user'name']))
  {} else {

  define('MY_DB_NAME', 'mydbname');
  define(''MY_DB_USER', 'mydbuser');
  define(''MY_DB_PASSWORD', 'mydbpass');
  define(''MY_DB_HOST', 'myhost');

  $link = mysql_connect(MY_DB_HOST, MY_DB_USER, MY_DB_PASSWORD);

  if (!$link) { die('could not connect to DB: ' . mysql_error()); }

    $db_selected = mysql_select_db(MY_DB_NAME, $link);

  if (!$db_selected) { die('could not use ' . MY_DB_NAME . ': ' . mysql_error()); }

  $sql="INSERT INTO Register (username, lastname, company, email, time) VALUES
('".$_POST['username']."','".$_POST['lastname']."','".$_POST['company']."','".$_POST['email']."','".$_POST['time']."')";

  if (!mysql_query($sql)) { die('Fatal Error: ' . mysql_error()); }

  mysql_close();

  }

?>

 

8 4 2
2,300 points

1 Answer

0 votes

Solution :

I have gone through your question and your code. Answer to your question is very simple.

I am giving the solution to you in the form of below code.

if(!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
    exit('This is Invalid email address'); // Please use your own error handling 
}
$select = mysqli_query($connectionID, "SELECT `email` FROM ` Register ` WHERE `email` = '".$_POST['email']."'") or exit(mysqli_error($connectionID));
if(mysqli_num_rows($select)) {
    exit('This email is already in use');
}

Simply paste above code above your insert query.

5 2 1
4,980 points

Related questions

0 votes
1 answer 15 views
15 views
Problem: I have basic knowledge in Mysql and Php. I am trying to use my below code which is unfortunately not working for me. I am trying to check if the value already exists in my mysql database in php. $con=mysqli_connect("localhost","root","","my_db"); ... am facing because of my above php code: "Object of class mysqli_result could not be converted to int in C:\xampp\htdocs\Exp\welcome.php."
asked Aug 15 Raphael Pacheco 4.9k points
0 votes
1 answer 3 views
3 views
Basically we are trying at the time of registering users in a database table, we need to ensure that every user's email address or username is unique.
asked Sep 26 Daniel Anderson 4k points
0 votes
1 answer 10 views
10 views
Problem: I want to check if the very specific value already exists in the database. I am currently accessing database from the java standalone app using the JDBC my queries for inserting records into the db are working so my setup and the connection are perfectly fine. ... near 'd-f05708071f8f' at line 1 But if I attempt to execute above command in the MySQL command line then it works properly.
asked Aug 3 Raphael Pacheco 4.9k points
0 votes
1 answer 7 views
7 views
My small project is about making an entry of a recipe. So it includes some special characters. So when I made an entry and save them to the database, it generates an error. But when I tried to insert it to my PHPAdmin, everything works well. Here is my recipe ... ½ tablespoons water 6 cups chicken broth 2 ¼ tablespoons soy sauce 1 tablespoon fish sauce"; Anyone who can show me how to save this?
asked 2 days ago RJ Lam 300 points
0 votes
1 answer 10 views
10 views
I am finding difficulty to insert hindi text in MySQL database using PHP
asked Oct 4 Daniel Anderson 4k points