adhbvklwqdbviabjiawdnbij Posted August 29, 2011 Share Posted August 29, 2011 I'm always getting the "Falha ao selecionar o usuario no banco de dados." error. Why??? Another thing, any tip to improve my code? A way to do the same thing, but with a "more clean" code... login.php <?php session_start(); require_once('../includes/link.php'); include('../functions/clean.php'); $errmsg_arr = array(); $errflag = false; $email = clean($_POST['email']); $password = clean($_POST['password']); if(($email == '') OR ($password == '')) { $errmsg_arr[] = 'Por favor, preencha todos os campos.'; $errflag = true; $_SESSION['ERRMSG_ARR'] = $errmsg_arr; session_write_close(); header("location: ../index.php"); exit(); } $query = "SELECT * FROM users WHERE email = '$email' AND passwd = '".md5($_POST['password'])."'"; $result = mysql_query($query); $user = mysql_fetch_assoc($result); if($result) { if(mysql_num_rows($result) == 1) { $user = mysql_fetch_assoc($result); session_regenerate_id(); $_SESSION['SESS_ID'] = $user['id']; $_SESSION['SESS_STATUS'] = $user['status']; $_SESSION['SESS_SCHOOL_ID'] = $user['school_id']; $_SESSION['SESS_CLASS_ID'] = $user['class_id']; $_SESSION['SESS_NAME'] = $user['name']; $_SESSION['SESS_REGISTRATION'] = $user['registration']; $_SESSION['SESS_EMAIL'] = $user['email']; session_write_close(); if($_SESSION['SESS_STATUS'] == 1) { header("location: ../users/superadministrator/index.php"); exit(); } } else { $errmsg_arr[] = 'Suas informacoes de login estao incorreta. Por favor, tente novamente.'; $errflag = true; $_SESSION['ERRMSG_ARR'] = $errmsg_arr; session_write_close(); header("location: ../index.php"); exit(); } } else { die("Falha ao selecionar o usuario no banco de dados."); } ?> link.php <?php define('DB_HOST', 'localhost'); define('DB_USER', 'root'); define('DB_PASSWORD', ''); define('DB_DATABASE', 'social_escola'); $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD); if (!$link) { die('Falha ao conectar ao servidor: ' . mysql_error()); } $db = mysql_select_db(DB_DATABASE); if (!$db) { die('Falha ao selecionar o banco de dados: ' . mysql_error()); } ?> clean.php <?php function clean($str) { $str = @trim($str); if(get_magic_quotes_gpc()) { $str = stripslashes($str); } return mysql_real_escape_string($str); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/245926-problem-with-login-form/ Share on other sites More sharing options...
MasterACE14 Posted August 29, 2011 Share Posted August 29, 2011 remove this 'if' statement and it's attached 'else' statement: if($result) { You don't need to check for $result twice. the other 'if' statement is all you need: if(mysql_num_rows($result) == 1) { Quote Link to comment https://forums.phpfreaks.com/topic/245926-problem-with-login-form/#findComment-1262994 Share on other sites More sharing options...
adhbvklwqdbviabjiawdnbij Posted August 29, 2011 Author Share Posted August 29, 2011 I find one error and changed what you said. Changed $query = "SELECT * FROM users WHERE email = '$email' AND passwd = '".md5($_POST['password'])."'";" to $query = "SELECT * FROM users WHERE email = '$email' AND password = '".md5($_POST['password'])."'"; And removed the if($result) and it's else, but now its giving a blank screen! Quote Link to comment https://forums.phpfreaks.com/topic/245926-problem-with-login-form/#findComment-1262995 Share on other sites More sharing options...
MasterACE14 Posted August 29, 2011 Share Posted August 29, 2011 show me your new code for login.php Quote Link to comment https://forums.phpfreaks.com/topic/245926-problem-with-login-form/#findComment-1263000 Share on other sites More sharing options...
adhbvklwqdbviabjiawdnbij Posted August 29, 2011 Author Share Posted August 29, 2011 login.php <?php session_start(); require_once('../includes/link.php'); include('../functions/clean.php'); $errmsg_arr = array(); $errflag = false; $email = clean($_POST['email']); $password = clean($_POST['password']); if(($email == '') OR ($password == '')) { $errmsg_arr[] = 'Por favor, preencha todos os campos.'; $errflag = true; $_SESSION['ERRMSG_ARR'] = $errmsg_arr; session_write_close(); header("location: ../index.php"); exit(); } $query = "SELECT * FROM users WHERE email = '$email' AND password = '".md5($_POST['password'])."'"; $result = mysql_query($query); $user = mysql_fetch_assoc($result); if(mysql_num_rows($result) == 1) { $user = mysql_fetch_assoc($result); session_regenerate_id(); $_SESSION['SESS_USER_ID'] = $user['id']; $_SESSION['SESS_STATUS'] = $user['status']; $_SESSION['SESS_SCHOOL_ID'] = $user['school_id']; $_SESSION['SESS_CLASS_ID'] = $user['class_id']; $_SESSION['SESS_NAME'] = $user['name']; $_SESSION['SESS_REGISTRATION'] = $user['registration']; $_SESSION['SESS_EMAIL'] = $user['email']; session_write_close(); if($_SESSION['SESS_STATUS'] == 1) { header("location: ../users/superadministrator/index.php"); exit(); } } else { $errmsg_arr[] = 'Suas informacoes de login estao incorreta. Por favor, tente novamente.'; $errflag = true; $_SESSION['ERRMSG_ARR'] = $errmsg_arr; session_write_close(); header("location: ../index.php"); exit(); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/245926-problem-with-login-form/#findComment-1263002 Share on other sites More sharing options...
adhbvklwqdbviabjiawdnbij Posted August 29, 2011 Author Share Posted August 29, 2011 Here is my database: CREATE TABLE IF NOT EXISTS `users` ( `id` int(100) unsigned NOT NULL auto_increment, `status` int(100) default NULL, `school_id` int(100) default NULL, `class_id` int(100) default NULL, `registration` varchar(255) default NULL, `name` varchar(255) NOT NULL default '', `email` varchar(255) NOT NULL default '', `password` varchar(255) NOT NULL default '', PRIMARY KEY (`id`) ) ENGINE=MyISAM; CREATE TABLE IF NOT EXISTS `schools` ( `id` int(100) unsigned NOT NULL auto_increment, `name` varchar(255) NOT NULL default '', PRIMARY KEY (`id`) ) ENGINE=MyISAM; CREATE TABLE IF NOT EXISTS `classes` ( `id` int(100) unsigned NOT NULL auto_increment, `school_id` int(100) default NULL, `name` varchar(255) NOT NULL default '', PRIMARY KEY (`id`) ) ENGINE=MyISAM; CREATE TABLE IF NOT EXISTS `subjects` ( `id` int(100) unsigned NOT NULL auto_increment, `school_id` int(100) default NULL, `class_id` int(100) default NULL, `teacher_id` int(100) default NULL, `name` varchar(255) NOT NULL default '', PRIMARY KEY (`id`) ) ENGINE=MyISAM; CREATE TABLE IF NOT EXISTS `grades` ( `id` int(100) unsigned NOT NULL auto_increment, `school_id` int(100) default NULL, `class_id` int(100) default NULL, `subject_id` int(100) default NULL, `teacher_id` int(100) default NULL, `student_id` int(100) default NULL, `value` float(2,2) default NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM; INSERT INTO `users` (`name`, `status`, `email`, `password`) VALUES ('Claudius ou Afonso', '1', 'email', 'e8d95a51f3af4a3b134bf6bb680a213a'); Quote Link to comment https://forums.phpfreaks.com/topic/245926-problem-with-login-form/#findComment-1263012 Share on other sites More sharing options...
MasterACE14 Posted August 29, 2011 Share Posted August 29, 2011 ok I can't see anything wrong with the file, or the database. Time to do some debugging. Add die('Error'); before the first 'if' statement and see if that is displayed, if so, you know everything is working in the script up until that point. Now move the die('Error'); inside the 'if' statement at the end of it. If it comes up again, move it further down the script. Keep moving it down from statement to statement until 'Error' doesn't come up. And post your code then so I can see where it is. Quote Link to comment https://forums.phpfreaks.com/topic/245926-problem-with-login-form/#findComment-1263023 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.