Click here to Skip to main content
15,892,161 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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);

?>
Posted
Comments
Richard Deeming 30-Oct-18 13:28pm    
You're storing passwords in plain text. Don't do that!
Secure Password Authentication Explained Simply[^]
Salted Password Hashing - Doing it Right[^]

PHP even includes functions to help you do the right thing:
PHP: password_hash[^]
PHP: password_verify[^]
Richard MacCutchan 30-Oct-18 13:35pm    
You never execute the INSERT statement (and did not in the first sample), nothing to do with the if.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900