Friday, 8 December 2017

php - Warning: mysqli_real_escape_string() expects exactly 2 parameters, 1 given... what i do wrong?

itemprop="text">

I try make php login but i get this
error: Warning: mysqli_real_escape_string() expects exactly 2 parameters, 1
given,
what I do wrong?



register.php




            html>


rel="icon" type='image/png'
href='images/logo.png'>
<br/>asd<br/>
href="css/styles.css" type="text/css"
rel="stylesheet">




method="post">

placeholder="Username">

placeholder="Email">

placeholder="Password">

placeholder="Password">

value="Register">




if(isset($_POST['submit']))
{
$username
= mysqli_real_escape_string($_POST['username']);
$pass =
mysqli_real_escape_string($_POST['pass']);
$pass1 =
mysqli_real_escape_string($_POST['pass1']);
$email =
mysqli_real_escape_string($_POST['email']);
if($username && $pass
&& $pass1 &&
$email)
{
if($pass==$pass1)

{

$connect = mysql_connect("mysql.example.com","username","password");

mysql_select_db("my_database");
$query = mysql_query("INSERT INTO users
VALUES('$username','$pass','$email');");
echo "You have been
registered.";
}
else
{
echo "Password must
match.";
}

}
else
{
echo
"All fields are required.";
}
}

?>


href="index.php">


asd



id="main">






class="jonne">

src="javascript/jquery.js">




i
use 000webhost and this first time when i use mysql databases
online.



Answer




You are mixing
mysqli and mysql
function.




If your are using mysql
function then instead
mysqli_real_escape_string($your_variable);
use



$username =
mysql_real_escape_string($_POST['username']);
$pass =
mysql_real_escape_string($_POST['pass']);
$pass1 =
mysql_real_escape_string($_POST['pass1']);
$email =
mysql_real_escape_string($_POST['email']);


If
your using mysqli_* function then you have to include your connection to database into
rel="noreferrer">mysqli_real_escape function
:




$username =
mysqli_real_escape_string($your_connection, $_POST['username']);
$pass =
mysqli_real_escape_string($your_connection, $_POST['pass']);
$pass1 =
mysqli_real_escape_string($your_connection, $_POST['pass1']);
$email =
mysqli_real_escape_string($your_connection,
$_POST['email']);


Note
: Use mysqli_* function since mysql has been deprecated. For information please read
rel="noreferrer">mysqli_*



No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print ...