0
I am creating a simple php login page based on in this video.
However, after doing exactly as the video describes, I learned that my PHP version has updated and it is no longer possible to use the command mysql_connect
.
<?php
// Get values passe from form in login.php file
$username = $_POST['user'];
$password = $_POST['pass'];
//to prevent mysql injection
$username = stripcslashes($username);
$password = stripcslashes($password);
$username = mysql_real_escape_string($username);
$password = mysql_real_escape_string($password);
//connect to the server and select database
mysql_connect("localhost", "root", "");
mysql_select_db("projeto_rc");
//Query for database user
$result = mysql_query("SELECT * FROM utilizadores 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";
}
Based on this code, how can I change it to support the minimum version of PHP 7? I know I should start by changing from mysql_real_escape_string
for mysqli_real_escape_string
as indicated by this error:
Fatal error: Uncaught Error: Call to Undefined Function mysql_real_escape_string() in C: xampp htdocs login process.php:9 Stack trace: #0 {main} thrown in C: xampp htdocs login process.php on line 9
However, you would need to use 2 parameters between parentheses and just use one. What are some of the possible solutions?
Take a look at filter_var. Documentation: http://php.net/manual/en/function.filter-var.php Tutorial: http://www.phpit.com.br/artigos/filtrando-e-validando-dados-no-php-filter_var.phpit
– Vítor André
Even with your question about the
mysql_real_escape_string
, I believe what you’re looking for is addslashes– rbz