0

This is my code i was trying that if I enter my email and pass where saved in my database(MySQL) will show that if I entered it correctly it shows that i logged in as that email. So i guess you can help me of where am i wrong on my coding, btw i am new in web programming so, I am greatful if you teach me the easiest to understand and the simpliest way.

<?php
    $servername = "localhost";
    $username = "root";
    $password = "crazyjaguar";
    $db = "getstarted";

    // Create connection
    $conn = mysqli_connect($servername, $username, $password);

    // Check connection
    if (!$conn) {
        die("Connection failed: " . mysqli_connect_error());
    }
    echo "Connected successfully";




    if ($_SERVER["REQUEST_METHOD"] == "POST") {
    //  error_reporting(0);
        if ($_POST['email'] && $_POST['pwd']) {

            // escape
                $email = mysqli_real_escape_string($conn, $_POST['email']);
                $password = mysqli_real_escape_string($conn, $_POST['pwd']);
                // sql email and pass
                $sql = "SELECT email, password FROM gs_users";

                // query esql & espl
                $result = mysqli_query($conn, $sql);

                // fetch_array
                $row = mysqli_fetch_array($result,MYSQLI_BOTH);

                    if ($row['email'] != $email) {

                        die ("No $email registered yet!");
                    }
                    if ($row['password'] = $password) {

                        die ("Wrong $pmail!");
                    }
                        echo "You're logged in as $email";

                        mysqli_close($conn);
    }   
    }

    ?>


    <body>
    <h1>Log In</h1>

    <form action='' method='post'>

    <input type='email' name='email' placeholder='Email'>
    <input type='password' name='pwd' placeholder='Password'><br />
    <a href='forgot.php'>Forgost Password?</a><br />
    <input type='submit' name='signup' value='Signup Here!'><br />
    <input type='submit' name='login' value='LogIn'>
    </form>
    </body>
japjap
  • 25
  • 1
  • 1
  • 7

2 Answers2

0

The mysqli_connect function can actually take 4 parameters. The last one is the database name.

syntax would be

mysqli_connect(host,username,password,dbname);

Though database name is optional even if you do not provide default database will be used.

But again you've alerady specified

$db = "getstarted";

proves you missed including database name :)

You can then use mysqli_select_db() to select the database that you want to query.

At last i would request you to use PDO.

Shashank Shah
  • 2,077
  • 4
  • 22
  • 46
  • is it better in mysqli ? it says that mysqli is easier to understand for the beginners. – japjap Mar 10 '16 at 09:56
  • Ignore the case of beginners! there are many advantages of using PDO over mysqli functions http://stackoverflow.com/questions/13569/mysqli-or-pdo-what-are-the-pros-and-cons – Shashank Shah Mar 10 '16 at 10:04
  • No, its easy! suppose you want to insert! you can try $stmt = $dbh->prepare("INSERT INTO TABLENAME (name, value) VALUES (:name, :value)"); $stmt->bindParam(':name', $name); $stmt->bindParam(':value', $value); $dbh is database object. Easy ? – Shashank Shah Mar 10 '16 at 10:08
0

Here I assume that you want to first check email, if email dose not exist then die with message "email not register yet", If email exist then check password enter by input, If password match then you are logedin.

Please change your code with following code..

<?php
    $servername = "localhost";
    $username = "root";
    $password = "crazyjaguar";
    $db = "getstarted";

    // Create connection
    $conn = mysqli_connect($servername, $username, $password, $db);

    // Check connection
    if (!$conn) {
        die("Connection failed: " . mysqli_connect_error());
    }
    echo "Connected successfully";




    if ($_SERVER["REQUEST_METHOD"] == "POST") {
    //  error_reporting(0);
        if ($_POST['email'] && $_POST['pwd']) {

            // escape
                $email = mysqli_real_escape_string($conn, $_POST['email']);
                $password = mysqli_real_escape_string($conn, $_POST['pwd']);
                // sql email and pass
                $sql = "SELECT email, password FROM gs_users where email = $email";

                // query esql & espl
                $result = mysqli_query($conn, $sql);
                if (mysqli_num_rows($result) > 0)
                {
                    $row = mysqli_fetch_array($result,MYSQLI_BOTH);
                    if ($row['password'] == $password)
                    {
                        echo "You're logged in as $email";
                    }
                    else
                    {
                        die ("Invalid password!");       
                    }
                }
                else
                {
                    die ("No $email registered yet!");
                }
        }   
    }
    mysqli_close($conn);

    ?>


    <body>
    <h1>Log In</h1>

    <form action='' method='post'>

    <input type='email' name='email' placeholder='Email'>
    <input type='password' name='pwd' placeholder='Password'><br />
    <a href='forgot.php'>Forgost Password?</a><br />
    <input type='submit' name='signup' value='Signup Here!'><br />
    <input type='submit' name='login' value='LogIn'>
    </form>
    </body>

Hope this will help you.

Sanjay Chaudhari
  • 420
  • 4
  • 13