kig på eks 1
http://php.net/manual/en/function.mysql-fetch-assoc.php$sql = "SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result = mysql_query($sql);
$count = mysql_num_rows($result);
$row = mysql_fetch_assoc($result);og så er der vel ikke nogle grund til at teste om count er lig med 1 flere gange
nåde ala dette
jeg har omdøbt din role til role_id
(utested)
<?php
// hvis session ikke er sat startes session, husk session_start() skal være på alle sider der anvender session
if(! isset($_SESSION) ) {session_start(); }
define('IN_DEBUG_MODE', true); // Sæt til false i production mode
if (IN_DEBUG_MODE) {
ini_set("display_errors", "on");
error_reporting(-1);
} else {
// Turn off all error reporting
error_reporting(0);
}
$host = "localhost"; // Host name
$username = "root"; // Mysql username
$password = ""; // Mysql password
$db_name = "test"; // Database name
$tbl_name = "tbl_udv"; // Table name
// Connect to server and select database.
mysql_connect($host, $username, $password) or die("cannot connect");
mysql_select_db($db_name)or die("cannot select DB");
// username and password sent from form
$myusername = $_POST['myusername'];
$mypassword = $_POST['mypassword'];
// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);
$sql = "SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result = mysql_query($sql);
$count = mysql_num_rows($result);
if ($count == 1) {
$row = mysql_fetch_assoc($result);
// administrator
if ($row['role_id'] == 2) {
$_SESSION['user'] = $myusername;
$_SESSION['name'] = $row['name'];
// Remember login if checked
if (isset($_POST["remember"])) {
setcookie('username', $myusername, time() + 3600 * 168, '/');
setcookie('password', $mypassword, time() + 3600 * 168, '/');
} else {
setcookie('username', "", time() - 3600, '/');
setcookie('password', "", time() - 3600, '/');
}
header("location:admin.php");
}
// "Superadmin"
else{
if ($row['role_id'] == 1) {
session_register("myusername");
session_register("mypassword");
$_SESSION['user'] = $myusername;
$_SESSION['name'] = $row['name'];
// Remember login if checked
if (isset($_POST["remember"])) {
setcookie('username', $myusername, time() + 3600 * 168, '/');
setcookie('password', $mypassword, time() + 3600 * 168, '/');
} else {
setcookie('username', "", time() - 3600, '/');
setcookie('password', "", time() - 3600, '/');
}
header("location:super.php");
}
}
}
?>
forslag til base
(utested)
CREATE TABLE IF NOT EXISTS `tbl_role` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) COLLATE utf8_danish_ci NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_danish_ci AUTO_INCREMENT=4 ;
--
-- Data dump for tabellen `tbl_role`
--
INSERT INTO `tbl_role` (`id`, `name`) VALUES
(1, 'Super Admin'),
(2, 'Admin'),
(3, 'Bruger');
-- --------------------------------------------------------
--
-- Struktur-dump for tabellen `tbl_udv`
--
CREATE TABLE IF NOT EXISTS `tbl_udv` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(255) COLLATE utf8_danish_ci NOT NULL,
`password` varchar(255) COLLATE utf8_danish_ci NOT NULL,
`role_id` int(11) NOT NULL,
`name` varchar(255) COLLATE utf8_danish_ci NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_danish_ci AUTO_INCREMENT=4 ;
--
-- Data dump for tabellen `tbl_udv`
--
INSERT INTO `tbl_udv` (`id`, `username`, `password`, `role_id`, `name`) VALUES
(1, 'sa', '1234', 1, 'super administrator'),
(2, 'admin', '1234', 2, 'administrator'),
(3, 'user', '1234', 3, 'bruger');
tilføjet
dersom der havde været flere rækker der skulle hentes ud skulle fetch_assoc selvføgelig være i en while løkke som der også er vist i eks 1 på php.net
Indlæg senest redigeret d. 03.12.2014 15:55 af Bruger #16075