Jump to content

[SOLVED] Login script problems..


selbekk

Recommended Posts

hey again...

 

i have a problem logging in with my new log in script... i get the error:

 

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/kristof3/public_html/tempwork/login.php on line 24
Wrong Username or Password. Try again.

 

this is my code:

<?php
/* Script that handles the login procedure. Gets info from login-form.php */
include_once("mysql-connect.php"); // Include the connection info for the MySQL-database

// Makes variables for the user info.
$username = $_POST['user'];
$password = $_POST['pass'];

// Protection against MySQL insertion (security measure)
$username = stripslashes($username);
$password = stripslashes($password);
$username = mysql_real_escape_string($username);
$password = mysql_real_escape_string($password);

// Encrypts the password
$password = md5($password);

// Making a query to the database
$sql = "SELECT * FROM login WHERE username='$username' AND password='$password'";
$result = mysql_query($sql);

// Checking if there is a match, and only one match
$count = mysql_num_rows($result);
if ($count == 1) {
// 
session_register("username");
session_register("password"); 
header("location:index.php?login=good");
} else echo "Wrong Username or Password. <a href='index.php?page=login-form'>Try again.</a>";
?>

 

Link to comment
https://forums.phpfreaks.com/topic/101429-solved-login-script-problems/
Share on other sites

Try using complete syntax in your query, also you should always verify the query works before continuing:

 

<?php
/* Script that handles the login procedure. Gets info from login-form.php */
include_once("mysql-connect.php"); // Include the connection info for the MySQL-database

// Makes variables for the user info.
$username = $_POST['user'];
$password = $_POST['pass'];

// Protection against MySQL insertion (security measure)
$username = stripslashes($username);
$password = stripslashes($password);
$username = mysql_real_escape_string($username);
$password = mysql_real_escape_string($password);

// Encrypts the password
$password = md5($password);

// Making a query to the database
$sql = "SELECT * FROM `login` WHERE `username` = '$username' AND `password` = '$password'";
$result = mysql_query($sql);

if(!$result){
   die("Unable to connect to database. ".mysql_error());
}

// Checking if there is a match, and only one match
$count = mysql_num_rows($result);
if ($count == 1) {
// 
session_register("username");
session_register("password"); 
header("location:index.php?login=good");
} else echo "Wrong Username or Password. <a href='index.php?page=login-form'>Try again.</a>";
?>

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.