Jump to content

[SOLVED] msqli_real_escape_string is giving me an error


kjtocool

Recommended Posts

I'm using the following code:

 

<?php
$databaseConnect = mysqli_connect("localhost", "username", "password", "database")
	Or die("Unable to connect to the database.");

function test_input($input) {
	// Stripslashes
	if (get_magic_quotes_gpc()) {
	  $input = stripslashes($input);
	}

	// Escape if not a number
	if (!is_numeric($input)) {
	  $input = mysqli_real_escape_string($databaseConnect, $input);
	}

	return $input;
}

$user = test_input($_POST['userName']);
echo $user;
?>

 

And getting the following error:

 

Warning: mysqli_real_escape_string() expects parameter 1 to be mysqli, null given in /home/worldofk/public_html/CMS/loginVal.php on line 170

 

Line 170 is: $input = mysqli_real_escape_string($databaseConnect, $input);

 

Why am I getting yelled at?

I thought I'd try something else, and see what it threw at me.  So I switched the code to:

 

<?php
$userName = $_POST["userName"];
$passwordHash = md5($_POST["password"]);

$databaseConnect = mysqli_connect("localhost", "username", "password", "database")
Or die("Unable to connect to the database.");

if($databaseConnect)
echo "win";
else
echo "probably an error";

$query = "INSERT INTO Admin_Users VALUES(NULL, '$userName', '$passwordHash', 1";

if (mysqli_query($databaseConnect, $query))
echo "New user successfully created.";
else
echo "Unable to create new user.";

mysqli_close($databaseConnect);
?>

 

I got: winUnable to create new user.

 

Maybe there is something wrong with the connection, but I just can't see what it could be.

$databaseConnect does not exist within your function. You need to pass it in as an argument.

 

Interesting, I had not thought of that.  I assumed that it would have access to the variable since it was 'outside' the function.

 

I tried what you suggested (though I simply put it inside the function, in the future I would do as you suggest and pass it as a parameter).  Now I'm getting a connection error, so something is wrong with my connect statement, but I don't understand what it could be.

 

<?php
function test_input($input) {
$databaseConnect = mysqli_connect("localhost", "username", "password", "database")
	Or die("Unable to connect to the database.");

// Stripslashes
if (get_magic_quotes_gpc()) {
  $input = stripslashes($input);
}

// Escape if not a number
if (!is_numeric($input)) {
  $input = mysqli_real_escape_string($databaseConnect, $input);
}

return $input;
}

$user = test_input($_POST['userName']);
echo $user;
?>

 

Warning: mysqli_connect() [function.mysqli-connect]: (28000/1045): Access denied for user 'username'@'localhost' (using password: YES) in /home/worldofk/public_html/CMS/loginVal.php on line 160
Unable to connect to the database.

 

Is something wrong with the host variable of my mysqli_connect statement?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.