-1

I'm having trouble selecting data from a MySql database which I have created in phpMyAdmin. The output is always: "invalid username" even though I have inserted the data I want to select from the database named st_login and the table named login in phpMyAdmin. Can someone show me the error?

<html >
<head>
<title></title>
</head>
<body>
<?php
print ("<form action='logincontroltest.php' method='post'>

    <p>Username
        <input type='text' name='username' />
    </p>
    <p>Password
        <input type='password' name='password' >
        <p/>
    <input type='submit' value='Log In'/>
</form>");
extract ($_POST);
if( !($database=mysql_connect("localhost","root",""))||!(mysql_select_db("st_login",$database))  )
    print("Could not connect");
if(isset($_POST['username'])&&isset($_POST['password']) )
{
    $username=$_POST['username'];
    $password=$_POST['password'];
    if ( !empty($username) &&!empty($password) ) 
    {
        $query = " SELECT 'id' FROM login WHERE   'username'='$username' AND 'password'='$password'  ";
        if($result=mysql_query($query,$database))
        {
            $query_num_rows=mysql_num_rows($result);
            if ($query_num_rows==0){
                echo "invalid username";}
            else if($query_num_rows==1) {
                echo "You logged in successfully";
            }
        } 
        die (mysql_error());
    }
    else echo "Fill in all blank fields";
} 
    ?>
    </body>
</html>
Nisse Engström
  • 4,738
  • 23
  • 27
  • 42
John Dow
  • 47
  • 2
  • 7
  • 1
    Consult these following links http://php.net/manual/en/function.mysql-error.php and http://php.net/manual/en/function.error-reporting.php and apply that to your code. – Funk Forty Niner Jan 02 '16 at 21:36
  • A concrete response please! – John Dow Jan 02 '16 at 21:40
  • That is a concrete response. If you're going to want to code, you also need to do what goes with the territory, and that is called "debug". If you're just doing this for kicks, then have fun. – Funk Forty Niner Jan 02 '16 at 21:42
  • I'd of closed your question with that ^ I voted too soon. – Funk Forty Niner Jan 02 '16 at 21:45
  • `SELECT 'id'` you don't encapsulate the table names with `quotes`, you use backticks **`**, but in your case, `id` isn't reserved, so you can just use `id`. – Ohgodwhy Jan 02 '16 at 21:45
  • 'id' is a field table not a table name – John Dow Jan 02 '16 at 21:51
  • You've been given an answer below. A wrong one, but an answer nonetheless. Oh, sure it'll work; if your data are integers, which I highly doubt. – Funk Forty Niner Jan 02 '16 at 21:53
  • Here are 2 terms you also need to learn. 1) **String Literals** http://dev.mysql.com/doc/refman/5.7/en/string-literals.html 2) **Identifier Qualifiers** http://dev.mysql.com/doc/refman/5.7/en/identifier-qualifiers.html - That's if you're serious about coding (MySQL). If not, then just disregard it. – Funk Forty Niner Jan 02 '16 at 22:18
  • Thanks a lot @-Fred -ii- – John Dow Jan 02 '16 at 22:30

1 Answers1

0

Read about error reporting - http://php.net/manual/en/function.mysql-error.php

And, doesnt the single quotes in SQL query breaks it? Try to use this:

$query = " SELECT id FROM login WHERE username='$username' AND password='$password' "
user3038744
  • 86
  • 1
  • 8
  • Thanks! It really helped me even though the correct answer is: $query = " SELECT `id` FROM `login` WHERE `username`='$username' AND `password`='$password' "; – John Dow Jan 02 '16 at 22:00
  • @EldaBacka You're sending the wrong message here by accepting that answer in its present state. This tells the community and visitors to the answer that using it like that, is proper syntax, which isn't. – Funk Forty Niner Jan 02 '16 at 22:01
  • This is why i wrote the right answer in the comment.It was approximately a right answer – John Dow Jan 02 '16 at 22:03
  • 2
    @EldaBacka Still; wrong message. This person needs to edit their answer accordingly. A comment is a comment. An answer is supposed to provide an actual "solution". – Funk Forty Niner Jan 02 '16 at 22:04
  • Done, thanks for pointing it. – user3038744 Jan 02 '16 at 22:04
  • @EldaBacka if you're talking to me, I don't spoonfeed people, I teach them to learn and you haven't learned anything as to why your code failed in the first place. Edit: You deleted your comment about theirs being a better answer than mine; probably the links I gave you to "learn" proper syntax. – Funk Forty Niner Jan 02 '16 at 22:07
  • Sorry @-Fred -ii- when your time is limited a concrete demonstration of the errors really helps better than a php manual – John Dow Jan 02 '16 at 22:29