0

I'm trying to solve a problem pertaining to PHP. So far I have created an html login form login.html and a PHP file login-insert.php. I'm having trouble detecting if the username already exists in the database created using phpmyadmin. I'm very new to coding in PHP so any help is appreciated

Please refer to link for PHP and html code https://liveweave.com/rAE7Cy

6
  • 1
    Does this "form" have an action to post/get the data to login-insert.php? If login-insert.php exists could you share that code here? Commented Mar 22, 2019 at 12:33
  • There is no form inside your database, see how to make a form here: w3schools.com/html/html_forms.asp Commented Mar 22, 2019 at 12:34
  • 1
    For your php script, you have to do it yourself, learn how to use php to connect to your database here: w3schools.com/php/php_mysql_connect.asp and perform select query here: w3schools.com/php/php_mysql_select.asp Commented Mar 22, 2019 at 12:36
  • @tshimkus look i have share my login-insert.php code link in the description please refer that. Commented Mar 23, 2019 at 6:05
  • Can anyone here help me out?? Commented Mar 23, 2019 at 8:59

2 Answers 2

1

Step 1 create a file config.php

<?php
   define('DB_SERVER', 'localhost:3036');
   define('DB_USERNAME', 'root');
   define('DB_PASSWORD', 'rootpassword'); //modify it as per your password
   define('DB_DATABASE', 'database'); //modify it as per your databse
   $db = mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
?>

Step 2 create another file login.php

<?php
   include("config.php");
   session_start();

   if($_SERVER["REQUEST_METHOD"] == "POST") {
      // username and password sent from form 

      $myusername = mysqli_real_escape_string($db,$_POST['uname']);
      $mypassword = mysqli_real_escape_string($db,$_POST['psw']); 

      $sql = "SELECT id FROM admin WHERE username = '$myusername' and password = '$mypassword'";
      $result = mysqli_query($db,$sql);
      $row = mysqli_fetch_array($result,MYSQLI_ASSOC);
      $active = $row['active'];

      $count = mysqli_num_rows($result);

      // If result matched $myusername and $mypassword, table row must be 1 row

      if($count == 1) {
         session_register("username");
         $_SESSION['login_user'] = $myusername;

         header("location: home.php");
      }else {
         $error = "Your Login Name or Password is invalid";
      }
   }
?>
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks mate for help but still something missing. Line 13 "$row = mysqli_fetch_array($result,MYSQLI_ASSOC);" and line 16 "$count = mysqli_num_rows($result);" error occured. the error is 1. "Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\copy\login-insert.php on line 13" and "Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\copy\login-insert.php on line 16".
hey can anyone here help me out ?
0

Change your file name with the extension login.php instead of login.html and then send the variable value through ajax in the same page

<?php

    // starting the session
    session_start();

    // checking if username and password entered of not
    if(isset($_POST['userid']) && isset($_POST['userpass'])){
        $server = 'server_name';
        $user = 'server_username';
        $pass = 'server_password';
        $db = 'database_name';

        $conn = new mysqli($server, $user, $pass, $db);

        $userid = base64_encode($_POST['userid']);
        $userpass = base64_encode($_POST['userpass']);
        $useremail = $_POST['userid'];

        $q = "SELECT * FROM table_name WHERE UserID='$userid' AND UserPWD='$userpass'";  // Fetching data from table
        $d = mysqli_query($conn, $q);
        if(mysqli_num_rows($d) == 1){
            while ($t = mysqli_fetch_assoc($d)) {
                $_SESSION['login_status'] = 1; // Creating A login status variable in case of logout make this variable value 0
                echo '1';
            }
        }else{
            echo '2';
        }
        mysqli_close($conn);
    }else{  //if username and password not entered
        ?>
            <input type="text" class="userid" placeholder="Username">
            <input type="password" class="userpass" placeholder="Password">
            <a href="#" class="login_btn ">login</a>

            <script>

                $('.login_btn').on('click',function(){
                    var userid = $('.userid').val();
                    var userpass = $('.userpass').val();
                    if(userid != ""){
                        if(userpass != ""){
                            $.ajax({
                                type:'post',
                                data:{
                                    userid:userid,
                                    userpass:userpass,
                                },
                                url:'login.php',  // Sending variable to same page

                                success:function(e){
                                    if(e == 1){ // Userid and password is correct
                                        window.location = 'index.php';
                                    }else if(e == 2){
                                        alert("Invalid Username and password");
                                    }else{
                                        console.log(e);
                                    }
                                }
                            })
                        }else{
                            alert("Password Required");
                        }
                    }else{
                        alert("Username Required");
                    }
                })
            </script>
        <?php        
    }

?>

8 Comments

if you can explain than i will
liveweave.com/LKpy8a plese check this link i have done this so far.
it seems like you have just started html css
I have already told you in the above code if there is any confusion that you can ask specifically
According to your code i have done some modifications as per my database names and table names but still missing something. liveweave.com/tAOyYg please check my modified code this code contain both html and php files.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.