For some reason when I run my code, I keep getting a parameter error.
I'm running XAMP, Atom, mySql and PhpMyAdmin. I realised maybe is was to do with the fact that I was using
mysql_real_escape_string
which isn't supported anymore. So I changed it to mysqli, but now its showing a different error.
I'm new to the whole programming scene, so I'm quite behind with everything.
$username = "";
$email = "";
$errors = array();
//connect to the database
$db = mysqli_connect('localhost', 'root', '', 'regist');
//if the register is clicked
if (isset($_POST['register'])) {
$username = mysqli_real_escape_string($_POST['username'], $db);
$email = mysqli_real_escape_string($_POST['email'], $db);
$password_1 = mysqli_real_escape_string($_POST['password_1'], $db);
$password_2 = mysqli_real_escape_string($_POST['password_2'], $db);
//ensure the form fields are filled properly
if (empty($username)) {
array_push($errors, "Username is required");
}
if (empty($email)) {
array_push($errors, "Email is required");
}
if (empty($password_1)) {
array_push($errors, "Password is required");
}
if ($password_1 != $password_2) {
array_push($errors, "The two password do not match");
}
//if there are no errors, save user to database
if (count($errors)==0) {
$password = md5($password_1); //encrypt password before storing in database
$sql = "INSERT INTO users (username, email, password) VALUES ('$username', '$email', '$password')";
mysqli_query($db, $sql);
}
}
I was expecting it to register the details into the database, instead i get the errors listed below.
Warning: mysqli_real_escape_string() expects parameter 1 to be mysqli, string given in C:\xampp\htdocs\Resgistration\server.php on line 11
Warning: mysqli_real_escape_string() expects parameter 1 to be mysqli, string given in C:\xampp\htdocs\Resgistration\server.php on line 12
Notice: Undefined index: password_1 in C:\xampp\htdocs\Resgistration\server.php on line 13
Warning: mysqli_real_escape_string() expects parameter 1 to be mysqli, null given in C:\xampp\htdocs\Resgistration\server.php on line 13
Warning: mysqli_real_escape_string() expects parameter 1 to be mysqli, string given in C:\xampp\htdocs\Resgistration\server.php on line 14
mysqli_real_escape_string()are in the wrong order. Though, just drop that function entirely and use$db->prepare()with placeholders instead of$db->query().sha1,md5) are poor methods of hashing - you should use newer methods for hashing your passwords. PHP has a built-inpassword_hash()function which is a lot more secure!mysqli::prepare()andmysqli_stmt::bind_param().mysqli_real_escape_string()isn't enough to secure your queries