0

I created an account on 000webhost to create html page that has a login form. But I am facing errors when i try to connect the form to the database.

Here is the html code:

     <html>

   <head>
      <title>Login Page</title>

      <style type = "text/css">
         body {
            font-family:Arial, Helvetica, sans-serif;
            font-size:14px;
         }

         label {
            font-weight:bold;
            width:100px;
            font-size:14px;
         }

         .box {
            border:#666666 solid 1px;
         }
      </style>

   </head>

   <body bgcolor = "#FFFFFF">




      <div align = "center">
         <div style = "width:300px; border: solid 1px #333333; " align = "left">
            <div style = "background-color:#333333; color:#FFFFFF; padding:3px;"><b>Login</b></div>

            <div style = "margin:30px">

               <form method="POST" action="C1.php">
                  <label>UserName  :</label><input type = "text" name = "username" required = "box"/><br /><br />
                  <label>Password  :</label><input type = "password" name = "password" required = "box" /><br/><br />


                  <input type = "submit" name="submit" value = " Login "/><br />

               </form>

               <div style = "font-size:11px; color:#cc0000; margin-top:10px"><?php echo $error; ?></div>

            </div>

         </div>

      </div>
   </body>
</html>

here is my php page

       <?php
 session_start();
$servername = "localhost";
$username = "******";
$password = "******";

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

 if (isset($_POST['submit']))
        {     

    $username='king';
    $password='king123';

    $query = mysqli_query($conn, "SELECT * FROM mylogin WHERE username='$username' and password='$password'");
     if (mysqli_num_rows($query) != 0)
    {
     echo "sucess";   
      }
      else
      {
    echo "fail";
    }
    }
    ?>

mysql has a table named mylogin and has the values:

INSERT INTO `mylogin`(`username`, `password`) VALUES ('king','king123')

i couldn't connect to the database, appreciate ur help in advance

  • code updated
Turki A
  • 9
  • 3

2 Answers2

1

You need to change this line:

 <input type = "submit" value = " Login "/>

To this:

 <input type = "submit" name="submit" value = " Login "/>

The if() statement in your php wasn't picking up on $_POST['submit'] because it didn't exist.

You're also mixing mysql_ and mysqli_ Everything needs to be mysqli_ given that mysql_ is deprecated and generally considered unsafe.

Difster
  • 3,264
  • 2
  • 22
  • 32
  • That's not the only issue. Mixing APIs too – Qirel Aug 01 '17 at 03:22
  • 1
    Good catch! Thanks. I'll edit my answer. – Difster Aug 01 '17 at 03:22
  • Also `$_POST['king']` the `king` is the value of the POST it looks like, not the name. – chris85 Aug 01 '17 at 03:24
  • I just assumed those were there as place holder for testing, that's why I didn't comment on it. – Difster Aug 01 '17 at 03:26
  • Warning: mysqli_connect(): (HY000/2002): Connection refused in /storage/ssd1/621/2425621/public_html/C1.php on line 8 Warning: mysqli_query() expects at least 2 parameters, 1 given in /storage/ssd1/621/2425621/public_html/C1.php on line 16 Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, null given in /storage/ssd1/621/2425621/public_html/C1.php on line 17 fail ( it gives me this errors) – Turki A Aug 01 '17 at 03:29
  • Please post your updated code in the original question so we know what we're working with. And mind the passwords. – Difster Aug 01 '17 at 03:30
  • I made the changes given by Difster & got the above errors – Turki A Aug 01 '17 at 03:31
  • Well you don't have anything your form with fields name `king` `$username=$_POST['king']; $password=$_POST['king123'];` Those should be: `$username=$_POST['username']; $password=$_POST['password'];` Unless you're trying to just bypass the actual form. Then make it `$username='king'; $password='king123';` – Difster Aug 01 '17 at 03:32
  • @TurkiA Stop at `Connection refused` and look for errors. Look at the manual for how `mysqli_query` should be used. – chris85 Aug 01 '17 at 03:38
  • If the connection is refused then you have got the wrong database password. – Difster Aug 01 '17 at 03:41
0

You did not mention about databasae. Your MySqli Connection needs the database parameter.

$conn = mysqli_connect($servername, $username, $password);

should be

$database_name = "foo";
        $conn = mysqli_connect($servername, $username, $password, $database_name);

First you can check whether your connection is correct using below code.

    if($conn){
    echo "Success";
    }
else{
    echo "Error Connection";}

AND

MySQL Functions should be according to MySQLi

 $query = mysqli_query($conn, "SELECT * FROM mylogin WHERE username='$username' and password='$password'");
     if (mysqli_num_rows($query) != 0)
Vimukthi Guruge
  • 285
  • 3
  • 15
  • Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in /storage/ssd1/621/2425621/public_html/C1.php on line 18 fail (that's what i got when i tried to log in) – Turki A Aug 01 '17 at 03:47
  • this is line 18: if (mysqli_num_rows($query) != 0) – Turki A Aug 01 '17 at 03:50
  • first check the connection is success and then try other codes. – Vimukthi Guruge Aug 01 '17 at 03:58
  • I updated the code above, I don't get errors, i get the message "success" even when i type the wrong username and password – Turki A Aug 01 '17 at 04:05
  • I think I need to change this line of code: if (mysqli_num_rows($query) != 0) *but i am not sure what to change it for, I really appreciate ur help – Turki A Aug 01 '17 at 04:16