• 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 23 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 4 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 16 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 10 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 18 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 26 views
0 votes
1 answer 16 views
I need to import xlsx file into mysql database applying PHP.
asked Sep 25, 2020 Daniel Anderson 4k points
0 votes
1 answer 12 views
Problem: i wander how to use if else condition in mysql query in PHP. Can anyone assist me with a example of getting last inserted id using if else condtion with query.
asked Oct 29, 2020 prog_learner 2.5k points
0 votes
1 answer 13 views
I have three tables that are link to each other via id of the main entry. For example, I need to create a user entry with username, name and password in the user table. And then after this information is created into the database, I need to create two new information to handle its ... label> <input type="text" name="name" id="name"><br><br> <button id="submit" name="submit">Submit</button> </form>
asked Oct 28, 2020 RJ Lam 650 points
0 votes
1 answer 11 views
I need to update my database but it seems not working. I posted my code here. I don&rsquo;t know what&rsquo;s wrong with this code. Can somebody help me? The record already exists in my database with the id of 1. But when I execute this code, and make an entry, then click the update ... "> </td> </tr> </table> <input name="save" type="submit" id="update" value="Update"> </form> </body> </html>
asked Oct 20, 2020 azriel 590 points