• Register
0 votes

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 :


  {} 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

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





8 4 2
2,300 points

Please log in or register to answer this question.

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 3 views
Problem: I am using the following PHP code to store data in my MySQL database, everything works perfect but I would like to build in a check; if the email already exists in the database then redirect to another page. (I'm a rookie when it comes to PHP)
asked Mar 31 ummesalma 25.2k points
0 votes
1 answer 41 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, 2020 Raphael Pacheco 4.9k points
0 votes
1 answer 12 views
Problem: check if email address already exists in the database using jquery and php I am attempting to validate an email address if it already exists in a table, but this isn't working. Can anyone help me?? Any help will be appreciated..!
asked Feb 25 Umar Ahmad 4.1k points
0 votes
1 answer 3 views
Problem: please help me to solve it... how to check if an email address exists without sending an email in php
asked Apr 1 Ifra 24.4k points
0 votes
1 answer 12 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, 2020 Daniel Anderson 4k points
0 votes
1 answer 57 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, 2020 Raphael Pacheco 4.9k points
0 votes
1 answer 19 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 Oct 28, 2020 RJ Lam 650 points
0 votes
1 answer 27 views
I am finding difficulty to insert hindi text in MySQL database using PHP
asked Oct 4, 2020 Daniel Anderson 4k points
0 votes
1 answer 43 views
0 votes
1 answer 20 views
I need to import xlsx file into mysql database applying PHP.
asked Sep 25, 2020 Daniel Anderson 4k points