-2

Fatal error: Uncaught Error: Call to undefined function mysql_real_escape_string() in C:\xampp\htdocs\MyFirstWebsite\register.php:19 Stack trace: #0 {main} thrown in C:\xampp\htdocs\MyFirstWebsite\register.php on line 19

   <?php
if($_SERVER["REQUEST_METHOD"] == "POST"){
    $username = mysql_real_escape_string($_POST['username']);
    $password = mysql_real_escape_string($_POST['password']);
    echo "Username entered is : ". $username . "<br/>";
    echo "Password entered is : ". $password;
}
?>

Here in line 19 the following code lies:

$username = mysql_real_escape_string($_POST['username']);

Then I tried using mysqli().so i changed my php code like this:

<?php
if($_SERVER["REQUEST_METHOD"] == "POST"){
    $username = mysqli_real_escape_string($_POST['username']);
    $password = mysqli_real_escape_string($_POST['password']);
    echo "Username entered is : ". $username . "<br/>";
    echo "Password entered is : ". $password;
}
?>

Then it shows this output:

Warning: mysqli_real_escape_string() expects exactly 2 parameters, 1 given in C:\xampp\htdocs\MyFirstWebsite\register.php on line 19

Warning: mysqli_real_escape_string() expects exactly 2 parameters, 1 given in C:\xampp\htdocs\MyFirstWebsite\register.php on line 20

Here, on line 19 and line 20 .The following code lies:

$username = mysqli_real_escape_string($_POST['username']);
        $password = mysqli_real_escape_string($_POST['password']);

Can anyone help me and tell me what should be in that code?

S. Shrestha
  • 79
  • 1
  • 3
  • 9
  • with the code using `mysqli_` it's telling you what the problem and googling the function name the very first result [is the documentation](http://php.net/manual/en/mysqli.real-escape-string.php) showing you can't just stick an i in the function name and expect it to work – Memor-X Oct 26 '16 at 03:11
  • You should pass link identifier as first parameter, see it on PHP Manual http://php.net/manual/en/mysqli.real-escape-string.php – Joko Wandiro Oct 26 '16 at 03:12
  • See also http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php – Machavity Oct 26 '16 at 03:21

1 Answers1

0

You have to pass connection variable as first argument like below:

$username = mysqli_real_escape_string($conn,$_POST['username']);

Here $conn is

$conn = mysqli_connect('hostname','username','password','dbname');
Devsi Odedra
  • 5,244
  • 1
  • 23
  • 37