Jump to content

PHP -> Login and Validation


Adam Brave

Recommended Posts

Hi, I want to make a login and a validation in a database. In order to do it I use the following code where: user -> table with all the user information like birthday, mail, username, first name etcpassword -> column of the table "user" that contains all the passwordsmail -> column of the table "user" that contains all the mails. The two variables, $maill and $passwordd, are use to store the information required at the login moment to the user.

$result = mysql_query("SELECT password FROM user WHERE mail=$maill");$row = mysql_fetch_array($result); if ( $row['mail'] == $maill &&  $row['password'] == $passwordd ){    echo $row['name'] . " " . $row['ultimonome'];  } else  {   echo "login not correct";  }

Output: Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\Program Files\EasyPHP-12.1\www\welcome.php on line 29login not correct Can someone help me?

Link to comment
Share on other sites

From what I see, the information submitted by the user (mail=asd@hotmail.com and password=asd) have something wrongon the password. For example I printed the two variables and get this:

 <form action="welcome.php" method="post">mail: <input type="text" name="fmail">password: <input type="text" name="fpass"><input type="submit"></form>...$maill = $_POST['fmail'];$passwordd = $_POST['fpass'];echo $maill;echo "<br>";echo $passwordd;

Output:asd@hotmail.comasdbool(false) From where come the "bool(false)" ?

Link to comment
Share on other sites

I have changed the code to this:

$maill = $_POST['fmail'];$passwordd = $_POST['fpass'];$mysqli = new mysqli("localhost",$maill,"");/* check connection */if ($mysqli->connect_errno) {    printf("Connect failed: %s\n", $mysqli->connect_error);    exit();}$mysqli->select_db("database.accdb");$result = $mysqli->query("SELECT * FROM user WHERE mail=$maill AND password=$passwordd");$row = $result->fetch_row();printf("Default database is %s.\n", $row[0]);

And my output is: "Fatal error: Call to a member function fetch_row() on a non-object in C:\Program Files\EasyPHP-12.1\www\welcome.php on line 27" Line 27 :

$row = $result->fetch_row();

Now, I'm sure that I have a row in my database with the mail and password stored on the variables $maill and $password and, I'm also sure, that these variables have the corresponding information so why can't I validate that user?

Link to comment
Share on other sites

"database.accdb"?!?!?Are you trying to use MySQLi to access an MS Access (2007+) database? If so, that simply won't work.The fact you're not getting an object means the query failed, so $result is a boolean false. You can check the reason for the failure at the $error property, e.g.

$maill = $_POST['fmail'];$passwordd = $_POST['fpass'];$mysqli = new mysqli("localhost",$maill,"");/* check connection */if ($mysqli->connect_errno) {    printf("Connect failed: %s\n", $mysqli->connect_error);    exit();}$mysqli->select_db("database.accdb");$result = $mysqli->query("SELECT * FROM user WHERE mail=$maill AND password=$passwordd");if (false === $result) {    printf('Error %s: %s', $mysqli->errno, $mysqli->error);} else {    $row = $result->fetch_row();    printf("Default database is %s.\n", $row[0]);}

Link to comment
Share on other sites

Solved :)

$result = mysql_query(SELECT * FROM userdb WHERE mail LIKE '$maill' AND pass LIKE '$passwordd');

Btw, can someone tell me how do I load a html page with php code? In javascript I would use the instruction location.href but how do I load a page with php?

Edited by Adam Brave
Link to comment
Share on other sites

location.href = "some-page.php"

or just link to the PHP file with an <a> tag and just have the PHP file output the HTML you want.

Edited by thescientist
Link to comment
Share on other sites

In PHP you can load data from another file using include() but if you want a redirect, you can use a location header:

header('Location: http://example.com/');exit;

The specifications say URLs in the location header must be absolute and prepended by the http: or https: protocol.If you send a location header, it must be sent before any HTML or printed content.

Link to comment
Share on other sites

In PHP you can load data from another file using include() but if you want a redirect, you can use a location header:
header('Location: http://example.com/');exit;

The specifications say URLs in the location header must be absolute and prepended by the http: or https: protocol.If you send a location header, it must be sent before any HTML or printed content.

Tkz, it worked :)
Link to comment
Share on other sites

  • 1 year later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...