0

I'm trying to dabble with php instead of cgi when it comes to creating a login page for a website. Here's what I've come up with:

<?php
 $username = mysqli_connect("localhost", "$_POST['user']", ;  
 $password = $_POST['pass'];

 $username = stripcslashes($username);
 $password = stripcslashes($password);
 $username = mysql_real_escape_string($username,$_POST['user'] );
 $password = mysql_real_escape_string($password,$_POST['pass'] );

 mysqli_connect("localhost", "root", "");
 mysql_select_db("login");

 $result= mysql_query("select * from users where username = '$username' and password = '$password'")
 or die("Failed to query database ".mysql_error());
 $row = mysql_fetch_Array($result);
 if($row=['username'] == $username && $row['password'] == $password)   
      {
      echo "Login success!!!! Welcome ".$row['username'];
      }
 else {
      echo "Failed to login!";
      }

?> 

I have received these errors upon compiling it with XAMPP Apache.

Notice: Undefined index: user in /opt/lampp/htdocs/process.php on line 2

Notice: Undefined index: pass in /opt/lampp/htdocs/process.php on line 3

Fatal error: Uncaught Error: Call to undefined function mysql_real_escape_string() in /opt/lampp/htdocs/process.php:7 Stack trace: #0 {main} thrown in /opt/lampp/htdocs/process.php on line 7

It's my first time dabbling with php and i just followed a YouTube tutorial so I really don't know how to fix this error. Any help? Thanks.

Kyle Ramos
  • 39
  • 1
  • 10
  • 1
    Throw away your tutorial as it is completely outdated. Noone uses `mysql` extension now. – u_mulder Oct 29 '16 at 21:54
  • Possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – u_mulder Oct 29 '16 at 21:56
  • 1
    `mysql_real_escape_string` was a built-in function that has been removed with PHP 7. The tutorial is simply too old. – Marvin Oct 29 '16 at 21:57
  • 1
    Oh, and you're mixing APIs. We need @Fred-ii- here. – u_mulder Oct 29 '16 at 21:58
  • I think you need to check if ur parameters are set before using them. Something along the lines of if(isset($_POST['username'])) { now assign variable to it } – Mueyiwa Moses Ikomi Oct 29 '16 at 22:09

0 Answers0